当在Bash脚本中的if条件表达式是一个命令时,它是什么意思?

huangapple go评论68阅读模式
英文:

When the condition expression in a if is a command in a bash script, what it means?

问题

The if condition in your bash script checks whether the two files, "output.bin" and "output_.bin," are the same. If they are different, it executes the continue statement; otherwise, it skips the continue. However, the behavior you observed is opposite because the cmp command returns a zero exit status (success) if the files are the same and a non-zero exit status (failure) if they are different. In bash, a zero exit status signifies success, so when the two files are the same, the continue statement will execute.

英文:

When I write the bash script like below:

file_path="/workspace/output.bin"
file_path_="/workspace/output_.bin"

if cmp -s "$file_path" "$file_path_"; then
    continue
fi

In my opinion, the condition is the return value of the cmp command, so the if expression above means when output.bin and output_.bin are different excute continue, otherwise skip continue.

But in fact, the execution behavior of bash is completely opposite.
When the two files are same, the continue will excute. Why?

Or what the if condition means?

答案1

得分: 3

以下是你要的翻译:

根据help if

if:if 命令; then 命令; [ elif 命令; then 命令; ]... [ else 命令; ] fi
    根据条件执行命令。

    执行`if 命令`列表。如果其退出状态为零,则执行`then 命令`列表。否则,逐个执行每个`elif 命令`列表,如果其退出状态为零,则执行相应的`then 命令`列表,然后if命令完成。否则,如果存在的话,执行`else 命令`列表。整个结构的退出状态是最后执行的命令的退出状态,如果没有测试条件为真,则为零。

    退出状态:
    返回最后执行的命令的状态。

根据cmp的退出状态,如果为真(零),则:

cmp -s "$file_path" "$file_path_"
echo $?

应该返回0,这意味着为true

如果你的系统中的cmp支持--help标志:

cmp --help | grep '^退出状态'

输出:

如果输入相同,则退出状态为0,如果不同,则为1,如果出现问题,则为2。

根据help test

help test | grep -F '! EXPR'

输出:

! EXPR        如果表达式为假,则为真。

! 感叹号表示否定,意味着退出状态相反。

这意味着你可以这样做:

if ! cmp -s "$file_path" "$file_path_"; then
    continue
fi

否则,需要一个elifelse块。

使用!进行否定不仅限于if子句/语句,你可以执行类似以下的操作:

! cmp -s "$file_path" "$file_path_"
echo $?

此外,&&|| 也适用于该表达式。

  • 请参阅LESS='+/Compound Commands' man bash(如果可用)。
  • 请参阅LESS='+/EXIT STATUS' man bash(如果可用)。
  • 请参阅Exit Status
  • 请参阅Conditional Construct
英文:

Accoring to

help if

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.

    The `if COMMANDS' list is executed.  If its exit status is zero, then the
    `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
    executed in turn, and if its exit status is zero, the corresponding
    `then COMMANDS' list is executed and the if command completes.  Otherwise,
    the `else COMMANDS' list is executed, if present.  The exit status of the
    entire construct is the exit status of the last command executed, or zero
    if no condition tested true.

    Exit Status:
    Returns the status of the last command executed.

So in your case if the exit status of cmp is true (zero):

cmp -s "$file_path" "$file_path_"
echo $?

Should return 0 if they are identical which means true


If the cmp in your system supports the --help flag:

cmp --help | grep '^Exit status'

Output

Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

Now according to help test

help test | grep -F '! EXPR'

Output

      ! EXPR         True if expr is false.

The ! bang negates, meaning the exit status is reverse.


Which means you could do:

if ! cmp -s "$file_path" "$file_path_"; then
    continue
fi

Otherwise an elif or else block is needed.


The negation using ! is not limited/attached to the if clause/statement, you could do things like:

! cmp -s "$file_path" "$file_path_" 
echo $?

Also the && and || works with that expression,

  • See LESS='+/Compound Commands' man bash if available.

  • See LESS='+/EXIT STATUS' man bash if avaialble

  • See Exit Status

  • See Conditional Construct

huangapple
  • 本文由 发表于 2023年4月11日 14:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定