Shell腳本(三)
一、使用for回圈
1.使用for 回圈創建用戶
[root@centos01 ~]# vim username.sh
#!/bin/bash
user=$(cat ./username.txt)
for username in $user
do
useradd $username
echo "pwd@123" | passwd --stdin $username &> /dev/null
done

2.使用for回圈洗掉用戶
[root@centos01 ~]# vim deluser.sh
#!/bin/bash
user=$(cat ./username.txt)
for username in $user
do
userdel -r $username
done

3.使用for回圈測驗網路連通性
[root@centos01 ~]# vim ping.sh
#!/bin/bash
read -p "請輸入指定的IP地址或主機名:" host
for ip in $host
do
ping -c 3 -i 0.2 -w 3 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo "host $ip 主機開機!!"
else
echo "host $ip 主機關機!!"
fi
done
二、while回圈
1.while回圈創建用戶
[root@centos01 ~]# vim whileadduser.sh
#!/bin/bash
prefix=stu
i=1
while [ $i -le 20 ]
do
useradd ${prefix}$i
echo "pwd@123" | passwd --stdin ${prefix}$1 &> /dev/null
let i++
done

增加執行權限
[root@centos01 ~]# chmod +x whileadduser.sh
執行
[root@centos01 ~]# ./whileadduser.sh
2.while回圈洗掉用戶
[root@centos01 ~]# cp whileadduser.sh whiledeluser.sh
[root@centos01 ~]# vim whiledeluser.sh
#!/bin/bash
prefix=stu
i=1
while [ $i -le 20 ]
do
userdel -r ${prefix}$i
let i++
done

[root@centos01 ~]# chmod +x whiledeluser.sh
[root@centos01 ~]# ./whiledeluser.sh
四、case服務控制
1.撰寫控制vsftpd
[root@centos01 ~]# vim vsftpd.sh
#!/bin/bash
#chkconfig:35 21 80
#description:vsftpd server
case "$1" in
start)
echo "正在啟動vsftpd服務[確認]"
;;
stop)
echo "正在停止vsftpd服務[確認]"
;;
restart)
echo "正在重啟vsftpd服務[確認]"
;;
*)
echo "使用語法:$0{ start | restart | stop }"
esac

[root@centos01 ~]# chmod +x vsftpd.sh
停止服務
[root@centos01 ~]# ./vsftpd.sh stop

啟動服務
[root@centos01 ~]# ./vsftpd.sh start

重啟服務
[root@centos01 ~]# ./vsftpd.sh restart

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/218845.html
標籤:其他
上一篇:linux 監控cpu、磁盤和記憶體的shell腳本并寫成定時任務
下一篇:Ansible自動化運維(1)
