- 论坛徽章:
- 39
|
有关第一蛋的帖子请看http://bbs.chinaunix.net/thread-4145766-1-1.html
分享之前还是说一下我的环境
我的环境是rhel6.4
GNU Bash-4.1 2009 December 29 BASH(1)
(环境不一样,可能语法不一样,或语法不存在)
有shell问题的 愿意交流的大神 请加六神建的群302706076
有错误的地方请各位毫不留情的指出来
有漏掉的地方请大婶们及时补充
while 循环语句 (一般用在无限循环里面)
先man bash 找出关于while语句的部分
我用翻译工具和自己粗糙的英文水平翻译下
while list; do list; done
until list; do list; done
The while command continuously executes the do list as long as
the last command in list returns an exit status of zero. The
until command is identical to the while command, except that the
test is negated; the do list is executed as long as the last
command in list returns a non-zero exit status. The exit status
of the while and until commands is the exit status of the last
do list command executed, or zero if none was executed.
中文意思如下
当条件为...的时候;做列表;完成
直到列表,列表;完成
只要最后一个命令在list里返回一个为零的退出状态while命令会一直做list
除了测试是否定的,这个until命令与while命令一样;
只要list中最后一个命令能返回非零退出状态do list才会被执行
如果看不懂英文和不理解中文的意思 没关系, 请直接看下面的栗子
语句结构如下
while
条件;
do
动作;
done | until
条件;
do
动作;
done | | 这个语句用得很少 | 语句解释--until这个语法比较特殊,所以不能仅从字面上去解释 | [tr][/tr] 语句解释如下
当
条件为真时
做
动作
语句结束 | 当
条件为假时
做
动作
语句结束 |
我们直接上栗子
read num
| while
[ $num -eq 10 ]
do
echo "$?"
echo "刷屏中..."
done | untile
[ $num -eq 10 ]
do
echo "$?"
echo "刷屏中..."
done |
这里我先不说结果,请大家先猜猜结果会怎样
OK
我来解释下这2个语句最终的结果
read num 相信大家很清楚,我上一贴也说过了
不清楚的我再解释下:
从键盘读取按键操作,把按键操作记录下后 定义成一个变量,变量名为num
当输入的字符=10
做
显示上一个命令的退出状态值
显示刷屏中
完毕 | 当输入的字符=10
不做
显示上一个命令的退出状态值
显示刷屏中
完毕 | 大家是否很奇怪语句结构一样 执行结果却不一样?
我特地的加上了1行echo $?
就是使大家明白
准确的来说应该是这样的
当输入的字符与10比较
比较后系统返回一个退出值,当退出值=0的时候
做
显示上一个命令的退出状态值
显示刷屏中
完毕 | 当输入的字符与10比较
比较后系统返回一个退出值,当退出值=1的时候
做
显示上一个命令的退出状态值
显示刷屏中
完毕 |
因此我们一般由系统返回的退出值来判定该条件是否为真
返回值=0时,条件为真
返回值=1时,条件为假
因此while和until判定条件是相反的
while
条件为真时
做
一直无限循环动作 | until
条件为假时
做
一直无限循环动作
|
因此我们一般用的比较多的就是while。
until语句基本上用得很少
OK,while语句已经讲完毕
for语句 (一般都用在逐个有规律的循环里面,也就是大家所说的遍历)
我们还是先man一下bash 查找一下有关for语句的信息
for name [ [ in [ word ... ] ] ; ] do list ; done
The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in
turn, and list is executed each time. If the in word is omit-
ted, the for command executes list once for each positional
parameter that is set (see PARAMETERS below). The return status
is the exit status of the last command that executes. If the
expansion of the items following in results in an empty list, no
commands are executed, and the return status is 0.
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to
the rules described below under ARITHMETIC EVALUATION. The
arithmetic expression expr2 is then evaluated repeatedly until
it evaluates to zero. Each time expr2 evaluates to a non-zero
value, list is executed and the arithmetic expression expr3 is
evaluated. If any expression is omitted, it behaves as if it
evaluates to 1. The return value is the exit status of the last
command in list that is executed, or false if any of the expres-
sions is invalid.
这里列出了有关for语句的详细信息
我用翻译工具和自己粗糙的英文水平翻译下
for name [ [ in [ word ... ] ] ; ] do list ; done
按照单词列表被扩展,产生一个项目列表。变量名设置为这个列表中依次处理每个元素,并列出每次都被执行。如果省略的话,则对命令所设置的每个位置参数执行一次表(见下面的参数) 。返回状态是执行的最后一个命令的退出状态。如果在导致一个空列表下的项目的扩展,没有执行命令,并返回状态是0 。
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
首先,算术表达式表达式1是根据下算术求下面描述的规则进行评价。算术表达式表达式2 ,然后反复计算,直到它的计算结果为零。每次表达式2的计算结果为非零值时,列表中被执行,该算术表达式表达式3进行评价。如果省略任何表达式,它的行为就好像它的计算结果为1 ,返回值是列表中的最后一个命令被执行了,还是假的,如果任何一个表达式是无效的退出状态。
没明白啥意思? 其实我也没明白...
那我就上栗子吧
a="1 2 3 4 5"
for f in a ;
do
echo $f ;
done | for f in 1 2 3 4 5;
do
echo $f;
done | for ((a=1;a<100;a++));
do
echo $a;
done |
定义一个变量a 使它的值为1 2 3 4 5
赋予一个变量f 使他的值依次为变量a里的每个值
做
显示变量f的值
完毕 | 定义一个变量f,赋予他值依次为1 2 3 4 5
做
显示变量f的值
完毕 | 定义一个变量a,初始值为1,变量a小于100,变量a依次递增
做
显示变量a的值
完毕 |
格式就是
for 变量 in 列表
do
语句
done
| for ((变量=初始值; 条件判断; 变量变化))
do
语句
done
|
1和2都满足第一种格式 3就满足第二中格式
for 变量 in 列表----------------> 变量会依次取列表中的值
在上述栗子中for f in 1 2 3 4 5,通俗一点就是说 f=1 f=2 f=3 f=4 f=5 依次取列表中的值
而a="1 2 3 4 5"for f in a
这一句也是一样的道理 变量f依次会取变量a的值
for ((变量=初始值; 条件判断; 变量变化)) ----------> 这种句型一定一定不能搞错顺序,也一定一定不能漏(( ))
----------> 这个句型用的比较多 比较广泛,一直一直要牢记句型
for (( expr1 ; expr2 ; expr3 )) ; do list ; done 由于这种句型应用的比较多 我在下面多来些栗子
a=0
for((i=1;i<=100;i++))
do
a=$((i+a))
done
echo $a | echo a b c d e | awk '{for(i=1;i<NF;i++) print $i}' | 这是一个比较经典的栗子,有的运维笔试题会考到
这里就是计算从1+2+3+...+100的值 | 这个命令就不解释了,只有会awk的人才知道是什么意思(其实我也不知道什么意思^_^) |
好了
for语句基本上就讲到这里.....
|
|