分享

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

 戴维图书馆 2018-07-06
shell编程中while true 中的true是bash的内置命令还是/bin/true呢?

先保证你的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),用于构造无穷循环。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多