shell编程中while ture和while :为什么等同?

作者&投稿:叶苗 (若有异议请与网页底部的电邮联系)
shell编程中while ture和while :为什么等同~

先保证你的linux命令行窗口是bash环境,我来给你细讲。
1)运行bash,确保是bash环境

2)下面看看相关命令的帮助。
运行 help true,你会看到:
$ help true
true: true
Return a successful result.

Exit Status:
Always succeeds.

true是bash的内置命令,总返回成功(退出状态总是0)。

3)运行 help : ,你会看到:
$ help :
:: :
Null command.

No effect; the command does nothing.

Exit Status:
Always succeeds.

冒号 : 是bash的内置命令,也叫空命令,也叫null命令,总返回成功(退出状态总是0)。

4)运行 help while ,你会看到:
$ help while
while: while COMMANDS; do COMMANDS; done
Execute commands as long as a test succeeds.

Expand and execute COMMANDS as long as the final command in the
`while' COMMANDS has an exit status of zero.

Exit Status:
Returns the status of the last command executed.

while 是bash的内置命令,while后面的命令成功(退出状态是0)时,do 和done之间的命令就会执行,即,do 和done之间的东西就会循环。
所以说 while true与while :确实等同。类似于C语言中的 while (1),用于构造无穷循环。

谢谢, 供参考。

你在while[xxx]之前将while中括号内的东西(xxx)打印一下看就明白了:
echo xxx
可能原因是其中有参数带有特殊字符,如”[”、“]“等,你可以这样引用参数:
while [ ${var} = ${var2} ]
do
...
如果还可能有空串的话,这样万无一失:
while [ ”X${var}“ = "X${var2}" ]
do
...
X是随便拼上去的一个字符,目的就是保证你那个串不会是空的

先保证你的linux命令行窗口是bash环境,我来给你细讲。

1)运行bash,确保是bash环境

2)下面看看相关命令的帮助。

运行  help true,你会看到:
$ help true
true: true
Return a successful result.

Exit Status:
Always succeeds.

true是bash的内置命令,总返回成功(退出状态总是0)。

3)运行  help :  ,你会看到:
$ help :
:: :
Null command.

No effect; the command does nothing.

Exit Status:
Always succeeds.

冒号 : 是bash的内置命令,也叫空命令,也叫null命令,总返回成功(退出状态总是0)。

4)运行  help while ,你会看到:
$ help while
while: while COMMANDS; do COMMANDS; done
Execute commands as long as a test succeeds.

Expand and execute COMMANDS as long as the final command in the
`while' COMMANDS has an exit status of zero.

Exit Status:
Returns the status of the last command executed.

while 是bash的内置命令,while后面的命令成功(退出状态是0)时,do 和done之间的命令就会执行,即,do 和done之间的东西就会循环。
所以说 while true与while :确实等同。类似于C语言中的 while (1),用于构造无穷循环。




雅思听力中的连读规则
My name is&helli p;[my nay•miz]because I’ve.[b'k'zäiv]pick up on the American intonation…[pi•kə pan the(y) əmer'kə ninətənashən]不只是句子中,读字母缩写也可以连读:LA[eh•Lay]读数字时也可以连读...

魔兽争霸3中各英雄语音的中英文对照
- Has hell frozen over yet? 地狱还没有冻住吗?- I am the one horseman of the Apocalypse. ...- Whhaaaaaattt? 什么???- Me busy, leave me alone. 我很忙,别烦我- No time for play.

海南藏族自治州19385618716: shell编程中while ture和while :为什么等同 -
喻沫洛吉: 先保证你的linux命令行窗口是bash环境,我来给你细讲. 1)运行bash,确保是bash环境2)下面看看相关命令的帮助. 运行 help true,你会看到:$ help true true: trueReturn a successful result. Exit Status:Always succeeds.true是bash...

海南藏族自治州19385618716: linux shell脚本while编程的问题 -
喻沫洛吉: 是你的程序写的有问题,while 后面的[]里面,要加上前后空格.要像这样:#!/bin/bash i=5 while [ $i -gt 0 ]; do echo "$i" i=$[ i - 1 ] done

海南藏族自治州19385618716: linux中用shell编程while循环的问题求助 -
喻沫洛吉: 这个地方的问题,对于一个初学者来说是怎么想也想不到的.问题就是: while [ $1 -ge 10 ] 在while与[]之间你没有加空格. 还有一点,如果你给出的参数值大于10的话,那么这个程序将会是一个死循环!! -le 和 -ge 要分清楚.

海南藏族自治州19385618716: 菜鸟问一个linux shell 程序中的while循环 -
喻沫洛吉: 重定向叠加上去符号是 >> 而你只是 > ,当然被覆盖成只有1行啦.也就是 grep $LINE a.txt >> a2.txt

海南藏族自治州19385618716: 用shell的for语句结构和while语句结构编写程序 -
喻沫洛吉: 1、以下是shell脚本代码:#!/bin/bashfor ((j=1;j<10;j++))do for ((i=1;i<j;i++)) do echo -en "\t" done for ((k=j;k<10;k++)) do echo -en "$((k*j))\t" done echo ""done2、执行结果见下图

海南藏族自治州19385618716: 求解答关于linux中shell的while脚本用法 -
喻沫洛吉: #!/bin/shnum=1while [ $num -le 10 ]do SUM =' expr $num \* $num ' #SUM等号之间不能有空格 ,等号后面语句单引号换成反引号 echo $SUMnum =" expr $num +1 " #这一行双引号换反引号 ,1与+号之间有空格. done shell的语法检查是比较严格的,尤其是空格以后多注意

海南藏族自治州19385618716: 求助linux shell下编程 用while语句一分钟执行一次命令 -
喻沫洛吉: #!/bin/bash while [ 1 ] do echo "haha"; sleep 60 done;

海南藏族自治州19385618716: shell编程:求前10个偶数(2 - 20)之和 (用while语句写) -
喻沫洛吉: #!/bin/bash sum=0; i=2; while (($isum=$((sum + i)); ((i=i+2)); done; echo $sum;

海南藏族自治州19385618716: shell编程用while循环怎么把文件信息逐行显示出来 -
喻沫洛吉: #!/bin/bash while read line;do echo $line done

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网