重定向输入一段时间循环
我们可以指定一个文件来读取输入,而不是来自键盘或者标准输入的输入。
考虑以下示例:
$ cat internalredir.ksh #!/bin/ksh # Script name: internalredir.ksh # set the Internal Field Separator to a colon IFS=: while read name number do print "The phone number for $name is $number" done < phonelist
在上面的例子中,重定向是在 while 块的末尾执行的:
done < phonelist
通过在标准输入上重定向,文件只打开一次。
每次循环时, read 从文件中返回下一行。
行被读入循环,而有行要从文件中读入。
到达文件末尾后,真实状态不再存在,循环终止。
循环终止后,执行 done 语句之后的语句。
注意:输入重定向发生在包含单词 done 的行上。
循环是一个完整的命令结构,任何输入重定向都必须在完整的命令结构之后。
$ cat phonelist Claude Rains:214-555-5107 Agnes Moorehead:710-555-6538 Rosalind Russel:710-555-0482 Loretta Young:409-555-9327 James Mason:212-555-2189
$ ./internalredir.ksh The phone number for Claude Rains is 214-555-5107 The phone number for Agnes Moorehead is 710-555-6538 The phone number for Rosalind Russel is 710-555-0482 The phone number for Loretta Young is 409-555-9327 The phone number for James Mason is 212-555-2189
while 循环允许我们在命令成功执行时重复执行一组语句。
while 循环的语法是:
while control_command do statement1 ... statementN done
其中:
control_command 可以是任何以成功或者失败状态退出的命令。
while 循环体中的语句可以是任何实用程序命令、用户程序、shell 脚本或者 shell 语句。
当执行 while 语句时,将评估 control_command。
如果 control_command 成功,则执行 do 和 done 之间的所有语句,然后再次执行控制命令。
只要 control_command 成功,循环体就会继续执行。
一旦 control_command 失败,则执行 done 语句之后的语句。
do 语句是一个单独的语句,必须单独出现在一行中(而不是出现在包含 while command_control 语句的行中),除非它前面有一个分号。
done 语句也是如此。
如果它存在于循环中最后一个语句行的末尾,则它必须单独存在或者以分号开头。
使用 while 循环的示例
在下面的例子中,变量 num 被初始化为 1,并进入了一个 while 循环。
这个 while 循环使用 num 而不是 $num 因为 ((...)) 命令自动进行变量扩展。
使用 num 强制整数算术,而使用 $num 对字符串执行算术。
整数运算速度更快。
在while循环中,num的当前值被打印到stdout。
变量 num 递增并再次检查 while 语句中的条件。
如果变量 num 的值在 while 循环中没有改变,则程序将处于无限循环(即永不结束的循环)中。
$ cat while.ksh #!/bin/ksh # Script name: while.ksh num=1 while (( num < 6 )) do print "The value of num is: $num" (( num = num + 1 )) # let num=num+1 done print "Done."
$ ./while.ksh Value of num is: 1 Value of num is: 2 Value of num is: 3 Value of num is: 4 Value of num is: 5 Done.
shell脚本键盘输入
我们可以将 read 语句放在 while 条件中。
每次用户从键盘输入内容时,都会执行一个循环。
当用户按下 ControlD 时,这会告诉 read 语句输入完成,条件变为 false,执行跳转到 done 语句之后。
$ cat readinput.ksh #!/bin/ksh # Script name: readinput.ksh print -n "Enter a string: " while read var do print "Keyboard input is: $var" print -n "\nEnter a string: " done print "End of input."
$ ./readinput.ksh Enter a string: OK Keyboard input is: OK Enter a string: This is fun Keyboard input is: This is fun Enter a string: I’m finished. Keyboard input is: I’m finished. Enter a string: ^d End of input.
while 循环语法
以下示例都是 while 语法的有效形式。
当 $var 的内容等于字符串 string_value 时,循环继续。
while [ "$var" = "string_value" ] while [[ "$var" == "string_value" ]]
在以下示例中,当 $num 的值小于或者等于 10 时,循环继续。
while [ $num -le 10 ] while (( num <= 10 ))
就像在 if 语句中一样,while 循环的控制命令通常是 ((...)) 或者 ... 或者 [ ... ] 命令。
通常,测试中的变量会更改 while 循环中的值,从而使循环最终终止。
以下两个示例说明如何更改循环体中的数值以导致 while 循环终止。
$ cat whiletest.sh #!/bin/sh # Script name: whiletest.sh num=5 while [ $num -le 10 ] do echo $num num=‘expr $num + 1‘ done
$ cat whiletest.ksh #!/bin/ksh # Script name: whiletest.ksh num=5 while (( num <= 10 )) do echo $num (( num = num + 1 )) # let num=num+1 done
shell 中有两个命令总是返回相同的退出状态。
这些命令是真假。
true 命令总是导致测试成功(零错误),而 false 命令总是导致测试失败(有一定数量的错误)。
以下示例导致无限循环。
while true
要通过强制条件为假来调试脚本,请使用以下命令:
while false
注意:null 语句 (:) 的计算结果始终为 true;因此,语句 while : 和 while ((1)) 是可以互换的。
使用评估数值表达式的 Korn shell 的 ((...)) 语法,可以在 while 测试中出现单个数值变量或者值。
在这种情况下,如果数值表达式的值不为零,则测试成功并执行循环。
如果数值表达式的值为 0,则测试失败,并执行循环后面的代码。
$ cat whiletest2.ksh #!/bin/ksh # Script name: whiletest2.ksh num=5 while (( num )) do echo $num let num=num-1 # let "num = num - 1" done
语句:while (( num )) 计算为 num 的逻辑否定。
因此,只要 num 的值不为 0,程序就会一直处于循环中。