Test, judge, cycle
Condition test:
To determine whether a demand is satisfied, it needs to be realized by testing mechanism;
How to write test expressions to implement the required tests;
(1) Execute the command, and use the command status return value to judge;
$?: return value of previous command
0: Success
1-255: Failure
grep "^&"/etc/init.d/functions &>/dev/null
echo $? Return 0 indicates a blank line, the test is successful;
(2) test expression;
Test method One:
Test expression
Test 2>3
echo $?
Test 2<3
echo $?
Test method Two:
[Expression]
[[Expression]]
Note: You must have a white-space character at both ends of expression, or a syntax error;
Test type of bash:
Numerical comparison
String test
File test
Logic test
Numeric test: integer comparison
-eq: Is it equal to; ["$num 1"-eq "$num 2"]
-ne: is not equal to;
-GT: Is it greater than;
-ge: is greater than or equal to;
-LT: Is it less than;
-le: is less than or equal to;
String test:
-Z "string": Determine whether the string is empty, NULL is true, not empty is false; (zero)
-N "string": Determine whether the string is not empty, empty is true, empty is false; (Non-zero)
=: is equal to; [Tom = Tom] [Tom = "$name"]
! =: Not equal;
"String1" = "string2" if string 1 equals string 2 is true, you can use "= =" instead of "=";
"String1"! = "string2" If string 1 is not equal to the string 2 is true, you can not use "= =" instead of "=";
: Is it greater than; (need to be escaped in single brackets)
<: Is it less than; (need to be escaped in single brackets)
=~: Whether the left string can be matched by the pattern on the right;
Name=tom
[["$name" =~ o.*]] true; only the matching part is true;
Note: (1) strings must be quoted;
(2) [] Both,> and < need to be escaped;
If you do not want to use the escape character, you can use the double brackets [[]];
[A > B]
[A < b]
["A" > "B"]
["A" < "B"]
[["A" > "B"]] True
[["A" < "B"]] false
File test:
The existence of the test, the existence of the true, does not exist as false;
-A file with-E
-E File
[-e/etc/rc.d/rc.sysinit]
Existence and type testing:
-B: Whether it exists and is a block device;
[-B/DEV/SDA]
-C: Whether it exists and is a character device file;
-D: Exists and is a directory file;
-F: Exists and is an ordinary file;
-H or-L: exists and is a symbolic link file;
-P: Whether it exists and is a pipe file;
-S: exists and is a socket file;
File permission test:
-R: Exists and is readable to the current user;
-W: exists and is writable to the current user;
-X: Exists and is executable to the current user;
Special permission tests:
-U: Exists and has SUID permission;
[-U/USR/BIN/SU]
-G: Exists and has sgid permissions;
[-U/USR/BIN/PASSWD]
-K: Whether it exists and has sticky permissions;
[-k/tmp]
Whether the file has content:
-S: There is content is true, empty is false;
Time Stamp test:
-N: Whether the file has been modified since the last read operation;
Dependency testing:
-O: Whether the current user is the owner of the file;
-G: Whether the current user belongs to the genus Group of the file;
Binocular test, File New and old comparison test:
File1-ef File2:file1 and File2 are hard links to the same inode as the same file system;
Whether File1-nt File2:file1 is new to file2; (newer than)
File1-ot File2:file1 is older than file2; (older than)
The main usage scenario for the new and old comparisons of files is to determine whether the files are updated or incremental;
Logic test, combination test conditions:
Logical operators:
COMMAND1 && COMMAND2
COMMAND1 | | COMMAND2
! COMMAND
Logical operation:
Number of operations: true (true,yes,on,1)
Fake (false,no,off,0)
And:
1 && 1 = 1
1 && 0 = 0
0 && 0 = 0
0 && 0 = 0
Or:
1 | | 1 = 1
1 | | 0 = 1
0 | | 1 = 1
0 | | 0 = 0
Non -:
!1 = 0
!0 = 1
Short Circuit rule:
COMMAND1 && COMMAND2
COMMAND1 is false, then COMMAND2 will not be executed;
Otherwise, COMMAND1 is true, then COMMAND2 must be executed;
COMMAND1 | | COMMAND2
COMMAND1 is true, then COMMAND2 will not execute;
Otherwise, the COMMAND1 is false, then the COMMAND2 must be executed;
[-o file] && [-R File]
ID user3 &>/dev/null && Exit 0 | | Useradd User3
If the ID User3 is true Then exit is executed, and if False, the useradd is executed;
Logic Tester:
Expression1-a expression2
Expression1-o expression2
!expression
[-O file-a-x file]
[!-e/var/log/messages] Test value is true the expression is false after the use of logical non;
In the shell, Process control is divided into two main categories:
One is: Judgment choice; (if,case)
One type is: cycle; (For,while,until,select)
If judgment structure:
If Expression;then
COMMAND
Fi
If is the simplest judgment statement, the command is executed if the test result is true (one-way selection)
IF/ELSE Judgment Structure:
If Expression;then
COMMAND
Else
COMMAND
Fi
If the judgment after if is true, then the command after then is executed, otherwise the command after the else is executed;
IF/ELIF/ELSE Judgment Structure:
If Expression1;then
COMMAND1
Elif Expression2;then
COMMAND2
Else
COMMAND
Fi
Note: Multiple conditions may be true at the same time, only the branch that is true for the first test is executed;
Case Judgment Structure:
Case VARIABLE in
VAR1) COMMAND1;;
VAR2) COMMAND2;;
VAR3) COMMAND3;;
*) COMMAND;
Esac
From top to bottom, compare $var and Var1, var2. is equal, if the match executes the following command, the default matches the last *);
Loop execution: Repeats a piece of code 0, 1, or more times;
Enter the condition: when the condition satisfies, only then enters the circulation;
Exit conditions: Each cycle should have an exit condition to have the opportunity to exit the loop;
There are 3 ways to loop a bash script:
For loop
While loop
Until cycle
For loop with list:
For VARIABLE in List;do
Loop body
Done
Entry criteria: As long as the list has elements available to enter the loop;
Exit Criteria: The element traversal in the list is completed;
How the list is generated:
(1) give directly;
(2) List of integers;
(a): {start.. End
echo {1..10}
echo {A.. Z
(b): seq
Seq Last seq 10
Seq First last SEQ 5 10
Seq First INCREMENT last seq 1 2 10; Seq 2 2 10
Seq 100-2 10 individually-2 to 10
(c): command to return a list
(d): Wild wildcard
(e): variable reference
[Email protected],$*
For loop without list:
For VARIABLE
Do
COMMAND
Done
When using a For loop without a list, pass the value of the variable to the for loop in the form of a parameter;
Vim for01.sh
#!/bin/bash
For VARIABLE
Do
Echo-n "$VARIABLE"
Done
With parameter execution:
#bash for01.sh 1 2 3
1 2 3
Although the syntax can work, but the readability is poor, so it is not recommended to use, you can use the special variable [email protected] rewrite the above structure, the function is exactly the same;
Vim for02.sh
#!/bin/bash
For VARIABLE in [email protected]
Do
Echo-n "$VARIABLE"
Done
A For loop for Class C:
For ((Expression1;expression2;expression3))
Do
COMMAND
Done
Expression1 is the initialization statement, which is generally used as the definition and initialization of variables;
Expression2 is the expression used to test whether the expression is true, to continue as true, and to exit for false;
Expression3 is used to modify the value of variable values and affect the cyclic behavior;
Vim for03.sh
#!/bin/bash
For ((i=1;i<=10;i++))
Do
Echo-n "$i"
Done
#bash for03.sh
1 2 3 4 5 6 7 8 9 10
While loop:
While expression
Do
COMMAND
Done
Tests the expression return value, executes the loop body for true, and does not execute the loop for false;
While Infinite loop:
Method One
while ((1))
Do
COMMAND
Done
Method Two
While True
Do
COMMAND
Done
Method Three
While:
Do
COMMAND
Done
A special use of the while loop: reads a file by line and is often used to process formatted data;
While Read line
Do
COMMAND
Done </path/to/file
Read each line in the file sequentially and assign a value to the variable lines;
The above uses the redirection method to complete the file reading, using the pipeline can also accomplish the same effect:
Cat/path/to/file | While Read line
Do
COMMAND
Done
Using redirects will only produce a shell;
Using a pipeline produces 3 shells: the first shell is cat, the second shell is the pipe, and the third shell is the while;
Until cycle:
Until expression
Do
COMMAND
Done
Tests the expression return value, executes the loop body for the false, and does not execute the loop for the true;
Until Infinite loop: (Opposite to while)
Method One
Until ((0))
Do
COMMAND
Done
Method Two
Until false
Do
COMMAND
Done
Select loop:
Select VARIABLE in List
Do
COMMAND
Done
is a menu extension loop, with syntax similar to a for loop with a list;
When you execute a select script, all the elements in the list are made available as a menu of 1, 2, 3, etc., and wait for user input;
When the user enters and returns, select can determine the input and execute subsequent commands;
If you enter directly, select will not exit, but again generate the list to wait for input;
The Select loop does not automatically exit, you need to use break to exit the loop, or terminate the script with the Exit command;
Display PS3 prompt, default is #, you can specify the prompt, directly specify ps3= "XXXX" can be;
The input of the user is stored in the built-in variable $reply;
Select is often used in conjunction with case;
#/bin/bash
Ps3= "What does want to eat?" "
Select VAR in gongbaojiding Baoyu Shanzhen quit
Do
Case $REPLY in
1) echo "You like $VAR.";;
2) echo "You like $VAR.";;
3) echo "You like $VAR.";;
4) echo "quit."; break;;
*) echo "Invalid input.";;
Esac
Done
This article is from "Bear Bears" blog, please be sure to keep this source http://keithtt.blog.51cto.com/3080111/1813930
Shell programming testing, judging, looping