exit status

The value of the status returned when exiting after the last command.

0 means success; A non-zero indicates failure.

The last status value can be printed on the command line

$ echo $?

If, until, and while depending on exit status

  • Grammar for until:

    until test-commands; do
      consequent-commands; 
    done
  • While the syntax

    while test-commands; do
      consequent-commands; 
    done
  • If the grammar

    if test-commands; then consequent-commands; [elif more-test-commands; then more-consequents;]  [else alternate-consequents;]  fi

When test-commands are executed, if, until, while depend on its exit status:

  1. Is 0, if executes;
  2. Is 1, until execute;
  3. When 0, while executes.

The conditions that test-commands contain

One or more sets of pipes form test-commands

  1. Groups of pipes can be separated by;.&.&&Or,||Space,
  2. by;.&Or,A newlineThe end;
  3. exit statusFrom the last set of pipesexit statusDecision;
  4. One or more commands constitute a pipe consisting of|| &Separated by the last commandexit statuspipe-determiningexit status;
  5. In general, a single command executes successfully with a status value of 0.

    // file test #! /usr/bin/bash if ls; ls; then echo ==111== else echo ==222== fi if ls; lss; then echo ==333== else echo ==444== fi $ ./test test test_1 test1 test test_1 test1 ==111== test test_1 test1 ./test: line 8: lss: command not found ==444==

((arithmetic expression)) to form test-commands

And let “expression”.

Addition, subtraction, multiplication and division, etc., the calculated value is 0, and the exit status value is 1; The calculated value is non-0, and the exit status value is 0;

// file test #! /usr/bin/bash if ((1+1)); then echo ==111== else echo ==222== fi if ((1-1)); then echo ==333== else echo ==444== fi $ ./test ==111== ==444==

[[conditional expression]] forms test-commands

// file test #! /usr/bin/bash if [[ str1 == str* ]]; then echo ==111== else echo ==222== fi if [[ 'str1' = 'str2' ]]; then echo ==333== else echo ==444== fi $ ./test ==111== ==444==

[conditional expressions] form test-commands

Similar to test expression, similar to [[conditional expression]]

// file test #! /usr/bin/bash if [ str1 == str* ]; then echo ==111== else echo ==222== fi if [ 'str1' = 'str2' ]; then echo ==333== else echo ==444== fi $ ./test ==222== ==444==

The difference between [[]] and []

  1. in[[]]In, word segmentation and filename extension will not be performed.
  2. in[[]],= =! =The right-hand operand is treated as a regular expression. (=Is equivalent to= =)

reference

  • Bash Reference Manual:https://www.gnu.org/software/…