Shell while循环
2016-06-28 12:02
471 查看
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:
命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。
以下是一个基本的while循环,测试条件是:如果COUNTER小于5,那么返回 true。COUNTER从0开始,每次循环处理时,COUNTER加1。运行上述脚本,返回数字1到5,然后终止。
COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER='expr $COUNTER+1'
echo $COUNTER
done
运行脚本,输出:
while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按<Ctrl-D>结束循环。
echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
echo "Yeah! great film the $FILM"
done
运行脚本,输出类似下面:
while command do Statement(s) to be executed if command is true done
命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。
以下是一个基本的while循环,测试条件是:如果COUNTER小于5,那么返回 true。COUNTER从0开始,每次循环处理时,COUNTER加1。运行上述脚本,返回数字1到5,然后终止。
COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER='expr $COUNTER+1'
echo $COUNTER
done
运行脚本,输出:
1 2 3 4 5
while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按<Ctrl-D>结束循环。
echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
echo "Yeah! great film the $FILM"
done
运行脚本,输出类似下面:
type <CTRL-D> to terminate enter your most liked film: Sound of Music Yeah! great film the Sound of Music
相关文章推荐
- Shell for循环
- Shell case esac语句
- Shell if else语句
- shell printf命令:格式化输出语句
- 使用 PowerShell 自动登录 Azure
- 使用 PowerShell 自动登录 Azure
- 10.Shell操作符
- 使用 PowerShell 自动登录Azure
- ubuntu系统shell 中source: not found错误
- 使用PowerShell .Net获取电脑中的UUID
- shell编程初探
- Managing linux Shell Jobs
- Bash shell 的算术运算有四种方式
- HBase shell 中的十六进制数值表示
- 使用PowerShell .Net获取电脑中的UUID
- shell编程代码备忘
- 用了就会上瘾的几个shell命令行快捷键
- linux----->shell高级编程----grep应用
- shell并发
- [shell实例]——用脚本实现向多台服务器批量复制文件(nmap、scp)