Shell编程之二:for条件语句、while条件语句及case条件语句

Shell编程之二:for条件语句、while条件语句及case条件语句
都是实际测试过的可用脚本。shell编程确实不容易,需要持之以恒的学习和总结。
七、批量创建用户名及密码
[root@bogon ~]# cat users.txt
andy
tom
jack
rose

[root@bogon ~]# cat users passwd.txt
andy: 123456
tom: 123456
jack: 123456
rose: 123456

Shell编程之二:for条件语句、while条件语句及case条件语句_第1张图片
脚本显示了一些乱码,只能贴图了。

解释:for语法:for 变量名 in 取值列表----do ----命令序列----done。
针对上述例子解释就是:for 用户名 in 列表文件—do----创建用户及密码----done

for语句允许脚本一次性读取多个信息,然后逐一对信息进行操作。
for user in就是逐一给user赋值users.txt里的内容。
新增系统变量$user
echo “新密码”|passwd --stdin 用户名,passwd --stdin选项用于从标准输入管道读入新的密码。
chpasswd命令是批量更新用户口令的工具,把一个文件内容重新定向添加到/etc/shadow中。
/etc/passwd文件中的每个用户都有一个对应的记录行,记录着这个用户的一下基本属性。该文件对所有用户可读。
而/etc/shadow文件是passwd文件的一个影子,/etc/shadow文件中的记录行与/etc/passwd中的一一对应,
它由pwconv命令根据/etc/passwd中的数据自动产生。但是/etc/shadow文件只有系统管理员才能够进行修改和查看。

结果:
[root@bogon ~]# sh test07.sh
Changing password for user andy.
passwd: all authentication tokens updated successfully.
Password written successfully
Changing password for user tom.
passwd: all authentication tokens updated successfully.
Password written successfully
Changing password for user jack.
passwd: all authentication tokens updated successfully.
Password written successfully
Changing password for user rose.
passwd: all authentication tokens updated successfully.
Password written successfully

[root@bogon ~]# cat /etc/shadow
-----省略输出内容—
andy: 1 1 1X5jFvmYb$i4SOFs5HrdDk2sZn5ZGnY1:18373:0:99999:7:::
tom:$1 5 C N G Q o 4 l 5CNGQo4l 5CNGQo4lcsPAWqk4lr5qD9RdtiRHs/:18373:0:99999:7:::
jack:$1 9 d m N . n r T 9dmN.nrT 9dmN.nrTthgWF1MVatnaS1pbv4Ber0:18373:0:99999:7:::
rose: 1 1 1u2EsSqDn$QajmDsP2eC8XSyH9S74Fx0:18373:0:99999:7:::

八、批量ping多个不同的IP地址
[root@bogon ~]# cat ipaddress.txt
192.168.137.1
192.168.137.2
192.168.137.3
192.168.137.129
8.8.8.8
144.144.144.144

[root@bogon ~]# vim checkhost.sh
#!/bin/bash
#by authos herrychen
HLIST=$(cat ~/ipaddress.txt)
for IP in $HLIST
do
ping -c 3 -i 0.3 -W 3 $IP &>/dev/null
if [ $? -eq 0 ];then
echo “Host $IP is on-line”
else
echo “Host $IP is off-line”
fi
done

解释:上一篇博客文章已经解释过了。
结果:
[root@bogon ~]# sh checkhost.sh
Host 192.168.137.1 is on-line
Host 192.168.137.2 is on-line
Host 192.168.137.3 is off-line
Host 192.168.137.129 is off-line
Host 8.8.8.8 is on-line
Host 144.144.144.144 is off-line

九、猜数字
[root@bogon ~]# vim test09.sh
#!/bin/bash
#by authos herrychen
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo “The actual price of goods is between 0 and 999,Guess what it is?”
while true
do
read -p “Please enter your guess price:” INT
let TIMES++
if [ $INT -eq $PRICE ]; then
echo “Congratulations, you’re right”
echo “You guessed $TIMES in all”
exit 0
elif [ $INT -gt $PRICE ]; then
echo “It’s too high!”
else
echo “It’s too low!”
fi
done

解释:
while语法:while 条件测试操作—do----命令序列----done
针对上述例子解释就是:while 未猜中正确价格—do----反复猜价格----done。

使用$RANDOM变量来调取一个随机的数值(范围为0-32767), 将这个随机数对1000进行取余操作,
并使用expr命令取出其结果,再用这个数值与read命令输入的值进行比较,
let TIMES++,对变量TIMES进行累加操作。
-eq为是否等于,-gt为是否大于。
要关注的是while语句的条件测试始终为true, 因此判断语句会无限执行下去,直到用户输入的值等于expr命令取出的值。

结果:
[root@bogon log]# sh test09.sh
The actual price of goods is between 0 and 999,Guess what it is?
Please enter your guess price:500
It’s too high!
Please enter your guess price:200
It’s too high!
Please enter your guess price:1
It’s too low!
Please enter your guess price:100
It’s too high!
Please enter your guess price:90
It’s too high!
Please enter your guess price:80
It’s too high!
Please enter your guess price:60
It’s too low!
Please enter your guess price:70
It’s too low!
Please enter your guess price:71
Congratulations, you’re right
You guessed 9 in all

十、统计/var/log 有多少个文件,并显示这些文件名
Shell编程之二:for条件语句、while条件语句及case条件语句_第2张图片

结果:
[root@bogon log]# sh test10.sh
file name:yum.log
file name:Xorg.0.log
file name:wtmp
file name:wpa_supplicant.log
file name:vmware-vmsvc.log
file name:vmware-vgauthsvc.log.0
file name:vmware-network.log
file name:vmware-network.1.log
-----省略部分输出信息-----
The total number of files is:22

十一、
[root@bogon log]# vim test11.sh
#!/bin/bash
read -p “press some key ,then press return :” KEY
case $KEY in
[a-z]|[A-Z])
echo “It’s a letter.”
;;
[0-9])
echo “It’s a digit.”
;;
*)
echo “It’s function keys、Spacebar or other ksys.”
esac

解释:
case 语句,只能判断一种条件关系,而if可以判断多种条件关系。
case $变量名 in
“值1”)
如果变量的值等于值1,则执行程序1
;;
“值2”)
如果变量的值等于值2,则执行程序2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
针对上述例子解释就是:case 输入的字符 in [a-z]|[A-Z]就提示为字母,----;;
----[0-9]就提示为数字,—;; — *)提示为特殊字符,—esac

结果:
[root@bogon ~]# sh test11.sh
press some key ,then press return :5
It’s a digit.
[root@bogon ~]# sh test11.sh
press some key ,then press return
It’s a letter.
[root@bogon ~]# sh test11.sh
press some key ,then press return :^
It’s function keys、Spacebar or other ksys.

十二、
#!/bin/bash
echo “What is your preferred scripting language?”
read -p “1)bash 2)perl 3)python 4)ruby:” lang
case $lang in
1) echo “You selected bash” ;;
2) echo “You selected perl” ;;
3) echo “You selected python” ;;
4) echo “You selected ruby” ;;
*) echo “I do not know!” ;;
esac

结果
[root@bogon ~]# sh test12.sh
What is your preferred scripting language?
1)bash 2)perl 3)python 4)ruby:3
You selected python
[root@bogon ~]# sh test12.sh
What is your preferred scripting language?
1)bash 2)perl 3)python 4)ruby:5
I do not know!

你可能感兴趣的:(笔记)