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:
- Is 0, if executes;
- Is 1, until execute;
- When 0, while executes.
The conditions that test-commands contain
One or more sets of pipes form test-commands
- Groups of pipes can be separated by
;
.&
.&&
Or,||
Space, - by
;
.&
Or,A newline
The end; exit status
From the last set of pipesexit status
Decision;- One or more commands constitute a pipe consisting of
|
或| &
Separated by the last commandexit status
pipe-determiningexit status
; -
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 []
- in
[[]]
In, word segmentation and filename extension will not be performed. - in
[[]]
,= =
和! =
The right-hand operand is treated as a regular expression. (=
Is equivalent to= =
)
reference
- Bash Reference Manual:https://www.gnu.org/software/…