shell脚本应用(二)

for循环
1、使用for批量化创建用户
[root@centos01 ~]#vim user.txt
bob
tom
alice
2、编写脚本添加账户
[root@centos01 ~]# vim useradd.sh
#!/bin/bash
user=$(cat /root/user.txt)
for username in $user
do
useradd $username
echo “123456” | passwd --stdin $username & > /dev/null
done

[root@centos01 ~]# chmod +x useradd.sh
[root@centos01 ~]# ./useradd.sh
3、查看
[root@centos01 ~]# tail -3 /etc/passwd
bob❌1000:1000:bob:/home/bob:/bin/bash
tom❌1001:1001::/home/tom:/bin/bash
alice❌1002:1002::/home/alice:/bin/bash
4、批量化删除账户
[root@centos01 ~]# vim useradd.sh
#!/bin/bash
user=$(cat /root/user.txt)
for username in $user
do
userdel -r $username
done

[root@centos01 ~]# ./useradd.sh
5、查看
[root@centos01 ~]# tail -3 /etc/passwd
postfix❌89:89::/var/spool/postfix:/sbin/nologin
sshd❌74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump❌72:72:😕:/sbin/nologin
for脚本ping主机
1、for脚本ping主机
[root@centos01 ~]# cat add.txt
192.168.100.10
192.168.100.20
192.168.100.254
2、编写脚本
[root@centos01 ~]# cat ping.sh
#!/bin/bash
host=$(cat /root/add.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
[root@centos01 ~]# chmod +x ping.sh
执行脚本
[root@centos01 ~]# ./ping.sh
host 192.168.100.10 is UP!!!
host 192.168.100.20 is DOWN!!!
host 192.168.100.254 is DOWN!!!
While循环
1、使用while随机创建账户名字user密码设置123456
[root@centos01 ~]# cat while.sh
#!/bin/bash
prefix=“user”
i=1
while [ $i -le 20 ]
do
useradd p r e f i x {prefix} prefixi
echo “123456” | passwd -stdin p r e f i x {prefix} prefixi &>/dev/null
let i++
done
[root@centos01 ~]# chmod +x while.sh
2、运行脚本并查看
[root@centos01 ~]# ./while.sh
[root@centos01 ~]# tail /etc/passwd
user11❌1010:1010::/home/user11:/bin/bash
user12❌1011:1011::/home/user12:/bin/bash
user13❌1012:1012::/home/user13:/bin/bash
user14❌1013:1013::/home/user14:/bin/bash
user15❌1014:1014::/home/user15:/bin/bash
user16❌1015:1015::/home/user16:/bin/bash
user17❌1016:1016::/home/user17:/bin/bash
user18❌1017:1017::/home/user18:/bin/bash
user19❌1018:1018::/home/user19:/bin/bash
user20❌1019:1019::/home/user20:/bin/bash
3、编辑删除用户的脚本
[root@centos01 ~]# cat while.sh
#!/bin/bash
prefix=“user”
i=1
while [ $i -le 20 ]
do
userdel p r e f i x {prefix} prefixi
let i++
done
4、运行脚本查看
[root@centos01 ~]# ./while.sh
[root@centos01 ~]# tail -3 /etc/passwd
postfix❌89:89::/var/spool/postfix:/sbin/nologin
sshd❌74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump❌72:72:😕:/sbin/nologin
5、创建猜数字游戏
[root@centos01 ~]# cat yx.sh
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo “商品实际价格范围为0-999,猜猜看是多少?”
while true
do
read -p “请输入你猜测的价格数目:” INT
let TIMES++
if [ $INT -eq $PRICE ]
then
echo “恭喜你答对了,实际价格是 $PRICE”
echo “你总共猜测了 $TIMES 次”
exit 0
elif [ $INT -gt $PRICE ]
then
echo “太高了!”
else
echo “太低了!”
fi
done
[root@centos01 ~]# chmod +x yx.sh
6、运行脚本
[root@centos01 ~]# ./yx.sh
商品实际价格范围为0-999,猜猜看是多少?
请输入你猜测的价格数目:500
太低了!
请输入你猜测的价格数目:750
太高了!
请输入你猜测的价格数目:650
太低了!
请输入你猜测的价格数目:700
太低了!
请输入你猜测的价格数目:725
太高了!
请输入你猜测的价格数目:720
太高了!
请输入你猜测的价格数目:710
太高了!
请输入你猜测的价格数目:709
太高了!
请输入你猜测的价格数目:705
太高了!
请输入你猜测的价格数目:702
恭喜你答对了,实际价格是 702
你总共猜测了 10 次

检查用户输入的字符类型
1、编写脚本
[root@centos01 ~]# vim hitkey.sh
#!/bin/bash
read -p “请输入一个字符,并按Enter键确认:” KEY
case “$KEY” in
[a-z] | [A-Z])
echo “您输入的是 字母.”
;;
[0-9])
echo “您输入的是 数字.”
;;
*)
echo “您输入的是 空格、功能键或其他控制字符.”
esac
[root@centos01 ~]# chmod +x hitkey.sh
2、运行脚本
[root@centos01 ~]# ./hitkey.sh
请输入一个字符,并按Enter键确认:k
您输入的是 字母.
[root@centos01 ~]# ./hitkey.sh
请输入一个字符,并按Enter键确认:8
您输入的是 数字.
[root@centos01 ~]# ./hitkey.sh
请输入一个字符,并按Enter键确认:_
您输入的是 空格、功能键或其他控制字符.
编写系统服务脚本
1、编写脚本
[root@centos01 ~]# cat fw.sh
#!/bin/bash
#chkconfig:35 90 21
#Description:test
case “$1” in
start)
echo “正在启动”
;;
stop)
echo “正在停止”
;;
restart)
echo “正在重启”
;;
*)
echo “用法$0{start|stop|restart}”
;;
Esac
[root@centos01 ~]# chmod +x fw.sh
2、运行脚本
[root@centos01 ~]# ./fw.sh start
正在启动
[root@centos01 ~]# ./fw.sh stop
正在停止
[root@centos01 ~]# ./fw.sh restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值