condition Test
1. Condition testing: Test [
The command test or [can test if a condition is true, if the test result is real, the exit status of the command is 0, if the test result is false,
The exit status of the command is 1 (note the exact opposite of the C-word logical table). For example, test the size relationship of two numbers:
Although it looks strange, the left parenthesis [is indeed a command name, which should be between the arguments passed to the command? Space
Open, for example, $VAR,-GT, 3,] are [four parameters of the command, they must be separated by a space. command test or [parameter
The form is the same, except that the test command does not require a] parameter. For the [command] example, the common test commands are shown in the following table:
Test command
[-D dir]: true if Dir exists and is a directory
[-F file]: True if file exists and is a normal file
[-Z string]: True if the length of the string is zero
[-N string]: True if the length of a STRING is nonzero
[STRING1 = STRING2]: True if two strings are the same
[STRING1! = STRING2]: True if the string is not the same
OP is-eq (equals)-NE (Not equal to)-lt (? less than)-le (? Less than equals)-gt (? greater than)-ge (? greater than equals) in the? One (integer judgments and C words have very big differences).
Similar to the C language, test conditions can also be done with, or, non-logical operations:
Test commands with and, or, not
[ ! Expr]: Expr can be any of the test conditions in the table above. Represents a logical inverse
[Expr1-a EXPR2]: EXPR1 and EXPR2 can be any one of the test conditions in the table above, the-a table of logic and
[Expr1-o EXPR2]: EXPR1 and EXPR2 can be any of the test conditions in the table above,-o represents a logical OR
For example:
2. If/then/elif/else/fi
similar to the C language, branch control is implemented in the shell with the IF, then, Elif, else, and fi commands. This Process Control Language
A sentence is essentially made up of several shell commands.
is actually three commands, if [-F ~/.BASHRC] is the first, then. ~/.BASHRC is the second, FI is the third article. If two lines
Commands are written in the same line and need to be separated from each other, and a single command on one line does not need to be written;
But this command is not finished, the shell will automatically continue, the next line is followed by then as a command processing. and [command one
Note that the command and parameters must be separated by a space. The IF command's parameters form a sub-command if the subcommand
Exit status is 0 (table), then the subcommand is executed after then, and if exit status is not 0 (table, false), the
Elif, else, or sub-command behind FI. A subcommand after an if is usually a test command, but it can also be another command.
The shell script does not have {} brackets, so use fi to indicate the end of the IF statement block. See the following example:
: is a special command called the Empty command, which does nothing, but exit status is always true. In addition, you can also perform
/bin/true or/bin/false get a true or false exit Status.
In addition, the shell also provides && | | syntax, similar to the C language, has short-circuit features, and many shell scripts like to write
&& equivalent to "If...then ..." while | | Equivalent to "if Not...then ...". && | | Used to connect two commands, the "-a" on?
And-o only? To connect two test conditions in a test expression, be aware of their differences, for example,
Two types of writing are equivalent, but there are differences!!
3. Case/esac
The case command is analogous to the C-switch/case statement, and ESAC represents the end of the case statement block. C-language case can only match
An integer or character constant expression, and the shell script case can match strings and wildcard, and each matching branch can
To have a number of orders, at the end of which must be;; At the end of execution, find the first matching branch and execute the corresponding command, then straight
After jumping to ESAC, you do not need to jump out of the same language as C.
Examples of using case statements can be found in the script directory/ETC/INIT.D of the system services.
Looping statements
4. For/do/done
Class C? mode
5. While/do/done
The shell also has a until loop, similar to the C-language do...while loop.
6. How to debug shell scripts
The shell provides some options for debugging scripts, as follows:
- N
read the command in the script but not execute it to check for syntax errors in the script
- v
- x
provides trace execution information and prints each command and result that executes sequentially
There are three ways to use these options,
one is to supply parameters at the command line
$ sh-x./script.sh
? is to provide parameters at the beginning of the script
#!/bin/sh-x
Conditional testing and looping statements in the shell