您的位置:首页 > 运维架构 > Shell

Linux 系统命令及shell脚本2

2017-11-16 17:33 471 查看
shell脚本的常用用法

01.sh

!/bin/bash

echo “参数1”echo“参数2”

echo “参数长度#”
for param in “@”

do

echo “param:$param”

done

调用

./01.sh 1 2 3

02.sh

-d 检查文件是否存在,并且是一个目录

-e 检查file是否存在

-f 检查是否存在,并且是一个文件

-r 检查是否存在,可读

-w 检查文件是否可写

-x 是否可执行

Switch的使用 03.sh

!/bin/bash

testUser=1casetestUser in

tim)

echo “tim”;;

david)

echo “david”;;

*)

echo “默认”;;

esac

*好等于default

每个case结束两个分号

调用

./03.sh tim

for循环04.sh

!/bin/bash

for item in Mther Father Brother

do

echo “$item”

done

for循环05.sh

!/bin/bash

Family=”Mther Father Brother”

for item in Familydoecho“item”

done

While 循环06.sh

!/bin/bash

a=10

while [ a−gt0]doa=[ a−1]echo“结果为:a”

if [$a -eq 5 ]

then

echo “跳出循环”

break

fi

done

写入文件 07.sh

输出

追加输出

<输入

!/bin/bash

file=test6

echo “aa”>fileecho“bb”>>file

‘>!’:如果存在则覆盖

‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中

‘>>’:追加到文件中

‘>>&’:屏幕上的信息追加到文件中

01.sh

!/bin/bash

echo “参数1”echo“参数2”

echo “参数长度#”
for param in “@”

do

echo “param:$param”

done

调用

./01.sh 1 2 3

02.sh

-d 检查文件是否存在,并且是一个目录

-e 检查file是否存在

-f 检查是否存在,并且是一个文件

-r 检查是否存在,可读

-w 检查文件是否可写

-x 是否可执行

Switch的使用 03.sh

!/bin/bash

testUser=1casetestUser in

tim)

echo “tim”;;

david)

echo “david”;;

*)

echo “默认”;;

esac

*好等于default

每个case结束两个分号

调用

./03.sh tim

for循环04.sh

!/bin/bash

for item in Mther Father Brother

do

echo “$item”

done

for循环05.sh

!/bin/bash

Family=”Mther Father Brother”

for item in Familydoecho“item”

done

While 循环06.sh

!/bin/bash

a=10

while [ a−gt0]doa=[ a−1]echo“结果为:a”

if [$a -eq 5 ]

then

echo “跳出循环”

break

fi

done

写入文件 07.sh

输出

追加输出

<输入

!/bin/bash

file=test6

echo “aa”>fileecho“bb”>>file

‘>!’:如果存在则覆盖

‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中

‘>>’:追加到文件中

‘>>&’:屏幕上的信息追加到文件中

找文件内容

grep -n bin 06.sh

01.sh

!/bin/bash

echo “参数1”echo“参数2”

echo “参数长度#”
for param in “@”

do

echo “param:$param”

done

调用

./01.sh 1 2 3

02.sh

-d 检查文件是否存在,并且是一个目录

-e 检查file是否存在

-f 检查是否存在,并且是一个文件

-r 检查是否存在,可读

-w 检查文件是否可写

-x 是否可执行

Switch的使用 03.sh

!/bin/bash

testUser=1casetestUser in

tim)

echo “tim”;;

david)

echo “david”;;

*)

echo “默认”;;

esac

*好等于default

每个case结束两个分号

调用

./03.sh tim

for循环04.sh

!/bin/bash

for item in Mther Father Brother

do

echo “$item”

done

for循环05.sh

!/bin/bash

Family=”Mther Father Brother”

for item in Familydoecho“item”

done

While 循环06.sh

!/bin/bash

a=10

while [ a−gt0]doa=[ a−1]echo“结果为:a”

if [$a -eq 5 ]

then

echo “跳出循环”

break

fi

done

写入文件 07.sh

输出

追加输出

<输入

!/bin/bash

file=test6

echo “aa”>fileecho“bb”>>file

‘>!’:如果存在则覆盖

‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中

‘>>’:追加到文件中

‘>>&’:屏幕上的信息追加到文件中

找文件内容

grep -n bin 06.sh

01.sh

!/bin/bash

echo “参数1”echo“参数2”

echo “参数长度#”
for param in “@”

do

echo “param:$param”

done

调用

./01.sh 1 2 3

02.sh

-d 检查文件是否存在,并且是一个目录

-e 检查file是否存在

-f 检查是否存在,并且是一个文件

-r 检查是否存在,可读

-w 检查文件是否可写

-x 是否可执行

Switch的使用 03.sh

!/bin/bash

testUser=1casetestUser in

tim)

echo “tim”;;

david)

echo “david”;;

*)

echo “默认”;;

esac

*好等于default

每个case结束两个分号

调用

./03.sh tim

for循环04.sh

!/bin/bash

for item in Mther Father Brother

do

echo “$item”

done

for循环05.sh

!/bin/bash

Family=”Mther Father Brother”

for item in Familydoecho“item”

done

While 循环06.sh

!/bin/bash

a=10

while [ a−gt0]doa=[ a−1]echo“结果为:a”

if [$a -eq 5 ]

then

echo “跳出循环”

break

fi

done

写入文件 07.sh

输出

追加输出

<输入

!/bin/bash

file=test6

echo “aa”>fileecho“bb”>>file

‘>!’:如果存在则覆盖

‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中

‘>>’:追加到文件中

‘>>&’:屏幕上的信息追加到文件中

找文件内容

grep -n bin 06.sh

01.sh

!/bin/bash

echo “参数1”echo“参数2”

echo “参数长度#”
for param in “@”

do

echo “param:$param”

done

调用

./01.sh 1 2 3

02.sh

-d 检查文件是否存在,并且是一个目录

-e 检查file是否存在

-f 检查是否存在,并且是一个文件

-r 检查是否存在,可读

-w 检查文件是否可写

-x 是否可执行

Switch的使用 03.sh

!/bin/bash

testUser=1casetestUser in

tim)

echo “tim”;;

david)

echo “david”;;

*)

echo “默认”;;

esac

*好等于default

每个case结束两个分号

调用

./03.sh tim

for循环04.sh

!/bin/bash

for item in Mther Father Brother

do

echo “$item”

done

for循环05.sh

!/bin/bash

Family=”Mther Father Brother”

for item in Familydoecho“item”

done

While 循环06.sh

!/bin/bash

a=10

while [ a−gt0]doa=[ a−1]echo“结果为:a”

if [$a -eq 5 ]

then

echo “跳出循环”

break

fi

done

写入文件 07.sh

输出

追加输出

<输入

!/bin/bash

file=test6

echo “aa”>fileecho“bb”>>file

‘>!’:如果存在则覆盖

‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中

‘>>’:追加到文件中

‘>>&’:屏幕上的信息追加到文件中

找文件内容

grep -n bin 06.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 脚本 linux