Article Directory
-
Table of Contents
Article directory
Preface
1.if statement syntax
double branch structure
multi-branch structure
case analysis
Summarize
Foreword
In the command connection symbols & amp; & amp; and || have their own judgment conditions. This is the condition judgment learned in the shell basics. In script writing, the shell script has its own condition judgment for process control.
一.if statement syntax
Grammar 1:
# Syntax 1: if <conditional expression> then instruction fi
Syntax 2:
#Syntax 2: if <conditional expression>;then instruction fi
Double branch structure
grammar
if <conditional expression> then Instruction sequence 1 else Instruction sequence 2 fi
Multi-branch structure
if conditional expression 1 then Instruction sequence 1 elif conditional expression 2 then Instruction sequence 2 else Instruction sequencen fi
Case Analysis
Case 1: Write the script choice1.sh, use a single branch structure to input 2 integers and output the maximum value
[root@server ~]# vim choice1.sh #!/bin/bash read -p "Please enter the first integer: " x read -p "Please enter the second integer: " y max=$x if (($max<y)) then max=$y fi echo "Maximum value: $max"
Case 2: Interview question, write the script choice2.sh to determine the remaining capacity of the system memory. If it is less than 100M, send a message to alert you.
#!/bin/bash free_mem=$( free -m | grep Mem | tr -s " " | cut -d " " -f4 ) if (($free_mem<100)) then echo "Warning, remaining memory: $free_mem, less than Warning Line 100MB" else echo "Remaining memory: $free_mem, enough space." fi # free -m: Indicates displaying memory, that is, virtual memory information, -m displays numbers in MB units # grep Mem: means filtering the lines containing Mem # tr -s " ": compress spaces into one # cut -d " " -f4: Use space as the separator and take the 4th part
Case 3: Write the script choice3.sh to determine the executor of the current script. If it is not the root account, it will prompt and exit.
# 4 ways to check the current account [root@server ~]# whoami root [root@server ~]# id -u [root@server ~]# echo $USER root [root@server ~]# echo $UID [root@server ~]# vim choice3.sh #!/bin/bash if [ "$USER" != "root" ] then echo "please switch user root" fi [root@server ~]# bash choice3.sh # Execute under root account [root@server ~]# pwd /root [root@server ~]# mv choice3.sh / # To allow ordinary accounts to access the script, it needs to be moved to / [root@server ~]# chmod + x /choice3.sh # Grant execution permissions [root@server ~]# ll /choice3.sh -rwxr-xr-x 1 root root 81 June 18 10:10 /choice3.sh [root@server ~]# tail -1 /etc/passwd # View common account names fox:x:1000:1000::/home/fox:/bin/bash [root@server ~]# su fox #Switch account [fox@server root]$ cd / [fox@server /]$ bash choice3.sh please switch user root
Case 4: Write the script choice4.sh to realize leap year judgment
[root@server ~]# vim choice4.sh #!/bin/bash read -p "Please enter a 4-digit integer year: " year if [ -n "$year" ] # Non-empty test then if ((year%4==0)) & amp; & amp; ((year 0!=0)) || ((year@0==0)) then echo "leap year" else echo "Ping Nian" fi else echo "Please enter a valid 4-digit integer year" fi
Case 5: Write the script choice5.sh to determine whether sshd is running
# Analysis #Judge based on the number of processes [root@server ~]# ps -ef | grep sshd | grep -v grep | wc -l #Judge based on port number [root@server ~]# netstat -lntup | grep 22 | wc -l [root@server ~]# vim choice5.sh #!/bin/bash num=$(ps -ef | grep sshd | grep -v grep | wc -l) if (($num>0)) then echo "sshd is running" else echo "sshd is not running" fi
Case 6: Interview question, write script choice6.sh to determine whether the host is alive
[root@server ~]# vim choice6.sh #!/bin/bash read -p "Please enter the IP address of the test host:" ip ping -c 2 -w 3 $ip &> /dev/null # -c 2 means sending 2 data packets, -w 3 means waiting for 3 seconds to end, note Note: You cannot wait for 1 second to end. It is possible that the second data packet has not been pinged. When it is over, an incorrect status code will be returned. if [ $? -eq 0 ] then echo "Host $ip is already running" else echo "Host $ip is not running" fi # Modify the above example, use loop to test whether multiple hosts are alive, and set the display color color [root@server ~]# vim choice6.sh #!/bin/bash for ip in 192.168.48.{125..135} do ping -c 2 -w 3 $ip &> /dev/null if [ $? -eq 0 ] then echo -e "\e[1;31m!!Host $ip Run!!\e[0m" # Highlight, red else echo "Host $ip is not running" fi done
Case 7: Write the script choice7.sh, enter the hundred-point score, and determine the grade
[root@server ~]# vim choice7.sh #!/bin/bash read -p "Please enter the percentage score: " score if [ -z $score ] then echo "Not entered, please re-enter" elif [ $score -lt 0 -o $score -gt 100 ] then echo "Incorrect score input, please enter a number between 0-100" elif[$score-ge90] then echo "Excellent results" elif[$score-ge80] then echo "Good grades" elif [ $score -ge 60 ] then echo "passing grade" else echo "make-up exam" fi
Case 8: Write a script to determine whether the user input is numbers, letters, or other characters.
[root@server ~]# vim choice8.sh #!/bin/bash read -p "Please enter numbers, letters, symbols: " str if echo $str | grep [a-zA-Z] > /dev/null then echo "letter" elif echo $str | grep [0-9] > /dev/null then echo "number" else echo "symbol" fi
Case 9: Write the script choice9.sh to determine the current host CPU manufacturer
[root@server ~]# vim choice9.sh #!/bin/bash vendor=$(grep "vendor_id" /proc/cpuinfo | uniq | cut -d " " -f2) if [ $vendor == GenuineIntel ] then echo "Intel" elif [ $vendor == AuthenticAMD ] then echo "AMD" else echo "other" fi
- The case statement will compare the value of the variable with the value in ) brackets. If it is equal to a certain value, the corresponding statement will be executed. When the “;;” symbol is encountered, the case statement is jumped out and the statement following the esac statement is executed. If there is no match with any value, execute the set of statements following *)
Case 10: Write the script choice10.sh to rewrite the hundred-point score judgment grade in Example 7 above.
[root@server ~]# vim choice10.sh #!/bin/bash read -p "Please enter the percentage score: " score case $score in 9[0-9]|100) echo "Excellent results" ;; 8[0-9]) echo "Good grades" ;; 6[0-9]|7[0-9]) echo "passing grade" ;; *) echo "make-up exam" esac
Summary
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS entry skill treeLinux introductionFirst introduction to Linux 37641 people are learning the system