1. If you judge some special usage
If [-Z $a] This indicates what happens when the value of variable A is empty
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ... Symbols such as <,>,==,!=,>=,<= cannot be used in []
$ $ does not exist, so n is empty;
[Email protected] ~]# n= ' wc-l/etc/passwd|awk ' {print $} ' [[email protected] ~]# echo $n [[email protected] ~]# if [-Z $n];then echo ' \ $n is null '; Fi$n is null
If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row?
1.txt contains 123;grep-q ' 123 ' 1.txt match OK, the return value is true;
[[email protected] shell]# cat 1.txt123sdwe[[email protected] shell]# if Grep-q "123" 1.txt; then echo Kong;fikong
2. Case Judgment in Shell
Format: case variable name in value1) command ;; value2) command ;; *) commond ;; esac in a case program, you can use | in the condition, meaning, such as 2|3) command ;; When the variable is 2 or 3 o'clock, the part of the command is executed.
The /etc/init.d/naginx has a case statement; the first parameter that matches the input is the start stop reload restart Conifgtest, and the other characters are returned
Usage:/etc/init.d/nginx {start|stop|reload|restart|configtest}
Case "$" in start) start; stop) stop;; reload) reload;; restart) restart;; Configtest) configtest;; *) echo $ "Usage: $ start|stop|reload|restart|configtest}" Retval=1esac
For example, enter a letter, prompt for a pure number, enter a number to determine whether it is even or odd;
[[email protected] 0618]# cat case1.sh #!/bin/bash# requires the number to be entered, to determine odd or even numbers, not to enter a number, and then exit ;read -p "Please input a number:" nn1= ' echo $n |sed ' s/[0-9]//g ' # The input is a number, the SED is replaced with NULL, the return value is NULL, the return value is not empty;if [ ! -z the input is a letter $n 1 ]then echo "please input a number " exit 1fin2=$[$n%2]case $n 2 in 0) echo "even" ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;1) echo "Odd" ;; *) echo "Not Present" ;; Esac
[Email protected] 0618]# sh-x case1.sh + read-p ' Please input a number: ' nplease input a number:de2++ echo de2++ sed ' s /[0-9]//g ' + n1=de+ ' ['! '-Z de '] ' + echo ' please input a number ' please input a number + exit 1[[email protected] 0618]# SH case1.sh Please input a number:234 even [[email protected] 0618]# sh case1.sh Please input a number:weplease input a numbe R
Case Experiment 2: input is blank then prompt to enter a number and then exit, enter the number in 0-100 to determine the score, the input is not a number, then prompted to enter a number and then exit;
Input negative and greater than 100 will prompt for the number of input is 0-100;
#!/bin/bash#case experiment, enter a number for the null prompt to enter the numbers and then exit; The number entered determines the score within 0-100; Enter a number with a non-numeric hint and exit;read -p "please input A number: " nif [ -z $n ]then echo "Please input a number" exit 1fin1= ' echo $n |sed ' s/[-0-9]//g ' #sed替换加了-Indicates negative;if [ ! -z $n 1 ]then echo "please input a number " exit 1fiif [ $n -ge 0 ] && [ $n -lt 60 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "Fail" ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2) echo "Pass" ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;3) echo "Good" ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;4) echo "Excellent" ;; *) echo "input number is 0-100" ;; Esac
[[Email protected] 0618]# sh case2.sh Please input a number:-200 enter a number for 0-100[[email protected] 0618]# sh case2.sh Put a number:101 the number entered is 0-100
3. Loops in shell scripts
For loop syntax structure: The for variable name in condition; Do ... done
While loop syntax structure: while condition; Do ... done dead loop used: denotes
Break directly ends this layer loop; Continue ignores the code under continue and makes the next loop directly
Exit directly out of the shell
For loop experiment: List all directories in/etc directory
[email protected] ~]# cat for.sh #!/bin/bashfor F in ' ls/etc/' doif [-d/etc/$f]then ls-d "/etc/$f" Fidone
While loop experiment: Judge the load cycle;
[[email protected] ~]# cat load.sh #!/bin/bash# Monitor Load script, take w load value within 1 minutes if greater than 10, then 30 seconds to send mail; while:d oload= ' W |head-1 |awk-f ' lo Ad average: ' {print $} ' |cut-d.-f1 ' If [$load-gt]then top|mail-s ' load is High: $load ' [Email protected]else Exit 0fi Sleep 30done
While loop experiment:
If the input is empty, the prompt asks to enter something, if the input character prompts to enter a pure number, enter a pure digital printing number, exit;
[[email protected] 0618]# cat while.sh #!/bin/bash# input is empty, prompting you to enter something until the input is not empty, and if you enter a letter, you are prompted to enter only a pure number until you enter a pure number, and the print digit ends; :d o read-p "Please input a number:" N if [-Z $n] then echo "Enter something" Continuefi n1= ' echo $n | Sed ' s/[-0-9]//g ' if [!-Z $n 1] then echo "Please enter a pure number" Continuefi Breakdoneecho $n
Continue exit this cycle, the loop inside continues, does not carry on the back of the loop;
Break jumps out of the loop, and the back of the loop executes.
Exit the word quit the entire script;
Break experiment: Exit the entire loop if conditions match;
[[email protected] 0618]# cat break.sh #!/bin/bash#break experiment; exit the entire loop if the condition matches; the back of the loop executes; for i in ' SEQ 1 5 ' do echo $i I f [$i = = 3]then break Fiecho $idoneecho OK
[Email protected] 0618]# sh break.sh11223ok
Continue experiment; exit this cycle and continue to execute the loop if the conditions match;
#!/bin/bashfor i in ' SEQ 1 5 ' do echo $i if [$i = = 3] Then continue fi echo $idoneecho OK
[Email protected] 0618]# sh continue.sh 112234455OK
Exit experiment; Quit the entire script if the condition matches;
#!/bin/bashfor i in ' SEQ 1 5 ' do echo $i if [$i = = 3] Then Exit 1 fi echo $idoneecho OK
[[Email protected] 0618]# sh break.sh 11223
4. Functions in the shell
The function is to organize a piece of code into a small unit, and give the small unit a name, when the code is used to call the name of the small unit directly.
Format: function F_name () {
Command
}
The function has to be on the front
The function can export global variables;
Function Experiment 1: Define the input function, enter a character, then print a character;
#!/bin/bashinput () {echo $}input yonglinux[[email protected] ~]# sh 1.sh yonglinux
Function Experiment 2: Define the SUM function and perform the sum operation;
#!/bin/bashsum () {s=$[$1+$2] echo $s}sum 1 2[[email protected] 0618]# sh 2.sh 3
View the function of the IP address;
#!/bin/bash# View the function of the IP address, enter a network card name, output the IP address of the network card, the network card name entered for the interaction, IP () {ifconfig |grep-a1 "$" |tail-1|awk ' {print-$} ' |awk-f ' : "{print $} '}read-p" Please input the ETH name: "emyip= ' IP $e ' echo ' $e address is $myip"
[[Email protected] 0618]# sh 2.sh Please input the ETH Name:eth0eth0 address is 192.168.11.100[[email protected] 0618]# sh 2.sh Please input the ETH name:eth1eth1 address is 192.168.20.100[[email protected] 0618]# sh 2.sh Please input the ETH n Ame:lolo address is 127.0.0.1
This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://8802265.blog.51cto.com/8792265/1664557
Shell script programming under Linux 2