In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Through the Shell script application (2) learned the use of if conditional statements and so on. As a scripting language, Shell also contains loops, branches and other program control structures, so that it can easily complete more complex and powerful functions. Today we will look at the specific application of for, while, and case statements. I. for Loop statement
In practical work, we often encounter the situation that a task needs to be executed many times, but each execution only deals with different objects and other commands are the same. For example, create a system account according to the list of names in the address book.
When you repeat work tasks in the face of various lists, the use of if statements can no longer meet the needs, so you need to use for statements.
1.for statement structure
When using the for loop statement, you need to specify a variable and a list of possible values, and repeat the same command sequence for different values until the variable value runs out of the loop.
1) the syntax structure of the for loop statement:
For variable name in value list
Do
Command sequence
Done
In the statement structure, the operation object of the for statement is the variable specified by the user bar, and a list of values is pre-set for the variable through the in keyword, and multiple values are separated by spaces. Located in do. The sequence of commands between done is called loop body, in which the execution statement needs to apply variables to complete the corresponding task.
2) flow chart of for loop statement
3) the usage scenario of for loop statement
1. Number of cycles unknown
two。 Complete traversal of the entire list of values.
Application example of 2.for statement
Request:
1. Create users in batches based on the list of names
two。 The user's initial password is "123456"
Implementation steps:
[root@localhost ~] # vim / root/users.txt / / create a value list xiaozhangxiaolixiaowangxiaosun [root@localhost ~] # vim useraddfor.sh / / create a script for creating users in batches #! / bin/bashULIST=$ (cat / root/users.txt) for UNAME in $ULISTdouseradd $UNAMEecho "123456" | passwd-- stdin $UNAME & > / dev/nulldone [root@localhost ~] # sh useraddfor.sh / / execute the script [root@localhost ~] # tail-4 / etc/passwd / / verify the effect Fruit xiaozhang:x:1001:1001::/home/xiaozhang:/bin/bashxiaoli:x:1002:1002::/home/xiaoli:/bin/bashxiaowang:x:1003:1003::/home/xiaowang:/bin/bashxiaosun:x:1004:1004::/home/xiaosun:/bin/bash
Note: if statements, for statements, and various other shell script statements can be nested.
II. While Loop statement
The for loop statement is very suitable for situations where the list object is irregular and the list source is fixed, but it is more suitable to use the while loop statement when it is required to control the number of loops, the operands are numbered in numerical order, and repeat operations are performed according to specific conditions.
The structure of the 1.while statement
Using the while loop statement, you can repeatedly execute a sequence of commands based on a specific condition until the condition is no longer satisfied. In the application of scripts, the dead loop should be avoided as far as possible, otherwise subsequent command operations will not be executed. Therefore, the sequence of commands in the loop should include statements that modify the test condition so that the test condition is no longer valid at the appropriate time, thus ending the loop.
1) the grammatical structure of while loop statement
While conditional test operation
Do
Command sequence
Done
2) flow chart of while loop statement
When using while loop statements, there are two special conditional test actions. That is, true (true) and false (false). When using true as a condition, it means that the condition is always true, and the sequence of commands in the loop will be executed indefinitely unless the script is forced to terminate (or exit the script using the exit statement); otherwise, if false is used as the condition, the loop body will not be executed.
3) the usage scenario of while loop statement
1. Number of cycles known
two。 There must be a statement that can control the loop variable.
Application example of 2.while statement
Request:
Create regular numbered users in batch
Implementation steps:
[root@localhost ~] # vim useraddwhile.shemale binbinBash PREFIX = "stu" i=1while [$I-le 10] douseradd ${PREFIX} $iecho "123456" | passwd-- stdin ${PREFIX} $I & > / dev/nulllet i++done [root@localhost ~] # sh useraddwhile.sh [root@localhost ~] # tail / etc/passwdstu1:x:1005:1005::/home/stu1:/bin/bashstu2:x:1006:1006::/home/stu2:/bin/bashstu3:x:1007:1007::/home / stu3:/bin/bashstu4:x:1008:1008::/home/stu4:/bin/bashstu5:x:1009:1009::/home/stu5:/bin/bashstu6:x:1010:1010::/home/stu6:/bin/bashstu7:x:1011:1011::/home/stu7:/bin/bashstu8:x:1012:1012::/home/stu8:/bin/bashstu9:x:1013:1013::/home/stu9:/bin/bashstu10:x:1014:1014::/home/stu10:/bin/bash
Inside the body of the loop, the value of the variable I is increased by 1 by the statement "let I +" (equivalent to i=expr $I + 1) and loops until the condition is met.
3. The structure of case branch statement 1.case statement
1) the syntax structure of case statement
Case variable value in
Mode 1)
Command sequence 1
Mode 2)
Command sequence 2
……
*)
Default command sequence
Esac
2) case statement flow chart
Using case branch statements are several noteworthy features:
Application of Shell script (3)
3) the usage scenario of case statement
There are multiple values for a variable, and a different command sequence needs to be executed for each of them, which is very similar to a multi-branch if statement, except that the if statement needs to judge several different conditions, while the case statement only judges the different values of a variable.
An example of the application of 2.case statements [root@localhost ~] # vim hitkey.shallop bind bind Bash read-p "Please enter a character and press enter to confirm:" KEYcase "$KEY" in [Amurz] | [Amurz]) echo "you entered the letter $KEY";; [0-9]) echo "you entered the number $KEY" *) echo "you entered the illegal character $KEY" esac [root@localhost ~] # sh hitkey.sh, please enter a character, and press enter to confirm: 1 you entered the number 1 [root@localhost ~] # sh hitkey.sh, please enter a character, and press enter to confirm: W you entered the letter w [root@localhost ~] # sh hitkey.sh please enter a character, and press enter to confirm: @ you entered the illegal character @
Original address: https://www.linuxprobe.com/shell-for-while.html
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.