shell循环,判断介绍,以及实例

shell的循环主要有3种,for,while,until

shell的分支判断主要有2种,if,case

 

一,for循环

[html] view plain copy print ?
  1. #!/bin/bash
  2.  
  3. for file in $(ls /tmp/test/mytest |grep sh) //for in格式是shell for的基本格式,根js的for in类似
  4. do //循环开始你就把它当成{
  5. echo $file
  6. done //循环结束你就把它当成}
  7.  
  8. for ((i=0;i<10;i++)) //注意是双小括号,由于受其他语言的影响,很容易搞错
  9. do
  10. echo -n $i
  11. done
  12.  
  13. echo \ //输出换行
  14.  
  15. for i in 0 1 2 3 4 5 6 7 8 9
  16. do
  17. echo -n $i
  18. done
  19.  
  20. echo \
  21.  
  22. for i in "0 1 2 3 4 5 6 7 8 9" //这个根上面是有区别的,这个循环只循环了一次,双引号里面只是一个变量
  23. do
  24. echo -n $i
  25. done
  26.  
  27. exit 0
#!/bin/bash for file in $(ls /tmp/test/mytest |grep sh) //for in格式是shell for的基本格式,根js的for in类似 do //循环开始你就把它当成{ echo $file done //循环结束你就把它当成} for ((i=0;i<10;i++)) //注意是双小括号,由于受其他语言的影响,很容易搞错 do echo -n $i done echo \ //输出换行 for i in 0 1 2 3 4 5 6 7 8 9 do echo -n $i done echo \ for i in "0 1 2 3 4 5 6 7 8 9" //这个根上面是有区别的,这个循环只循环了一次,双引号里面只是一个变量 do echo -n $i done exit 0


 

二,while循环

[html] view plain copy print ?
  1. #!/bin/bash
  2. i=0
  3. while ((i<10))
  4. do
  5. echo $i
  6. ((i += 1))
  7. done
  8.  
  9. i=0
  10. while [ $i -lt 10 ] //注意括号内侧二边的空格
  11. do
  12. echo $i
  13. let "i+=1" //加1
  14. done
  15. exit 0
#!/bin/bash i=0 while ((i<10)) do echo $i ((i += 1)) done i=0 while [ $i -lt 10 ] //注意括号内侧二边的空格 do echo $i let "i+=1" //加1 done exit 0


三,until循环

[html] view plain copy print ?
  1. #!/bin/bash
  2.  
  3. END_CONDITION=end
  4. until [ "$var1" = "$END_CONDITION" ] //读取的变量根设定的变量相等时退出循环,不然永远循环
  5. do
  6. echo "Input variable #1 "
  7. echo "($END_CONDITION to exit)"
  8. read var1
  9. echo "variable #1 = $var1"
  10. echo
  11. done
  12. exit 0
#!/bin/bash END_CONDITION=end until [ "$var1" = "$END_CONDITION" ] //读取的变量根设定的变量相等时退出循环,不然永远循环 do echo "Input variable #1 " echo "($END_CONDITION to exit)" read var1 echo "variable #1 = $var1" echo done exit 0

四,if语句

[html] view plain copy print ?
  1. #!/bin/bash
  2.  
  3. echo "Input a number #1 "
  4. read num
  5. echo "variable #1 = $num"
  6.  
  7. if [ $num -lt 60 ] //注意lt前面的-,很容易忘的
  8. then
  9. echo "you are not pass"
  10. elif [ $num -lt 70 ] && [ $num -ge 60 ] //多个条件的判断
  11. then
  12. echo "pass"
  13. elif [[ $num -lt 85 && $num -ge 70 ]] //如果放在一起,要注意是双方括号,不要写成[ $num -lt 85 && $num -ge 70 ]
  14. then
  15. echo "good"
  16. elif (( $num <= 100 )) && (( $num >= 85 )) //对于有语言基础的人来说,这种写法让人觉得很舒服,不要忘了是双小括号
  17. then
  18. echo "very good"
  19. else
  20. echo "num is wrong"
  21. fi //if要有结束标签的,根XML很像,不闭合,就报错
  22.  
  23. exit 0
#!/bin/bash echo "Input a number #1 " read num echo "variable #1 = $num" if [ $num -lt 60 ] //注意lt前面的-,很容易忘的 then echo "you are not pass" elif [ $num -lt 70 ] && [ $num -ge 60 ] //多个条件的判断 then echo "pass" elif [[ $num -lt 85 && $num -ge 70 ]] //如果放在一起,要注意是双方括号,不要写成[ $num -lt 85 && $num -ge 70 ] then echo "good" elif (( $num <= 100 )) && (( $num >= 85 )) //对于有语言基础的人来说,这种写法让人觉得很舒服,不要忘了是双小括号 then echo "very good" else echo "num is wrong" fi //if要有结束标签的,根XML很像,不闭合,就报错 exit 0


 

五,case语句

[html] view plain copy print ?
  1. #!/bin/sh
  2. case $1 in
  3. start)
  4. echo "start ok"
  5. ;; //注意一点,要注意是双分号
  6. stop)
  7. echo "stop ok"
  8. ;;
  9. restart)
  10. echo "restart ok"
  11. ;;
  12. *)
  13. echo "no param"
  14. ;;
  15. esac //注意闭合标签
  16. exit 0
  17.  
  18. [root@krlcgcms01 forif]# sh c1.sh stop
  19. stop ok

你可能感兴趣的:(shell循环,判断介绍,以及实例)