The second of Shell programming learning
First, Bash's condition test
Test method, or test writing:
Test EXPR
[EXPR]
[[EXPR]]
For example: Test whether the variable user_name is root
Test $User _name= "root"
[$User _name = = "Root"]
[[$User _name = = "Root"]
Depending on the type of operand being compared, the test type is divided into:
Test Type |
Operator |
The meaning that the operator represents |
Example |
Plastic test |
-gt-lt-ge-le -eq-ne |
-GT greater than-lt less than -ge greater than or equal to-le less than or equal to -eq equals-ne Not equal to |
are both binocular operators: [$num-GT 3] Test $num-ne $sum [[$num-ne 0]] |
character test |
= = =~ > <! = |
= = equals > greater than < small != not equal to =~ pattern matching |
are binocular operators: [$num = = "root"] [[" $num "=~/bin/bash$]" P style= "Margin-top:0px;margin-bottom:0px;padding:0px;white-space:normal;" > |
-n-z |
-Z Test whether the contents of the file are empty, empty true, not empty for false -N test file content is not empty, empty is true, empty is false |
Monocular operator [-n ' $file _name '] |
file test |
-a|e-f-l|h-d -b-c-s-p-S -r-w-x-n |
-a|e test file exists -F test file as generic file -l|h test file for soft connection file -D test file is directory -b-c-p-s test file for block device, character device, pipe file, socket file -r-w-X test file readable, writable, executable -S test file empty file -N test file Mtime new to Atime |
are all monocular operators [-F "$file _name"] [[-B ' $file _name]] Test-x $file _name |
-nt-ot |
File1-nt file2 file1 mtime new File2 mtime File1-ot File2 File1 's mtime old File2 mtime |
Binocular operator [File1-nt "$file _name"] [[$file 1-ot "$file _name"]] |
Second, logical operation
Boolean operation: The result of the Boolean operation is only 2: true and False.
Common logical operations are: with or not XOR. With or very && | | ! To express.
The logical relationship with:
Fake && Free |
False |
True && True |
Really |
Or a logical relationship:
true | | Any |
Really |
False | | False |
False |
Non-logical relationship:
! fake |
Really |
! really |
False |
Different or logical relationship: the same is false, the difference is true
Third, the choice
The selection statements in bash are usually if,case. Of course if,case can be converted to each other in most cases.
1) If SELECT statement
1. Single Branch if statement:
If Condition;then
Select Branch
Fi
2, dual-branch if statement:
If Condition;then
Select branch 1
Else
Select branch 2
Fi
3, multi-branch if statement:
If Condition1;then
Select branch 1
Elif Condition2;then
Select branch 2
Elif Condition3;then
Select Branch 3
...
Else
Select Branch N
Fi
4. Practice
Write a script that accomplishes the following, using the following form:
script.sh {Start|stop|restart|status}
which
If the argument is empty, the Help information is displayed and the script exits;
If the argument is start, an empty file/var/lock/subsys/script is created and the starting script successfully is displayed.
If the parameter is stop, the file/var/lock/subsys/script is deleted and the stop script successfully is displayed.
If the parameter is restart, the file/var/locksubsys/script is deleted and recreated, and then the restarting script successfully is displayed.
If the parameter is status, then:
If the file/var/lock/subsys/script exists, "script is running ..." is displayed, otherwise "script is stopped."
Description: script.sh is the script file name, at the time of creation, its name can be defined at will, but if its name occurs variable, the file name under/var/lock/sussys/will change accordingly;
[[email protected] scripts]# cat myhttpd#!/bin/bash## description: # version: 0.0.1# author: alex# date: 2014-07-09# determine the number of parameters, if the parameter is less than 1, then prompt usage and exit if [ $# -lt 1 ];then echo "Usage: ' basename $0 ' {start|stop|restart|status} ' exit 2fi# judgment/ Var/lock/subsys is present and does not exist create this directory If [ ! -d /var/lock/subsys ];then/bin/mkdir -p /var/lock/subsysfiif [ "$" == "start" ];then# if/var/lock/subsys/' basename $0 ' file does not exist, it is created, and the display starts successfully; [ ! -e "/var/lock/subsys/' basename $0 '"   if present;] && /bin/touch "/var/lock/subsys/' basename $0 '" && echo "starting ' basename $0 ' successfully." | | echo "' basename $0 ' is already start." Elif [ "$" == "Stop" ];then# if/var/lock/subsys/' basename $0 ' file exists, then delete, and show shutdown succeeded [ -e "/var/lock/subsys/' basename $0 '" if it does not exist ] && /bin/rm -f "/var/lock/subsys/' basename $0 '" && echo "stop myhttpd successfully. " | | echo -e "stopping myhttpd \033[31mfailed\033[0m." elif [ "$" == "status" ];then# if/var/lock/subsys/' basename $0 ' file exists, show running If not present, prompt stopd[ -e "/var/lock/subsys/' basename $0 '" ] && echo " Basename $0 ' is running ... ' | | echo "' basename $0 ' is stopped." elseecho "usage: ' basename $0 ' {start|stop|restart|status}" fi
Iv. Other
1. Combination condition test: Implement logical operation between multiple conditions
With: [Condition1-a Condition2]
Condition1 && Condition2
Or: [Condition1-o condition2]
Condition1 | | Condition2
Non: [-not condition]
! Condition
2. Interactive programming of Bash programming
Read interacts with the user
-P "prompt" prompt statement
-T Timeout setting time-out
3. Exit
The program exits the statement, which is generally used as exit N, and N is the error state value.
4, shift
Usually shift n is used, and N is the number of arguments to be kicked off
[[email protected] scripts]# cat./aboutshift #!/bin/bashecho $1shift 2 #踢掉2个参数echo $1shift 3 #踢掉3个参数echo $1[[email Protec Ted] scripts]#./aboutshift 1 2 3 4 5 6 7 8 9 10136
Not to be continued ....
This article is from the "After Dark" blog, be sure to keep this source http://guoting.blog.51cto.com/8886857/1436505