标签:linux linux shell 编程 shell 脚本
前面有了变量的概念和逻辑运算符,就可以写判断语句了,不过这里注意中括号的两边必须得留空格,不然报错.
先举个栗子呗~
#!/bin/bash read var1 if [ "$var1" -gt 15 ] then echo "$var1 is greater than 15" else echo "$var1 is less than 15" fi
#!/bin/bash read day week=(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) case $day in 1) echo ${week[1]};; 2) echo ${week[2]};; 3) echo ${week[3]};; 4) echo ${week[4]};; 5) echo ${week[5]};; 6) echo ${week[6]};; 7) echo ${week[0]};; *) echo "no such day...";; esac
下面写了四种格式,貌似还有别的
#!/bin/bash for var1 in Mon Tue Wed Thu Fri Sat Sun do echo "Today is $var1 " done for var2 in {1..10..2} do echo "This is $var2" done for var3 in $(seq 1 2 10) do echo "I have a/an $var3" done for var4 in $(ls) do echo "Here is the file $var4" done
#!/bin/bash i=1 while (( i <= 5 )) do echo "i = $i" let "++ i" done
这个select语句到时木有见过,运行的时候可以和用户交互, 运行一下下面的程序就懂了. 注意不加break的话,会死循环下去
#!/bin/bash echo "Which color do you like?" select var1 in red green blue do echo "I like $var1" break done
标签:linux linux shell 编程 shell 脚本
原文地址:http://blog.csdn.net/zhangxb35/article/details/42402333