Shell脚本While循环示例
时间:2020-01-09 10:43:40 来源:igfitidea点击:
每个while循环均由一组命令和一个条件组成。
bash while循环的常规语法如下:
while [ condition ] do command1 command2 commandN done
- 评估条件,如果条件为真,则执行命令1,2N。
- 重复此过程,直到条件变为假。
- 条件可以是整数($i <5),文件测试(-e/tmp/lock)或者字符串($ans!="")
ksh while循环语法:
while [[ condition ]] ; do command1 command1 commandN done
csh while循环语法:
while ( condition ) commands end
BASH while循环示例
#!/bin/bash c=1 while [ $c -le 5 ] do echo "Welcone $c times" (( c++ )) done
KSH while循环示例
#!/bin/ksh c=1 while [[ $c -le 5 ]]; do echo "Welcome $c times" (( c++ )) done
CSH while循环示例
#!/bin/csh c=1 while ( $c <= 5 ) echo "Welcome $c times" @ c = $c + 1 end
另一个例子:
#!/bin/csh set yname="foo" while ( $yname != "" ) echo -n "Enter your name : " set yname = $< if ( $yname != "" ) then echo "Hi, $yname" endif end