This is the 24th day of my participation in the August More Text Challenge
A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.
Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!
The original link: Shell programming – control structure | base paper
If-then-else branch
If-then-else is a process control structure based on conditional test results. If the test result is true, the corresponding command list in the control structure is executed; Otherwise, another conditional test or exit the control structure.
1.1 If-then-else syntax
If condition 1 then command list 1 elIF condition 2 then command list 2 else command list 3 fiCopy the code
Note: If condition 1 is true, execute command list 1 and exit if-then-else control structure. If condition 2 is true, execute command list 2 and exit the if-then-else control structure.
Otherwise, execute command listing 3 and exit the if-then-else control structure. There can only be one if statement and one else statement in the same if-then-else structure, and there can be multiple EILF statements. Where if statements are required, elif and else statements are optional.
The Shell script first determines whether file test1 is readable, and if it is, it will output is readable! Prompt message; Otherwise, no action is performed.
[root@localhost 20190105]# vi test.sh filename=test1 if [-r $filename] then echo $filename' is readable ! ' fi [root@localhost 20190105]# sh test.sh test1 is readable !Copy the code
The Shell script determines whether The number variable is equal to 100, and if so, prints The number is equal to 100! The prompt; The number is not equal 100! .
[root@localhost 20190105]# vi number.sh number=200 if [$number-eq 100] // The number is equal to 100! Then echo 'The number is equal 100! 'else // Otherwise print' The number is not equal 100! Echo 'The number is not equal 100! ' fi [root@localhost 20190105]# sh number.sh The number is not equal 100 !Copy the code
The Shell script first determines if The number variable is less than 10, and if so prints The number < 10! ; Otherwise, check whether the number variable is greater than or equal to 10 and less than 20.
10 =< The number < 20! ; Otherwise, check whether the number variable is greater than or equal to 20 and less than 30.
If so, print 20 =< The number < 30! ; Otherwise, output 30 <= The number! .
[root@localhost 20190105]# vi number1.sh number=25 if [$number -lt 10] then echo 'The number < 10! 'elif [$number -ge 10 -a $number -lt 20] then echo '10 =< The number < 20! 'elif [$number -ge 20 -a $number -lt 30] then echo '20 =< The number < 30! Echo '30 <= The number! ' fi [root@localhost 20190105]# sh number1.sh 20 =< The number < 30 !Copy the code
2. Case branch structure
If-then-else structures can support multiple branches (multiple ELIF statements), but if there are multiple branches, the program becomes difficult to read. The case structure provides a cleaner way to implement multi-way branching.
2.1 Case Syntax Format
Case value or variable in mode 1) Command list 1; Mode 2) Command list 2; . esacCopy the code
The case statement is followed by the value or variable to be tested. The Shell sequentially compares the values or variables to be tested against the patterns specified in the case structure, and when a match is successful, executes the list of commands corresponding to that pattern and exits the case structure (each command list is marked with two semicolons;;). To conclude). If no matching pattern is found, the case structure is exited after esAC.
The following script tests the value of the number variable and outputs the corresponding information if it matches the pattern.
[root@localhost 20190105]# vi case.sh number=66 case $number in 33) echo 'The number is 33 ! '//number variable equals 33; 44) echo 'The number is 44 ! '//number variable equals 44; 55) echo 'The number is 55 ! '//number variable equals 55; 66) echo 'The number is 66 ! '//number variable equals 66; 77) echo 'The number is 77 ! '//number variable equals 77; 88) echo 'The number is 88 ! '//number variable equals 88; [root@localhost 20190105]# sh case.sh The number is 66! // Output of the commandCopy the code
3. For loop structure
The for loop structure can execute a list of commands repeatedly, depending on the list of values specified in the for statement to continue or break the loop. The for loop checks for unused values in the list of values before executing the command list. If so, it assigns the value to the variable specified in the for statement, and then executes the list of commands in the loop structure. This loop continues until all of the values in the value list are used.
3.1 For Loop Structure syntax
For variable name in Value list DO command 1 Command 2 Command 3... doneCopy the code
3.2 Lists of values using constants
Using variables 1, 2, 3, 4, 5, 6 as a list of values, the for loop simply prints the values from the list of values.
[root@localhost 20190105]# vi for1.sh #! /bin/bash for n in 1 2 3 4 5 6 // loop read 1-6 do echo $n done [root@localhost 20190105]# sh for1.sh 1 2 3 4 5 6Copy the code
3.3 Lists of values using variables
The list of values can be an environment variable.
[root@localhost 20190105]# vi for2.sh #! /bin/bash values="1 2 3 4 5 6" // Assign value to values variable for n in $values // loop read values in values variable do echo $n done [root@localhost 20190105]# sh for2.sh 1 2 3 4 5 6Copy the code
3.4 Using the command result as the value list
The Shell supports using the results of the command as a list of values for the for loop. Use “command” or “$(command)” to refer to the result of a command in the Shell. The result of the ls command will be used as the list of values.
[root@localhost 20190105]# vi for3.sh #! Do echo $n // Print the value of variable n done [root@localhost 20190105]# sh for3.sh case.sh for1.sh for2.sh for3.sh HelloWorld.sh number1.sh number.sh test1 test2 test.shCopy the code
Expr command calculator
Expr is a command line counter for addition, subtraction, multiplication, and division.
[root@localhost 20190105]# expr 9 \* 8 //9 times [root@localhost 20190105]# expr 666/8 // 666 divided by 8 is 83 83Copy the code
In the loop structure, expr is used for incremental calculations, starting at 10 and adding 11/12 each time expr is used. Note that the expr command is used with backapostrophes, not single quotes.
[root@localhost 20190105]# number= 'expr $number + 11' [root@localhost 20190105]# number= 'expr $number + 11 [root@localhost 20190105]# echo $number 21 [root@localhost 20190105]# number= 'expr $number + 12 [root@localhost 20190105]# echo $number 33Copy the code
While loop structure
The while structure executes a series of commands through a loop and decides whether to continue the loop or break out of the loop based on the test conditions specified in the while statement. If the condition is true, the while loop executes a series of commands in the structure. After the command is executed, the control returns to the top of the loop and restarts until the test condition is false.
5.1 Syntax for while loop structures
While condition do command 1 command 2... doneCopy the code
5.2 Circular incremental calculation: Incremental calculation is used in a while loop, and the result is as follows.
[root@localhost 20190105]# vi while1.sh #! While [$count -lt 5] do [$count -lt 5] do $count = 'expr $count + 1' echo $count done [root@localhost 20190105]# sh while1.sh 1 2 3 4 5 [root@localhost # 20190105]Copy the code
5.3 Reading contents from files in a loop
There is a file that holds the student’s grade information, where the first column is the student’s name and the second column is the student’s grade.
[root@localhost 20190105]# vi students.log
jake 85
tom 68
lucy 79
sam 95
Copy the code
Now to the above documents in the student score statistics, calculate the number of students and the average score of students. The contents of variables STUDENT and SCORE are read by the while read statement, and then the total number of students and the total SCORE of students are calculated by the expr command in the while loop. Finally, the average value is calculated and output. To execute this script, you need to redirect the contents of the students.log file to the while2.sh script.
[root@localhost 20190105]# vi while2.sh #! /bin/bash TOTAL=0 /bin/bash TOTAL=0 /bin/bash TOTAL=0 /bin/bash TOTAL=0 AVG= 'expr $TOTAL / $COUNT' echo 'There are '$COUNT' students, the avg score is '$AVG [root@localhost 20190105]# sh while2.sh < students.log There are 4 students , the avg score is 81 [root@localhost 20190105]#Copy the code
6, until loop structure
Until is a loop structure, other than for and while, that executes a series of commands over and over until the condition is true.
6.1 Until loop Syntax
Until condition do command 1 Command 2... doneCopy the code
The until loop reads the user’s input and displays it on the screen, ending the loop when the user enters exit.
[root@localhost 20190105]# vi until1.sh #! /bin/bash XXX ="" # end the loop until ["$XXX "= exit] do # Read the user's input to the ANS variable read XXX # Print the user's input if it is not exit [ "$xxx" != exit ] then echo 'The user input is : Else echo 'Exit the script.' fi done [root@localhost 20190105]# sh until1.sh hello the user input is: hello welcome to HongKong! The user input is : welcome to HongKong! exit Exit the script. [root@localhost 20190105]#Copy the code
Recommended reading
Super hardcore! 11 very useful Python and Shell scripts to use examples!
7 very useful Shell take to use script examples!
Shell programming – condition test | base paper
In this paper, to the end.
Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!
By the way, please give me some free attention! In case you get lost and don’t find me next time.
See you next time!