加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 为什么shell脚本中的空循环无效?

发布时间:2020-12-15 22:15:36 所属栏目:安全 来源:网络整理
导读:我想让我的 shell脚本无限地等待,并认为下面的代码会这样做. #!/bin/bashwhile truedodone 但是,上面的脚本报告语法错误. ./Infinite_Loop.sh: line 4: syntax error near unexpected token `done’ ./Infinite_Loop.sh: line 4: `done’ 与编程语言不同,为
我想让我的 shell脚本无限地等待,并认为下面的代码会这样做.

#!/bin/bash
while true
do
done

但是,上面的脚本报告语法错误.

./Infinite_Loop.sh: line 4: syntax error near unexpected token `done’

./Infinite_Loop.sh: line 4: `done’

与编程语言不同,为什么shell脚本期望循环中至少有一个语句?

解决方法

I wanted to make my shell script wait infinitely

如果您的系统支持它,请使用:

sleep infinity

如果您的系统不支持它,请使用间隔较大的睡眠:

while :; do sleep 86400; done

注意:

>使用while:代替while,可以/将删除不必要的fork,具体取决于实现的真实性(内置于shell中,或作为独立应用程序).

您正在尝试实现繁忙的循环,不要这样做.

繁忙的循环将:

>使用100%CPU没有用处
>防止其他任务获得CPU时间
>降低整个系统的感知性能
>使用超出必要的功率,尤其是支持dynamic frequency scaling的系统

Why is an empty loop invalid in shell script?

因为它是… bash中while循环的格式如下:

while list-1; do list-2; done

如果您不提供list-2,那么您没有正确格式化的while循环.

正如其他人所指出的那样,使用noop(:)或其他任何东西来满足list-2.

:记录如下:

: [arguments]
    No effect; the command does nothing beyond expanding arguments and performing any
    specified redirections.  A zero exit code is returned.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读