测试:使用 -z 选项时需要参数

发布于 2024-11-25 12:28:07 字数 349 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-12-02 12:28:07

您没有将 `command` 字符串括在双引号中,因此如果找不到该字符串,该命令将简化为 if [ -z ] 这是一个错误(缺少参数)。 (它扩展为,而不是空参数。)并且它以困难的方式做事。

if grep ">Success<" $OUTFILE

您可能需要 grep 上的 -s 和(如果是 GNU grep-q 选项。

You didn't wrap the `command` string in double quotes, so if the string isn't found, the command reduces to if [ -z ] which is an error (missing parameter). (It expands to nothing, not to an empty parameter.) And it's doing things the hard way.

if grep ">Success<" $OUTFILE

You may want the -s and (if GNU grep) -q options on grep.

£噩梦荏苒 2024-12-02 12:28:07

您应该引用 -z 的参数!

if [ -z "$(grep ">Success<" $OUTFILE)" ]

当然,要测试是否找到 >Success< ,还有一种更简单的方法:

if grep -q ">Success<" $OUTFILE

You should quote the argument to -z!

if [ -z "$(grep ">Success<" $OUTFILE)" ]

Of course, to test whether >Success< is found, there's an even easier way:

if grep -q ">Success<" $OUTFILE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文