在 while 循环中未找到 Bash 脚本命令错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48296831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:46:45  来源:igfitidea点击:

Bash script command not found error in while loop

bashwhile-loopcomparisondeclaretypeset

提问by Süleyman Orhan

I have problem and cant still handle with it. :( Here is my code

我有问题,仍然无法处理。:(这是我的代码

declare -i disk_usage_rate=$(df -h /appdata/SCT_CDR | cut -d '%' -f 1 | awk 'NR==2{print }')

while ["$disk_usage_rate" -gt 80]
do
...
...
done

I get the disk usage rate from df -h command. But in while loop I get the followig error. Btw it is bash script.

我从 df -h 命令获得磁盘使用率。但是在 while 循环中,我收到了 followig 错误。顺便说一句,它是 bash 脚本。

bash: [84: command not found

bash: [84: 找不到命令

I tried so many things but I didnt solve yet.

我尝试了很多事情,但我还没有解决。

回答by vivekyad4v

You are missing space near square brackets used in while loop such as below -

您在 while 循环中使用的方括号附近缺少空格,如下所示 -

declare -i disk_usage_rate=$(df -h /appdata/SCT_CDR | cut -d '%' -f 1 | awk 'NR==2{print }')

while [ "$disk_usage_rate" -gt 80 ]
do
...
...
done

A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13

一点历史:这是因为 '[' 在历史上不是 shell 内置的,而是一个单独的可执行文件,它接收表达式作为参数并返回结果。如果您没有用空格将 '[' 括起来,shell 将在 $PATH 中搜索不同的文件名(而不是找到它)。– Andrew Medico 2009 年 6 月 24 日 1:13

Ref - bash shell script syntax error

参考 - bash shell 脚本语法错误

回答by romaric crailox

I see one error : add a space after '['and before ']'on your while line :

我看到一个错误:在您的 while 行'['前后添加一个空格']'

while [ "$disk_usage_rate" -gt 80 ]

Is it better ?

这个会比较好吗 ?