Shell script for, while loop statement and case branch statement

One, the structure of the for statement

(1) The principle of for statement execution:

Read different variable values ​​to execute the same set of commands one by one
— Format:
for variable name in value list
do
command sequence
done
— example:
for recipient in mail address list
do
  send mail
done

(2) Application example of for statement

[root@localhost ~]# cat <<EOF > /root/users.txt   
 `(输入EOF后回车直接往文档写入,回车可以空到下一行,结尾用EOF结束)`
> zhangsan
> lisi
> wangwu
> EOF
————批量添加用户
`***用户名存放在users.txt文件中,每行一个`
[root@localhost ~]# cat /root/users.txt
chenye
dengchao
zhangjie
`***初始密码均设为123456`
[root@aaa ~]# vim a.sh
#!/bin/bash
A=$(cat /root/users.txt)
for B in $A
do
        useradd $B &> /dev/null
        echo "123456" | passwd --stdin $b &> /dev/null
        echo "用户 $B 已创建并设置初始密码"
done
[root@localhost ~]# chmod +x a.sh
[root@localhost ~]# ./a.sh
[root@localhost ~]# tail -3 /etc/passwd
chenye:x:1011:1011::/home/chenye:/bin/bash
dengchao:x:1012:1012::/home/dengchao:/bin/bash
zhangjie:x:1013:1013::/home/zhangjie:/bin/bash
`可以登录用户验证密码是否为123456`

Second, the structure of the while statement

(1) The principle of while statement execution:

Test a certain condition repeatedly, and execute it repeatedly as long as the condition is established
—format:
while conditional test operation
do
command sequence
done
— example:
while not guessing the correct price
do
repeatedly guessing the commodity price
done

(2) Application example of while statement

————批量添加用户
***用户名称以stu开头,按数字顺序进行编号
***一共添加20个用户,即stu1、stu2、……、stu20
***初始密码均设为123456
[root@localhost ~]# cat B.sh
#!/bin/bash
A="stu"
B=1
`(循环条件:序号<=20)`
while [ $B -le 20 ]
do
    useradd ${A}$B
    echo "123456" | passwd --stdin ${A}$B&> /dev/null
    let B++
    `(序号递增,避免死循环)`
done
[root@localhost ~]#chmod +x B.sh
[root@localhost ~]# ./B.sh   
[root@localhost ~]# grep "stu" /etc/passwd | tail -3
stu18:x:1028:1028::/home/stu18:/bin/bash
stu19:x:1029:1029::/home/stu19:/bin/bash
stu20:x:1030:1030::/home/stu20:/bin/bash
`true 真,他可以借助此命令达到 死循环的作用,将命令永远的执行下去`

Three, the structure of the case statement

(1) The principle of case statement execution:

For different values ​​of variables, execute different command sequences
—format:
case variable value in
mode 1)
command sequence 1
;;
mode 2)
command sequence 2
;;
 ……
*) ( “ *)” 即结尾,*和)之间有空格,)要用英文的)
Default command sequence
esac —Example
:(服务启动脚本格式)
case control command in
start)
start XX service
;;
stop)
stop XX service
;;
 ……
*) (中间有空格)
show usage of service script
esac

  • Regular expression
    [] —— Value range
    | —— Logical OR

(2) Case statement application example

————击键类型识别
***提示用户输入一个字符
***判断出该字符是字母、数字或者其他字符
[root@localhost ~]# cat C.sh
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" A
case "$A" in
  [a-z]|[A-Z])  `(匹配小写或大写字母)`
      echo "您输入的是 字母 $A。"
      ;;
  [0-9])  `(匹配数字0-9)`
      echo "您输入的是 数字 $A。"
      ;;
  *)       `(如果都不匹配则)`
      echo "您输入的是 空格、功能键或其他控制字符。"
esac
[root@localhost ~]# ./C.sh (给可执行权限)
请输入一个字符,并按Enter键确认:k
您输入的是 字母 k 。
[root@localhost ~]# ./C.sh
请输入一个字符,并按Enter键确认:8
您输入的是 数字 8 。
[root@localhost ~]# ./C.sh
请输入一个字符,并按Enter键确认:^[[19~
【按功能键F8】
您输入的是 空格、功能键或其他控制字符。

**写脚本的时候,所有的 )、;之类的都要使用英文的,如果使用xshell远程使用centos的话,一定要看好输入法之类的**

Guess you like

Origin blog.csdn.net/rzy1248873545/article/details/110375091