Use if... then
- Single-layer, simple conditional
If [conditional statement]; then Command work content that can be executed when the conditional statement is set; FI <== writes if in turn, and then becomes Fi! End if! |
- & Stands for and;
- | Represents or;
["$ YN" = "Y"-o "$ YN" = "Y"]
The above formula can be replaced
["$ YN" = "Y"] | ["$ YN" = "Y"]
[[Email protected] Scripts] # cp sh06.sh sh06-2.sh <= use change faster! [[Email protected] Scripts] # vi sh06-2.sh #! /Bin/bash # program: # This program shows the user's choice # History: #2005/08/25 vbird first releasepath =/bin:/sbin:/usr/bin: /usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathread-P "Please input (y/N ): "ynif [" $ YN "=" Y "] | [" $ YN "=" Y "]; thenecho" OK, continue "Exit 0 fiif [" $ YN "=" N "] | [" $ YN "=" N "]; thenecho" Oh, interrupt! "Exit 0 fiecho" I don't know what your choice is "& Exit 0 |
- Multiple and complex condition types
# One condition judgment, successful or failed (else) If [conditional statement]; then Command work content that can be performed when the conditional statement is set; else command work content that can be executed when the condition criterion is invalid; FI |
# Multiple condition judgment (if... elif... elif... else) run if [conditional statement 1] in different situations; then Command work that can be performed when the conditional statement is created; Elif [conditional statement 2]; then command work content that can be executed when the condition criterion is set to two; else command work content that can be performed when neither the condition criterion 1 nor the two are true; FI |
[[email protected] scripts]# cp sh06-2.sh sh06-3.sh[[email protected] scripts]# vi sh06-3.sh#!/bin/bash# Program:# This program shows the user‘s choice# History:# 2005/08/25 VBird First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N): " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ]; thenecho "OK, continue"elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; thenecho "Oh, interrupt!"elseecho "I don‘t know what your choice is"fi |
Generally, if you do not want the user to input additional data on the keyboard, the user will include the parameter when issuing the command! Now we want the user to enter the keyword "hello", the parameter method can be designed in sequence as follows:
- Determine whether $1 is "hello". If yes, "Hello, how are you" is displayed? ";
- If no parameters are added, the system prompts you to use the parameter issuing method;
- If the added parameter is not "hello", the user is reminded to use "hello" as the parameter only.
The entire program can be written as follows:
[[email protected] scripts]# vi sh09.sh#!/bin/bash# Program:#Check $1 is equal to "hello"# History:# 2005/08/28VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHif [ "$1" == "hello" ]; thenecho "Hello, how are you ?"elif [ "$1" == "" ]; thenecho "You MUST input parameters, ex> {$0 someword}"elseecho "The only parameter is ‘hello‘, ex> {$0 hello}"fi |
The netstat command can query the network service ports enabled on the current host)
[[Email protected] ~] # Netstat-tulnactive Internet connections (only servers) proto Recv-Q send-Q local address foreign address statetcp 0 0 0.0.0.0: 111 0.0.0.0: * listentcp 0 0 127.0.0.1: 631 0.0.0.0: * listentcp 0 0 127.0.0.1: 25 0.0.0.0: * listentcp 0 0 0 ::: 22 ::: * listenudp 0 0 0.0.0.0: 111 0.0.0.0: * UDP 0 0.0.0.0: 631 0.0.0.0: * # local IP address in the Encapsulation Format: Port remote IP Address: whether the port listens |
The above focuses on the field "local address (corresponding to the IP address and port of the local host)", which represents the network service started on the local host! If it is 127.0.0.1, it is only open to the local machine. If it is 0.0.0.0 or:, it means it is open to the entire internet. Each port has a specific network service. The relationship between several common ports and related network services is as follows:
- 80: www
- 22: SSH
- 21: ftp
- 25: Mail
- 111: RPC (Remote program call)
- 631: CUPS (printing service function)
Assume that my host is interested in detecting common port 21, 22, 25, and 80, then, how can I use netstat to detect whether my host has enabled these four major network service ports? The keyword of each service is followed by the colon ":", so we can detect it by retrieving a keyword similar to ": 80! Then I can simply write this program like this:
[[Email protected] Scripts] # vi sh10.sh #! /Bin/bash # program: # using netstat and grep to detect WWW, ssh, FTP and mail services. # History: #2005/08/28 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path #1. Do some Notification actions first ~ Echo "now, I will detect your Linux server's services! "Echo-e" the WWW, FTP, ssh, and mail will be detect! \ N "#2. Start some testing and output some information! Testing = $ (netstat-tuln | grep ": 80") # Check whether port 80 is correct? If ["$ testing "! = ""]; Thenecho "WWW is running in your system." fitesting = $ (netstat-tuln | grep ": 22") # Check whether port 22 is correct? If ["$ testing "! = ""]; Thenecho "SSH is running in your system." fitesting = $ (netstat-tuln | grep ": 21") # Check whether port 21 is available? If ["$ testing "! = ""]; Thenecho "FTP is running in your system." fitesting = $ (netstat-tuln | grep ": 25") # Check whether port 25 is available? If ["$ testing "! = ""]; Thenecho "mail is running in your system." fi |
Since date is treated in subtraction mode, we can use date to display the date and time and convert it to the number of seconds accumulated from, after the remaining number of seconds is obtained, the number of days can be converted to the number of days. The entire script creation process is a bit like this:
- Let users enter their retirement date first;
- Compare the retired date with the current date;
- By comparing the two dates, the words "a few days is needed" can be retired.
After converting "date -- date =" yyyymmdd "+ % s" to seconds, the next action is much easier! If you have already completed the program, refer to the following statement:
[[Email protected] Scripts] # vi sh11.sh #! /Bin/bash # program: # You input your demo-ization date, I calculate how many days # Before you demo-ize. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path #1. Inform the user of the purpose of this program and how to enter the date format? Echo "this program will try to calculate:" Echo "how many days before your demo-ization date... "Read-P" Please input your demo-ization date (yyyymmdd ex> 20090401): "date2 #2. test whether the input content is correct? Use the regular expression date_d =$ (echo $ date2 | grep '[0-9] \ {8 \}') # Check whether there are eight numbers if ["$ date_d" = ""]; thenecho "you input the wrong date format .... "Exit 1fi #3. start computing date declare-I date_dem = 'date -- date = "$ date2" + % s' # Number of retired date seconds declare-I date_now = 'date + % s' # current date seconds declare-I date_total_s =$ ($ date_dem-$ date_now )) # remaining number of seconds statistics declare-I date_d =$ ($ date_total_s/60/60/24) # convert to the number of days if ["$ date_total_s"-lt "0"]; then # determine if echo "you had been demo-ization before:" $(-1 * $ date_d )) "Ago" elsedeclare-I date_h = $ ($ date_total_s-$ date_d * 60*60*24)/60/60 )) echo "you will demo-ize after $ date_d days and $ date_h hours. "fi |
Use Case... esac to judge
Case $ variable name in <= the keyword is case, and there is also a rich variable front font size "first variable content") <= each variable content is recommended to be enclosed in double quotation marks, keyword is parentheses) program segment; <= two consecutive semicolons are used at the end of each category for processing! "Second variable content") program segment ;;*) <= The Last variable content uses * to indicate that all other program segments whose values do not contain the first variable content or the second variable content Exit 1 ;; esac <= end of the final case! "Write in turn 』! |
[[Email protected] Scripts] # vi sh09-2.sh #! /Bin/bash # program: # Show "hello" from $1 .... by using case .... esac # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathcase $1 in "hello") echo "Hello, how are you? ";" ") Echo" you must input parameters, ex> {$0 someword} "; *) # actually equivalent to bytes, 0 ~ Infinite number of arbitrary bytes! Echo "usage $0 {Hello}"; esac |
/Etc/init. d/syslog restart
In general, in the "case $ variable in" syntax, the "$ variable" has two methods:
- Direct release: As mentioned above, the script is used. sh variable "to directly give the content of the variable $1, which is also in/etc/init. d directory.
- Interactive: The READ command allows users to input variable content.
The user can enter One, Two, and Three and display the user's variables on the screen. If it is not one, two, or three, the user is notified that there are only three options.
[[Email protected] Scripts] # vi sh12.sh #! /Bin/bash # program: # This script only accepts the flowing parameter: one, two or three. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathecho "this program will print your selection! "# Read-P" input your choice: "choice # temporarily canceled. It can be replaced! # Case $ choice in # temporarily canceled. It can be replaced! Case $1 in # used now, you can replace it with the above two lines! "One") echo "your choice is one"; "two") echo "your choice is two"; "three") echo "your choice is three ";; *) echo "usage $0 {one | two | three}"; esac |
Use Function
Function fname () {program segment} |
Because shell script runs from top to bottom, from left to right, the function configuration in shell script must be at the beginning of the program, in this way, it can be found at runtime!
[[Email protected] Scripts] # vi sh12-2.sh #! /Bin/bash # program: # use function to repeat information. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathfunction printit () {echo-n "your choice is" # Add-N to continue display in the same row} echo "this program will print your selection! "Case $1 in" one ")Printit; Echo $1 | tr 'a-z'' A-Z' # convert the parameters into uppercase and lowercase letters !;; "Two ")Printit; Echo $1 | tr 'a-z'' A-Z'; "three ")Printit; Echo $1 | tr 'a-z'' A-Z'; *) echo "usage $0 {one | two | three}"; esac |
The function also has built-in variables. The function name represents $0, and the subsequent variables are also $1, $2...
[[Email protected] Scripts] # vi sh12-3.sh #! /Bin/bash # program: # use function to repeat information. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathfunction printit () {echo "your choice is $1" # This $1 must be issued by referring to the following command} echo "this program will print your selection! "Case $1 in" one ")Printit 1# Note that the printit command is followed by parameters !;; "Two ")Printit 2; "Three ")Printit 3; *) Echo "usage $0 {one | two | three}"; esac |
Chapter 2. Learning the conditional attention style of shell scripts