bash shell 脚本中的非法数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25088858/
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
Illegal number in shell script
提问by whla
I am new to writing scripts and am trying to start out with a simple one. I am stumped as to why I am receiving the error: [: 13: Illegal number: count
from the code below. Line 13 is the last fi
.
我是编写脚本的新手,正在尝试从一个简单的脚本开始。我很困惑为什么我会收到错误:[: 13: Illegal number: count
来自下面的代码。第 13 行是最后一个fi
。
count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l
if [ count -ge 50 ]
then
if [ count -lt 100 ]
then
exit 1
fi
if [ count -ge 100 ]
then
exit 2
fi
exit 0
fi
Also is there anyway to do compound if statements like if(count >= 50 && count < 100)
?
还有无论如何要做复合 if 语句if(count >= 50 && count < 100)
吗?
采纳答案by Jonathan Leffler
The line:
线路:
count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/zumigo/zlsapp.log | wc -l
probably does not do what you think it does (or you've not accurately copied and pasted your actual code into the question). If run in the middle of 2014-08-01, it runs the command "2014-08-01 12:00" with the log file as an argument and the environment variable count
set to the value grep
, and pipes the output from the probably non-existent command to wc -l
.
可能没有按照您的想法去做(或者您没有准确地将实际代码复制并粘贴到问题中)。如果在 2014-08-01 中间运行,它会运行命令“2014-08-01 12:00”,以日志文件为参数,将环境变量count
设置为 value grep
,并将输出从可能非- 存在的命令wc -l
。
When you subsequently go to test $count
in the test statements, it is an empty string, which doesn't convert properly to a number.
当您随后$count
在测试语句中进行测试时,它是一个空字符串,无法正确转换为数字。
You probably meant:
你可能的意思是:
count=$(grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/zumigo/zlsapp.log | wc -l)
This captures the output of running grep
on the log file and piping the output to wc -l
.
这会捕获在grep
日志文件上运行的输出并将输出通过管道传输到wc -l
.
If you run your script with bash -x
or equivalent (the -x
option usually shows the execution of the script), you should see this.
如果你运行你的脚本bash -x
或等价物(该-x
选项通常显示脚本的执行),你应该看到这个。
回答by David C. Rankin
Two reasons. (1)in bash variables are untyped (could be int, could be char). In order to remove ambiguity, you can specify the type with:
两个原因。(1)在 bash 中的变量是无类型的(可以是 int,也可以是 char)。为了消除歧义,您可以使用以下命令指定类型:
declare -i count
To tell bash it should be an int. (2)you need to dereference your variables with $
to get the number back. I.E.
告诉 bash 它应该是一个整数。(2)您需要取消引用您的变量$
以取回数字。IE
[ $count -lt 100 ]
(it is also good practice to quote your variables - not required, but good practice: [ "$count" -lt 100 ]
. Drop a comment if you still have trouble.
(引用您的变量也是一种很好的做法 - 不是必需的,但很好的做法:[ "$count" -lt 100 ]
如果您仍然遇到问题,请发表评论。
回答by Keith Thompson
The problem is that count
does not refer to the variable count
; it's simply the string "count"
.
问题是count
没有引用变量count
;它只是字符串"count"
。
Change:
改变:
if [ count -ge 50 ]
to
到
if [ $count -ge 50 ]
and make the corresponding change elsewhere (but not in the initial assignment).
并在其他地方进行相应的更改(但不在初始分配中)。
You should also use double quotes:
您还应该使用双引号:
if [ "$count" -ge 50 ]
Also is there anyway to do compound if statements like if(count >= 50 && count < 100)?
还有无论如何要执行复合 if 语句,例如 if(count >= 50 && count < 100)?
Yes:
是的:
if [ "$count" -ge 50 -a "$count" -lt 100 ]
if [ "$count" -ge 50 -a "$count" -lt 100 ]
is likely to work, but the -a
(logical and) operator is marked as obsolescent by POSIX. Instead write
可能有效,但-a
(逻辑与)运算符被POSIX 标记为过时。而是写
if [ "$count" -ge 50 ] && [ "$count" -lt 100 ]
If you're using bash, info bash
and search for the "test" command ([
is an alias for test
).
如果您使用 bash,info bash
并搜索“test”命令([
是 的别名test
)。
And if you're using bash, you should consider using [[ ... ]]
rather than [ ... ]
-- or you can use (( ... ))
for arithmetic expressions. See the bash documentationfor more information (follow the iink or type info bash
).
如果您使用 bash,您应该考虑使用[[ ... ]]
而不是[ ... ]
-- 或者您可以使用(( ... ))
算术表达式。有关更多信息,请参阅bash 文档(按照 iink 或 type info bash
)。
In addition to the missing $
signs, the first line of your script:
除了缺少的$
符号,脚本的第一行:
count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l
doesn't set the count
variable, since you're not capturing the output of the grep ... | wc -l
command. To do so:
不设置count
变量,因为您没有捕获grep ... | wc -l
命令的输出。这样做:
count=$(grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l)
(Yes, $(...)
can be nested.)
(是的,$(...)
可以嵌套。)