Shell scripting is a skill that any back-end programmer should master. Today, let’s learn its conditional branch structure together, and then master it through several cases.
First, let’s look at the basic structure of the shell script, which is as follows:
#!/bin/bash Code
Below, let’s write the simplest shell script.
#!/bin/bash echo 'hello world'
When the above program is executed, the hello world characters will be printed on the screen.
Next, let’s take a look at the conditional branch structure. Shell scripts have if and case statements about conditional branches.
if
Like other programming languages, the if statement of the shell program , conditional branches are also divided into single branch, double branch and multi-branch.
# Single branch if condition ;then ... the fi # double branch if condition ;then ... else ... the fi # multi-branch if condition; then ... elif condition; then ... else ... fi
First of all, let’s use a simple example to practice and write a shell script. The function of this script is that when the user enters a score, the program outputs different comments through the score, failed, good , Excellent, etc.
First of all, we need to prompt the user to enter a number within three digits. When the format entered by the user is incorrect, the user needs to be told to re-enter the score, and then exit the program. The code is as follows:
read -p "Please enter the score, the score range is 0-100: " score if [ -z `echo $score | egrep '^[0-9]+$'` ];then echo "The format of the grade entered is incorrect" fi
In the above code, we use the single-branch structure of if. Next, we need to use multiple branches, and print out different comments according to the results.
if ((score >= 90));then echo 'excellent' elif ((score >= 80));then echo 'good' elif ((score >= 70));then echo 'General' elif ((score >= 60 ));then echo 'Pass' else echo 'failing' fi
The above code is very simple, let’s post the complete code below:
#!/bin/bash read -p "Please enter the score, the score range is 0-100: " score if [ -z `echo $score | egrep '^[0-9]+$'` ];then echo "The format of the grade entered is incorrect" the fi if ((score >= 90)); then echo 'excellent' elif ((score >= 80));then echo 'good' elif ((score >= 70));then echo 'General' elif ((score >= 60 ));then echo 'Pass' else echo 'failing' fi
case
Next, let’s look at another conditional branch statement case, its basic structure is as follows:
case $variable in "Content 1") code block 1 ;; "Content 2") code block 2 ;; ... *) code block n ;; esac
The meaning of the above content is this, when the “variable value” is equal to “content 1”, execute code block 1, and when it is equal to “content 2”, execute code block 2, if none of the previous If satisfied, execute code block n.
Next, let’s use a simple case to see how the case is used.
#!/bin/bash case $1 in "start") echo "this code is start" ;; "stop") echo "this code is stop" ;; "restart") echo "this code is restart" ;; *) echo "Usage ${0} {start|stop|restart}" ;; esac
The meaning of the above code is that when the user input parameter is start, the program prints this code is start, when the input parameter is stop, it outputs this code is stop, when the input parameter is restart, Output this code is restart, otherwise input “Usage script file name {start|stop|restart}”.
The above is the detailed content of mastering the conditional branch structure of shell programming through several cases. For more information, please pay attention to other related articles on 1024programmer.com!