linux脚本let命令,shell脚本快速⼊门之-----循环(for、while、
until)
⽂章⽬录
⼀、for循环
1、for循环语句
2、使⽤for批量添加⽤户
3、⽰例–根据IP地址检查主机状态
4、⽤循环语句输出1-10的数字的四种办法
5、shell中let命令
6、计算1-10偶数/奇数的和
⼆、while循环语句
1、while语句结构
2、while语句应⽤⽰例
3、while死循环
4、使⽤while批量添加⽤户
5、猜商品价格游戏
三、untli循环语句
1、until语句的结构
2、until语句应⽤1
3、until语句应⽤2
⼀、for循环
1、for循环语句
for语句结构
语句结构
for 变量名 in 取值列表
do
命令序列
done
语句结构举例
for 收件⼈ in 邮件地址列表
do
发送邮件
done
2、使⽤for批量添加⽤户
⽤户名存放在⽂件中,每⾏⼀个
初始密码均设为123456
[root@localhost opt]#
zhangsan
lisi
wangermazi
zhaoliu
tianji
wangba
[root@localhost opt]# vim useradd.sh
#!/bin/bash
Lists=$(cat /)
for bb in $Lists
do
useradd $bb
echo "123456" | passwd --stdin $bb
echo "$bb添加成功"
done
[root@localhost opt]# chmod +x useradd.sh
[root@localhost opt]# ./useradd.sh
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully. zhangsan添加成功
useradd: warning: the home directory already exists. Not copying any file from skel directory into it.
Creating mailbox file: File exists
Changing password for user lisi.
passwd: all authentication tokens updated successfully. lisi添加成功
Changing password for user wangermazi.
passwd: all authentication tokens updated successfully. wangermazi添加成功
Changing password for user zhaoliu.
passwd: all authentication tokens updated successfully. zhaoliu添加成功
Changing password for user tianji.
passwd: all authentication tokens updated successfully. tianji添加成功
Changing password for user wangba.
passwd: all authentication tokens updated successfully. wangba添加成功
[root@localhost opt]# cat /etc/passwd | grep "bash" root:x:0:0:root:/root:/bin/bash
dd:x:1000:1000:cn-tangzheng:/home/dd:/bin/bash zhangsan:x:1001:1001::/home/zhangsan:/bin/bash lisi:x:1002:1002::/home/lisi:/bin/bash
wangermazi:x:1003:1003::/home/wangermazi:/bin/bash zhaoliu:x:1004:1004::/home/zhaoliu:/bin/bash
tianji:x:1005:1005::/home/tianji:/bin/bash
wangba:x:1006:1006::/home/wangba:/bin/bash
脚本可以适当修改优化:如
echo "123456" | passwd --stdin $bb &> /dev/null
批量删除⽤户:在刚刚的基础上修改
[root@localhost opt]# vim useradd.sh
#!/bin/bash
Lists=$(cat /)
for bb in $Lists
do
userdel -r $bb'-r 删除家⽬录'
#echo "123456" | passwd --stdin $bb &> /dev/null
echo "$bb删除成功"
done
[root@localhost opt]# ./useradd.sh
linux命令及shell编写zhangsan删除成功
lisi删除成功
wangermazi删除成功
zhaoliu删除成功
tianji删除成功
wangba删除成功
[root@localhost opt]# cat /etc/passwd | grep "bash"
root:x:0:0:root:/root:/bin/bash
dd:x:1000:1000:cn-tangzheng:/home/cn-tangzheng:/bin/bash 3、⽰例–根据IP地址检查主机状态
检测IP地址192.168.197.0-192.168.197.10段落
使⽤ping命令检测各主机的连通性
[root@localhost opt]# vim ipchk.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
ping -c 3 -i 0.2 -W 3 "192.168.197.$i" &> /dev/null
if [ $? -eq 0 ]
then
echo "Host 192.168.197.$i is up"
else
echo "Host 192.168.197.$i is down"
fi
done
[root@localhost opt]# ./ipchk.sh
Host 192.168.197.1 is up
Host 192.168.197.2 is up
Host 192.168.197.3 is down
Host 192.168.197.4 is down
Host 192.168.197.5 is down
Host 192.168.197.6 is down
Host 192.168.197.7 is down
Host 192.168.197.8 is down
Host 192.168.197.9 is down
Host 192.168.197.10 is down
4、⽤循环语句输出1-10的数字的四种办法⽅式⼀:
#!/bin/bash
for ((i=1;i<=10;i++))
do
echo "$i"
done
⽅式⼆:
#!/bin/bash
i=1
for ((;i<=10;i++))
do
echo "$i"
done
⽅式三:
#!/bin/bash
i=1
for ((;i<=10;))
do
echo "$i"
let i++
done
⽅式四:
#!/bin/bash
i=1
for ((;;))
do
if [ $i -le 10 ]
then
echo "$i"
let i++
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论