测试:使用 -z 选项时需要参数
我有一个 unix 脚本,偶尔会出错并显示消息“测试:需要参数”。下面这行代码是脚本中唯一的 if 语句
if [ -z `grep ">Success<" $OUTFILE` ]
$OUTFILE 是脚本开始运行时创建的文件。该脚本调用将输出写入 OUTFILE 的 Web 服务。如果输出文件中包含“Success”,则表示 Web 服务已成功完成。
该脚本每 10 分钟调用一次,上面的逻辑在大多数情况下都工作得很好。但有时,脚本会因测试参数预期错误而出错,我无法找出此错误的原因。还有其他人遇到过类似的问题吗?如果有人可以提供有关此问题的指示,那就太好了。
谢谢。
I have a unix script that occasionally errors out with the message "test: argument expected". The following line of code is the only if statement in the script
if [ -z `grep ">Success<" $OUTFILE` ]
The $OUTFILE is a file created when the script starts to run. The script calls a web service that writes the output to the OUTFILE. If the outfile has "Success" in it, then it means that the web service completed successfully.
This script is called every 10 minutes and the logic above works perfectly fine for most of the cases. But on occasionally, the script errors out with the test argument expected error and I am unable to figure out the reason for this error. Has anyone else faced a similar problem? It would be great if someone can provide pointers to this issue.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有将
`command`
字符串括在双引号中,因此如果找不到该字符串,该命令将简化为if [ -z ]
这是一个错误(缺少参数)。 (它扩展为无,而不是空参数。)并且它以困难的方式做事。您可能需要
grep
上的-s
和(如果是 GNUgrep
)-q
选项。You didn't wrap the
`command`
string in double quotes, so if the string isn't found, the command reduces toif [ -z ]
which is an error (missing parameter). (It expands to nothing, not to an empty parameter.) And it's doing things the hard way.You may want the
-s
and (if GNUgrep
)-q
options ongrep
.您应该引用
-z
的参数!当然,要测试是否找到
>Success<
,还有一种更简单的方法:You should quote the argument to
-z
!Of course, to test whether
>Success<
is found, there's an even easier way: