如果[$?-eq 0)对于shell脚本意味着什么?

时间:2022-04-20 21:39:26

There is this line in a shell script i have seen:

在我看到的shell脚本中有这一行:

grep -e ERROR ${LOG_DIR_PATH}/${LOG_NAME}  > /dev/null
if [ $? -eq 0 ] 

3 个解决方案

#1


32  

It's checking the return value ($?) of grep. In this case it's comparing it to 0 (success).

它检查grep的返回值($?)。在本例中,它将其与0(成功)进行比较。

Usually when you see something like this (checking the return value of grep) it's checking to see whether the particular string was detected. Although the redirect to /dev/null isn't necessary, the same thing can be accomplished using -q.

通常,当您看到这样的东西(检查grep的返回值)时,它会检查是否检测到特定的字符串。尽管重定向到/dev/null并不是必需的,但是使用-q可以完成同样的事情。

#2


26  

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded.

$ ?是最近执行的命令的退出状态;按照惯例,0表示成功,其他任何表示失败。这一行正在测试grep命令是否成功。

The grep manpage states:

grep从状态:

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”或更大。)

So in this case it's checking whether any ERROR lines were found.

在本例中,它检查是否找到了错误行。

#3


2  

It is an extremely overused way to check for the success/failure of a command. Typically, the code snippet you give would be refactored as:

它是检查命令的成功/失败的一种极其过度使用的方法。通常,您给出的代码片段将被重构为:

if grep -e ERROR ${LOG_DIR_PATH}/${LOG_NAME} > /dev/null; then
   ...
fi

(Although you can use 'grep -q' in some instances instead of redirecting to /dev/null, doing so is not portable. Many implementations of grep do not support the -q option, so your script to fail if you use it.)

(尽管您可以在某些实例中使用“grep -q”,而不是重定向到/dev/null,这样做是不可移植的。grep的许多实现不支持-q选项,因此如果您使用它,您的脚本就会失败。

#1


32  

It's checking the return value ($?) of grep. In this case it's comparing it to 0 (success).

它检查grep的返回值($?)。在本例中,它将其与0(成功)进行比较。

Usually when you see something like this (checking the return value of grep) it's checking to see whether the particular string was detected. Although the redirect to /dev/null isn't necessary, the same thing can be accomplished using -q.

通常,当您看到这样的东西(检查grep的返回值)时,它会检查是否检测到特定的字符串。尽管重定向到/dev/null并不是必需的,但是使用-q可以完成同样的事情。

#2


26  

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded.

$ ?是最近执行的命令的退出状态;按照惯例,0表示成功,其他任何表示失败。这一行正在测试grep命令是否成功。

The grep manpage states:

grep从状态:

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”或更大。)

So in this case it's checking whether any ERROR lines were found.

在本例中,它检查是否找到了错误行。

#3


2  

It is an extremely overused way to check for the success/failure of a command. Typically, the code snippet you give would be refactored as:

它是检查命令的成功/失败的一种极其过度使用的方法。通常,您给出的代码片段将被重构为:

if grep -e ERROR ${LOG_DIR_PATH}/${LOG_NAME} > /dev/null; then
   ...
fi

(Although you can use 'grep -q' in some instances instead of redirecting to /dev/null, doing so is not portable. Many implementations of grep do not support the -q option, so your script to fail if you use it.)

(尽管您可以在某些实例中使用“grep -q”,而不是重定向到/dev/null,这样做是不可移植的。grep的许多实现不支持-q选项,因此如果您使用它,您的脚本就会失败。