シェル(2)のwhileステートメント、untilステートメントの判断ステートメントをループします

whileステートメント
1。機能:条件がtrueの場合はループに入り、条件がfalseの場合はループを終了します。

2.文法構造:

   while   表达式
    do
          command...
    done

はじめに:

1〜50の偶数を計算し
ここに写真の説明を挿入
ここに写真の説明を挿入
、ステートメント
1まで。機能:条件が満たされない限り、whileの反対であり、ループを続けます(繰り返し失敗します)
。2。ステートメントの構造:

   until expression 
    do
          command
    done

はじめに:

1〜50の偶数の合計を計算します

ここに写真の説明を挿入
ここに写真の説明を挿入
実際の戦闘スクリプト
演習1:30秒ごとにシステム時間を同期するスクリプトを作成します。同期が失敗した場合は、電子メールアラームが送信されます。同期が成功した場合は、通知のために100回ごとに電子メールが送信されます。
ここに写真の説明を挿入
演習2:nfsサービスを自動的に構築するスクリプトを作成する

#!/bin/bash
echo -e  "\033[32m######testing the network.......\033[0m"
ping -c1 -w1 172.25.254.6 &>/dev/null
	if [ $? -eq 0 ]
	then
		echo  -e "\033[32mthe network is ok !!\033[0m"
	else
		echo  -e "\033[31mthe network is not running!!\033[0m"
		exit
	fi

echo -e "\033[32m########turnning down the selinux.......\033[0m"
setenforce 0 &>/dev/null
	if [$? -eq 0 ]
	then
		echo  -e "\033[32mselinux is turning down\033[0m"
	fi
systemctl stop firewalld &>/dev/null
	if [ $? -eq 0 ]
	then
		echo -e "\033[32mfirewall is stoped"
	fi
echo -e "\033[32m########testing the software......\033[0m"
rpm -q rpcbind &>/dev/null
	if [ $? -eq 0 ]
	then
		echo "rpcbind is already installed"
     	else
		echo "rpcbind is not installed"
dnf install rpcbind -y
        if [ $? -eq 0 ];then
                         echo "rpcbind is now installed"
        else
                         echo "rpcbind install failed"   
                         exit 
                 fi
             exit
	fi
echo -e "\033[32m#######mkdir dir network chmod .......\033[0m"
read -p "Please input share dir:  " dir
[ -e $dir ] && {
        echo "the dir exists"
}||{
        mkdir -p $dir
        echo "$dir create success"
        exit
}
chmod 1777 $dir
read -p "please input the subnet to share: " subnet
read -p "please input share permission: " permission
 
echo -e "\033[32medit nfs configure file /etc/exports\033[0m"
read -p "input 1 -> clear config,default is add: " choice
if [ $choice -eq 1 ];then
     > /etc/exports
fi
cat >> /etc/exports << EOF
$dir $subnet($permission)
EOF
 
echo -e "\033[32msetting service start with poweron and start searvice......\033[0m "
 
echo -e "\033[31mcheck nfs-server is start?\033[0m"
systemctl status nfs-server.service | grep active &>/dev/null
if [ $? -eq 0 ];then
        systemctl restart nfs-server
        echo "Nfs server restart success"
else
        systemctl start rpcbind
        systemctl start nfs-server
        systemctl enable --now rpcbind
        systemctl enable --now nfs-server
fi
 
 
echo -e "\033[32m########testing wheather network could be use......."
showmount -e 172.25.254.6 | grep $dir &> /dev/null
[ $? -eq 0 ] && {
        echo -e "\033[32mshare success\033[0m" 
}||{
        echo -e "\033[32mshare failed\033[0m"
        exit 
}

echo -e "\033[32mnfs.service is created successfully!!!\033[0m"

おすすめ

転載: blog.csdn.net/qq_42958401/article/details/108488075