Shell scripting application - for, while loop

By Shell Script applications (2) learn to use if conditions such as conditional statements. Shell as a scripting programming language, also containing other process control loop structure, branch, etc., making it easy to complete more complex and powerful. We come to know for, while, the specific application case statement today.

A, for loop

In practice, often encounter cases a task needs to be performed several times, but each time the target is just not the same treatment, the same as other commands. For example: create a list of account numbers according to the system for a contact.

When faced with a variety of repeat list tasks, use an if statement can not meet the need, you need to use the for statement.

1.for sentence structure

When using a for loop, you need to specify a list of possible variables and values, repeat the same sequence of commands for different values, know the value of the variable exhausted exit the loop.

1) for circulating the grammatical structure of the sentence:

for 变量名 in 取值列表
do
  命令序列
done

Sentence structure, for the operation of the sentences for the user to specify the name of the variable, right, and by keyword in this tag is preset to a value separated by spaces list multiple values. Located between commands do ...... done called loop sequence, wherein the variable to execute statements need to be applied to accomplish the task.

2) for loop statement flowchart

Shell scripting application (c)

3) for loop usage scenarios

1. 循环次数未知;
2.完整遍历整个取值列表。

2.for statement Application Examples

要求:
1.根据姓名列表批量创建用户;
2.用户的初始密码是“123456”
实施步骤:
[root@localhost ~]# vim /root/users.txt                 //新建一个取值列表
xiaozhang
xiaoli
xiaowang
xiaosun 
[root@localhost ~]# vim useraddfor.sh               //创建批量创建用户的脚本
#!/bin/bash
ULIST=$(cat /root/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo "123456" | passwd --stdin $UNAME &> /dev/null
done
[root@localhost ~]# sh useraddfor.sh                //执行脚本
[root@localhost ~]# tail -4 /etc/passwd             //验证效果
xiaozhang:x:1001:1001::/home/xiaozhang:/bin/bash
xiaoli:x:1002:1002::/home/xiaoli:/bin/bash
xiaowang:x:1003:1003::/home/xiaowang:/bin/bash
xiaosun:x:1004:1004::/home/xiaosun:/bin/bash  

Note: if statements, for statements and various other shell scripts can be nested statements are used.

Two, while loop

loop suitable for very irregular objects list, and the case where the source list has been fixed, and for controlling the number of cycles required, the operation target numbers in numerical order, the case of performing repeated operations in a particular condition, it is more suitable for use cycle --while statements.

Structure 1.while statement

Use while loop is a sequence of commands can be repeatedly performed in accordance with specific conditions, this condition is not satisfied so far known. In the script application, you should try to avoid the emergence of an infinite loop, or behind the command operation can not be performed. Thus, the command sequence in the loop body should include statements modified test conditions, test conditions so that the appropriate time is no longer established, thereby ending the cycle.

1) grammatical structure while loop statement

while 条件测试操作
do
命令序列
done

2) while the flowchart loop

Shell scripting application (c)
When using the while loop, there are two special test operation conditions. That is true (true) and false (false). Use as a true condition, the condition is always represented established body of the loop will be infinite sequence of commands to perform it, unless forced to terminate script (or use the exit statement exits the script); on the contrary, if the use of false As a condition, the loop will not be executed.

3) while loop usage scenarios

1.循环次数已知;
2.必须有一个可以控制循环变量的语句。

2.while statement Application Examples

要求:
批量创建有规律编号的用户
实施步骤:
[root@localhost ~]# vim useraddwhile.sh
#!/bin/bash
PREFIX="stu"
i=1
while
[ $i -le 10 ]
do
useradd ${PREFIX}$i
echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null
let i++
done
[root@localhost ~]# sh useraddwhile.sh 
[root@localhost ~]# tail /etc/passwd
stu1:x:1005:1005::/home/stu1:/bin/bash
stu2:x:1006:1006::/home/stu2:/bin/bash
stu3:x:1007:1007::/home/stu3:/bin/bash
stu4:x:1008:1008::/home/stu4:/bin/bash
stu5:x:1009:1009::/home/stu5:/bin/bash
stu6:x:1010:1010::/home/stu6:/bin/bash
stu7:x:1011:1011::/home/stu7:/bin/bash
stu8:x:1012:1012::/home/stu8:/bin/bash
stu9:x:1013:1013::/home/stu9:/bin/bash
stu10:x:1014:1014::/home/stu10:/bin/bash

Circulating inside of the body, by the statement "let i ++" (i = equivalent expr $i + 1) to the value of the variable i by 1 until cycle until the condition is satisfied.

Three, case branching statements

Structure 1.case statement

1) the grammatical structure of the sentence case

case  变量值  in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
 ……
* )
默认命令序列
esac

2) case statement flowchart

Shell scripting application (c)

Use case branching statements, several noteworthy features:
Shell scripting application (c)

3) case statement usage scenarios

There are many values ​​of a variable, the need performed separately for each different value of the command sequence which, in this case, the multi-branch if statement is very similar, except that if statement is necessary to determine a plurality of different conditions, and case statement just judge the different values ​​of a variable.

Application examples 2.case statement

[root@localhost ~]# vim hitkey.sh 
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" KEY
case "$KEY" in
[a-z]|[A-Z])
echo "你输入的是字母 $KEY"
;;
[0-9])
echo "你输入的是数字 $KEY"
;;
*)
echo "你输入的是非法字符 $KEY"
esac
[root@localhost ~]# sh hitkey.sh 
请输入一个字符,并按Enter键确认:1
你输入的是数字 1
[root@localhost ~]# sh hitkey.sh 
请输入一个字符,并按Enter键确认:w
你输入的是字母 w
[root@localhost ~]# sh hitkey.sh 
请输入一个字符,并按Enter键确认:@
你输入的是非法字符 @

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160120.htm