#!/bin/bash #判断字符串是否相等 if [ "ok" == "ok" ] then echo"equal" fi #判断23是否大于22 if [ 23 -gt 22 ] then echo"大于" fi #判断文件/home/aaa.txt是否存在 if [ -e /home/aaa.txt ] then echo"存在" fi
equal 大于 存在
1 2 3 4 5 6 7 8 9 10
#!/bin/bash if [ $1 -ge 60 ] then echo"及格了" elif [ $1 -lt 60 ] then echo"不及格" else echo"搞个鸡儿" fi
2 流程控制
2.1 for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/bin/bash for i in"$*" do echo"the num is $i" done echo"================" for i in"$@" do echo"the num is $i" done
the num is 1 2 3 ================ the num is 1 the num is 2 the num is 3
1 2 3 4
for i in {1..100} do echo$i done
1 2 3 4 5 6 7 8 9 10
#!/bin/bash SUM=0 for (( i=1;i<=100;i++)) do SUM=$[SUM+$i] done echo$SUM
5050
1 2 3 4
forfilein $(ls /root) do echo $file done
2.2 while
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash SUM=0 i=1 while [ $i -le $1 ] do SUM=$[$SUM+$i] i=$[$i+1] done echo$SUM
./testWhile.sh 100 5050
1 2 3 4 5 6 7 8 9
#!/bin/bash i=1 sum=0 while [ $i -le $1 ] do let sum=sum+$i let i++ done echo$sum