1.for创建20用户用户前缀由用户输入用户初始密码由用户输入
例如:test01,test10
[root@cotenos day06]# vim useradd.sh
#!/bin/bash
read -p "请输入用户前缀:" user
read -p "请输入初始密码:" pass
for ((i=1;i<=20;i++ ))
do
if [ $i -lt 10 ];then
users=${user}0$i
else
users=${user}$i
fi
if ! id -u $users &> /dev/null
then
useradd $users
echo "$pass" | passwd --stdin $users &> /dev/null
else
echo "$users is exists..."
fi
done
执行:
[root@cotenos day06]# chmod +x useradd.sh
[root@cotenos day06]# ./useradd.sh
2、for ping测试指网段的主机网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 ---192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
创建脚本
[root@cotenos day06]# vim ping_ceshi.sh
#!/bin/bash
read -p "请输入网段: " ip
for ((i=120;i<=130;i++))
do
IP="$ip"."$i"
echo $IP
if ping -c 2 $IP &> /dev/null
then
echo "$IP is up" >>/tmp/host_up.txt
else
echo "$IP is down" >>/tmp/host_down.txt
fi
done
执行结果
[root@cotenos day06]# bash ping_ceshi.sh
请输入网段: 192.168.112
192.168.112.120
192.168.112.121
192.168.112.122
192.168.112.123
^C192.168.112.124
192.168.112.125