Shell之进阶篇(循环语句)


进阶篇(二)
  在Shell脚本中可以使用 for、while、until 语句,相对于其他编程语言,多了一个 until 语句,那它的作用是什么?下面我们来一一验证。

一、for 语句

== for 语法结构 ==

for 变量名 [ in 取值列表 ]
do
	循环体
done

小 实 验一:

实现批量用户创建

#!/bin/bash
##########################
#2020-03-09              #
#Author Zcoder           #
##########################
#password variable
password=123456

read -p "Please input users prefix: " prefix
if [ -z "$prefix"]; then
	echo -e "\e[1;31mThe users prefix error!"
	exit 2
fi

while :
do
	read -p "Please input the number of users suffix you want to create: " number
	#number limit
	if [[ $number =~ ^[1-9]+[0-9]*$ ]]; then
		break
	else
		echo -e "\e[1;31mInput the number error! \e[0m"
	fi
done

for i in `seq -w $number`
do
	users=$prefix$i
    id $users &> /dev/null
    if [ $? -eq 0 ]; then
		echo -e "\e[1;31m $users is existed"
		exit 2
	else
		useradd $users
		echo "$password" |passwd --stdin $users &> /dev/null
		if [ $? -eq 0 ]; then
			echo -e "\e[1;32m $users is created~ \e[0m"
		fi
	fi
done

在shell脚本中第19行使用了正则表达式,判断用户输入的数值是否正确。
[[ =~ ]] 表示正则匹配

小 实 验二:

验证多台服务器网络情况

#!/bin/bash
##########################
#2020-03-09              #
#Author Zcoder           #
##########################

#Create file ping.txt
>ping.txt

#Ip variable
ip=192.168.56.
for i in {1..254}
do
	{
	ping -c1 -W1 $ip$i &> /dev/null
	if [ $? -eq 0 ]; then
		echo "$ip$i network is OK~"|tee -a ping.txt
	fi
	}&
done
wait
echo "Ping test finish..."

二、while 语句

while 语法结构
:循环次数可以不固定;
当条件测试成立(即 条件测试为真),执行循环体。

while 条件测试
do
	循环体
done

小 实 验一:

根据文本文件批量创建用户

#!/bin/bash
##########################
#2020-03-10              #
#Author Zcoder           #
##########################
while read line
do
	if [ ${#line} -eq 0 ]; then
		echo "------------------------"
		continue
	fi
	
	user=`echo $line|awk '{ print $1 }'`
	pass=`echo $line|awk '{ print $2 }'`
	
	id $user &> /dev/null
	if [ $? -eq 0 ]; then
		echo -e "\e[1;31mThe $user already exists! \e[0m"
	else
		useradd $user &> /dev/null
		echo "$pass" |passwd --stdin $user &> /dev/null
		if [ $? -eq 0 ]; then
			echo -e "\e[1;32mThe $user is created~ \e[0m"
		fi
	fi
done < $1

使用方法:

# cat user.txt 
ah 123456

jack 654321
# ./脚本名 user.txt

三、until 语句

until 语法结构
:循环次数可以不固定;
当条件测试不成立(即 条件测试为假),执行循环体。

until 条件测试
do
	循环体
done

While VS Until

#!/bin/bash
##########################
#2020-03-10              #
#Author Zcoder           #
##########################
ip=192.168.56.
i=1
until [ $i -gt 254 ]
do
        {
        ipaddr=$ip$i
        ping -c1 -W1 $ipaddr &> /dev/null
        if [ $? -eq 0 ];then
                echo "$ipaddr up"
        fi
        }&
        let i++
done
wait
echo "all finish..."

下面再来对比一下while循环

#!/bin/bash
##########################
#2020-03-10              #
#Author Zcoder           #
##########################
ip=192.168.56.
i=1
while [ $i -le 254 ]
do
        {
        ipaddr=$ip$i
        ping -c1 -W1 $ipaddr &> /dev/null
        if [ $? -eq 0 ];then
                echo "$ipaddr up"
        fi
        }&
        let i++
done
wait
echo "all finish..."
发布了89 篇原创文章 · 获赞 196 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_40791253/article/details/104741017
今日推荐