UNIX System Variables
$? The return code of the previous command or function.
$ # Number of parameters
$, $0 is the program body, from $1, $2, $3 is the parameter
$ * String: saves all parameters in the form of "parameter 1 parameter 2 ..."
$ @ String array: All parameters are saved in the string array format of "parameter 1" "parameter 2 "...
$ PID of the Program (process ID)
$? Return code of the last command (success 0, failure 1)
$ Ls 111.txt 111. txt $ Echo $? 0 If the previous command is successful, 0 is returned. |
[MACG @ machome ~] $ Ls sdsdf Ls: sdsdf: no such file or directory [MACG @ machome ~] $ Echo $? 1 The previous command failed (error). 1 is returned. |
Common return code $? Checks whether the program exits abnormally and whether execution fails.
| [Mac @ machome ~] $ VI test. Sh Ls-L $1 If [$? ! = 0]; The then program exits unexpectedly and fails to be executed. Echo command fail Else Echo command success Fi |
| [Mac @ machome ~] $ Sh test. Sh 111.txt -RW-r -- 1 Mac 0 Jun 7 :35 111.txt Command success [Mac @ machome ~] $ Sh test. Sh 222.txt Ls: 222.txt: no such file or directory Command fail |
Standard command line parameter processing mechanism-use shelf to process command line parameters in sequence-only process $1 (the first parameter), shift until $1 is empty
While [-n "$1"]; do Why is the loop always $1 (the first parameter )? Case $1 in -H) H = $1 Echo "your choice is $ H" Shift to forward parameters in sequence Shift ;; -F) F = $1 Echo "your choice is $ F" Shift ;; -C) C = $1 Echo "your choice is $ C" Shift ;; -Z) z = $1 Echo "your choice is $ Z" Shift ;; *) Echo "$1 is wrong paratism" Break ;; Parameters that do not meet the condition are not recycled (break) (equal to or later parameters are not checked) Esac Done |
[MACG @ machome ~] $ Sh test. Sh-H-z-C-F Your choice is-H Your choice is-z Your choice is-C Your choice is-F [MACG @ machome ~] $ Sh test. Sh-6-H -6 is wrong paratism [MACG @ machome ~] $ Sh test. Sh-H-z-6 Your choice is-H Your choice is-z -6 is wrong paratism |
The most typical single-parameter command line is used for START | stop | restart | status processing of the Sys v STARTUP script.
Case "$ @" in Start) Echo-n "Starting firewall ..." ... Echo "OK! " Exit 0 ;; Stop) Echo-n "Stopping firewall ..." ... Exit 0 ;; Restart) $0 stop $0 start ;; Status) Clear Echo "> ------------------------------------------" Iptables-l Echo "> ------------------------------------------" Iptables-T nat-l postrouting Exit 0 *) Echo "Usage: $0 {START | stop | restart | status }" $0: Execute the original program Exit 1 Esac |
Several Methods for passing parameters in Shell programs
1. Command Line Parameters
2. Read Parameters
3. Read the configuration file
AA = 'cat param.txt | gawk '/input:/{print $1 }'' Echo "AA is $ AA"$ Sh TB. Sh AA is echo |
Check the number of command line parameters
If the parameter is not full ([$ #-lt 3]), the help If [$ # lt 3]; then Echo "Usage: $0 host user passwd" Exit 1 Fi |
[MACG @ machome ~] $ Sh test. Sh 23 23 Test. sh: line 18: [: Lt: binary operator expected |
Where is the error? INTEGER (GT, LT, EQ, NE) must contain "-"
Change to if [$ #-lt 3]; then |
Use simplified if and $1, $2, and $3 to detect parameters. If it is unreasonable, call help.
[-Z "$1"] & help if the first parameter does not exist (-Z string length is 0)
["$1" = "-h"] & Help