bash 在 shell 脚本中检查命令的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586290/
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-09 22:06:51  来源:igfitidea点击:

Check the output of a command in shell script

bashshell

提问by One Two Three

I'm writing a very simple shell scripts that would looked at the log of all failed tests, and print out all the name of all files in the current directory that are in the log

我正在编写一个非常简单的 shell 脚本,它会查看所有失败测试的日志,并打印出日志中当前目录中所有文件的所有名称

 1  #! /bin/sh

 2  for file in *
 3  do
 4      echo "checking: $file"
 5      if [$(grep $file failed.txt -c) -ne 0]
 6      then
 7          echo "$file FAILED"
 8      fi
 9  done

When I execute it, I get this error:

当我执行它时,我收到此错误:

line 6: [0: command not found

Does anyone have any idea why?

有谁知道为什么?

Thanks!!

谢谢!!

回答by Dunes

[is actually a command in linux (like bash or cat or grep).

[实际上是 linux 中的命令(如 bash 或 cat 或 grep)。

$(grep $file failed.txt -c)is a command substitution which in your case evaluated to 0. Thus the line now reads [0 -ne 0], which is interpreted as run a program called [0with arguments -ne 0].

$(grep $file failed.txt -c)是一个命令替换,在您的情况下评估为 0。因此该行现在读取[0 -ne 0],它被解释为运行一个[0使用参数调用的程序-ne 0]

What you should write instead is [ $(grep $file failed.txt -c) -ne 0 ]. Shell scripts require that there be spaces between the opening and closing square braces. Otherwise you change the command that is executed (the closing ]indicates that there are no more arguments to be read.

你应该写的是[ $(grep $file failed.txt -c) -ne 0 ]. Shell 脚本要求左方括号和右方括号之间有空格。否则,您更改执行的命令(结束]表示没有更多参数可供读取。

So now the command evaluates to [ 0 -ne 0 ]. You can try executing this in your shell to see what happens. [exits with a value of 0if the expression is true and 1if it is false. You can see the exit value by echoing $?(the exit value of the last command to be run).

所以现在命令评估为[ 0 -ne 0 ]. 您可以尝试在 shell 中执行此操作以查看会发生什么。如果表达式为真,如果为假,[则以值退出。您可以通过回显(要运行的最后一个命令的退出值)来查看退出值。01$?

回答by l0b0

Instead of testing the count, you can test the return code of grep:

您可以测试以下返回码,而不是测试计数grep

if grep -q $file failed.txt &>/dev/null

回答by Rony

The script can be

脚本可以是

#!/bin/sh

for file in *; do
    echo "checking: $file"
    grep failed.txt $file && echo "$file FAILED"
done

or, as an one-liner in user shell command history:

或者,作为用户 shell 命令历史中的一行:

for file in *; do { echo "checking: $file" && grep failed.txt $file && echo "$file FAILED"; done

for file in *; do { echo "checking: $file" && grep failed.txt $file && echo "$file FAILED"; done

in man grep

man grep

EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)

退出状态
如果找到选定的行,退出状态为 0,如果未找到,则退出状态为 1。如果发生错误,退出状态为 2。(注意:POSIX 错误处理代码应检查 '2' 或更大的值。)