Shell(三)
一.for循环、
1.创建取值列表
[root@centos ~]# cat ./user.txt
bob
tom
alice
2.创建用户脚本
[root@centos ~]# cat ./useradd.sh
#!/bin/bash
user=$(cat /root/user.txt)
for user in $user
do
useradd $user
echo “pwd@123” | passwd --stdin $user &> /dev/null
done
给权限
[root@centos ~]# chmod +x useradd.sh
验证
使用for命令ping测试主机
(1)
[root@centos ~]# cat ./ping.sh
#!/bin/bash
read -p “请输入IP地址:” host
for ip in $host
do
ping -c 3 -i 0.3 -w 3 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo “host $ip is up!!”
else
echo “host $ip is down!!”
fi
done
给权限
[root@centos ~]# chmod +x ping.sh
验证
(2)
[root@centos ~]# cat 1.txt
192.168.100.10
192.168.100.20
192.168.100.30
[root@centos ~]# cat ./ping.sh
#!/bin/bash
host=$(cat /root/1.txt)
for ip in $host
do
ping -c 3 -i 0.3 -w 3 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo “host $ip is up!!”
else
echo “host $ip is down!!”
fi
done
验证
二.while循环
1.while循环损及创建用户
[root@centos ~]# cat 123.sh
#!/bin/bash
prefix=stu
i=1
while [ $i -le 20 ]
do
useradd
p
r
e
f
i
x
{prefix}
prefixi
echo “pwd@123” | passwd --stdin
p
e
r
f
i
x
{perfix}
perfixi &> /dev/null
let i++
done
给权限
[root@centos ~]# chmod +x 123.sh
验证
三.case控制计算机服务
1.控制vsftpd脚本
给权限验证
2.判断用户输入内容
[root@centos ~]# cat 999.sh
#!/bin/bash
read -p “请输入键盘内容:” key
case “$key” in
[a-z] | [A-Z])
echo “你输入的是字母”
;;
[0-9])
echo “你输入的是数字”
;;
*)
echo “你输入的是符号”
esac
给权限
[root@centos ~]# chmod +x 999.sh
验证