shell编程 循环结构
2011-08-07 20:01
190 查看
=====================================while语句========================================
while语句格式
while 和 if 的条件表达式完全相同,也是[ ] 或commad或test
最基本的i++ 条件型循环
命令型while 循环
while command 命令返回值0(成功执行),循环继续
While echo … 此命令没有失败的可能,所以必须有break,return,exit之类的指令
while 关键字
break———— 用来跳出循环
continue—— 用来不执行余下的部分,直接跳到下一个循环
===========================================FOR语句===================================
for语句格式
i++,n=n+1 必须用双层括号 $(($num+1)) ,单层括号$($num+1)不管用
(( ))与[ ]作用完全相同
再证明(( ))与[ ]完全相同--------if (( ))
$foo=$(($foo+1)) # 运行的时候这个地方报错
给变量赋值,左边的变量不需要加 $ 符号,
foo=$(($foo+1))
赋值=,read,export都不需要加变量$
while语句格式
while 表达式 do command command done |
While 表达式 | If 表达式 |
表达式值为0,则循环继续 | 表达式值为0,then |
表达式值为非0,则循环停止 | 表达式值为非0,else |
i=1 while [ $i -lt 10 ] do sed -n "${i}p" 111.txt i=$(($i+1)) 必须双层括号 done |
while command 命令返回值0(成功执行),循环继续
pause函数,输入任何值继续,输入q退出程序 pause() { while echo "Press <return> to proceed or type q to quit:" do read cmd case $cmd in [qQ]) exit 1;; exit直接退到底,退出shell script "") break;; break跳出循环 *) continue;; continue跳到循环底,重新开始新循环循环 esac done |
while 关键字
break———— 用来跳出循环
continue—— 用来不执行余下的部分,直接跳到下一个循环
===========================================FOR语句===================================
for语句格式
for 表达式 do command command done |
[root@mac-home home]# vi test.sh : echo "input num:" read num echo "input is $num" num=$($num+1) echo "new num is $num" [root@mac-home home]# sh test.sh input num: 3 input is 3 test.sh: line 6: 3+1: command not found new num is |
[root@mac-home home]# vi test.sh : echo "input num:" read num echo "input is $num" num=$(($num+1)) echo "new num is $num" [root@mac-home home]# sh test.sh input num: 3 input is 3 new num is 4 |
echo input: read i i=$(($i+1)) echo $i | echo input: read i i=$[$i+1] echo $i |
[macg@localhost ~]$ sh ttt.sh input: 6 7 | [macg@localhost ~]$ sh ttt.sh input: 6 7 |
if (( $# != 3 )); then echo "usage: $0 host user passwd" exit 1 fi | if [ $# != 3 ]; then echo "usage: $0 host user passwd" exit 1 fi |
[macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd | [macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd |
给变量赋值,左边的变量不需要加 $ 符号,
foo=$(($foo+1))
赋值=,read,export都不需要加变量$
相关文章推荐
- 脚本乐园 shell编程循环结构
- Shell编程---分支和循环结构
- shell编程三种循环结构
- 15、C#里面的for循环结构的使用
- java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
- 循环结构程序设计代码示例
- 循环结构(for循环和while循环的区别)
- java循环结构
- 由具体到抽象设计循环结构
- PHP函数continue在循环结构中的用法
- 数据结构基础-队列(循环队列、链式队列)以及STL中queue的使用
- C++编程实例-多重循环结构
- C语言实验——判断素数(循环结构)
- 线性结构之循环队列实现
- 三种循环结构的使用及区别
- iOS开发-简单的循环结构分析
- java基础第三题 循环结构
- OC学习笔记六 循环结构
- 数据结构之循环队列
- 数据结构之顺序循环队列