1,邏輯判斷
2,if流程控制語句
在開始之前,先了解以下邏輯判斷符號:
&&與,||或,!非
A && B 必須A,B同時成立才能通過判斷
-------->判斷流程:先執行A判斷其是否成立,若成立,則繼續執行B,判斷其是否成立
-------->若不成立,則直接結束,不再執行B
A || B A,B有一個成立,則可通過判斷
!A 若A不成立,則通過判斷
--------------------------------------------------------
-------------------------------------------------------
在條件判斷時,那面會比較數值或字符串,這是我們會用到一些參數和符號,如下:
比較數值:
等於------> -eq
不等於------->-ne
小於--------->-lt
大於--------->-gt
小於等於-------->-le
大於等於-------->-ge
一些英文單詞:幫助記憶
equal等於,not equal,less than,great than
比較字符串:
= 等於,== 等於,同=,!=不等於,>大於,<小於,-z字符串為空,-n字符串非空null
注意:在[ ]結構中,<和> 需要使用轉義符號,如下圖
[lu@bogon ~]$ [ $A > $B ] && echo "yes"
yes
[lu@bogon ~]$ [ $A < $B ] && echo "yes"
yes
[lu@bogon ~]$ [ $A \> $B ] && echo "yes"
[lu@bogon ~]$ [ $A \< $B ] && echo "yes"
yes
實戰1:判斷當前系統語言環境
[lu@bogon ~]$ echo $LANG
zh_CN.UTF-8
[lu@bogon ~]$ echo $LANG | awk -F . '{print $1}'
zh_CN
vim lang.sh
#!/bin/bash
lang=$(echo $LANG | awk -F . '{print $1}')
if [ $lang == 'en_us' ]
then
echo 'the default language is $lang.'
else
echo 'please set default language as english'
fi
[lu@bogon ~]$ bash -x lang.sh
++ echo zh_CN.UTF-8
++ awk -F . '{print $1}'
+ lang=zh_CN
+ '[' zh_CN == en_us ']'
+ echo 'please set default language as english'
please set default language as english
[lu@bogon ~]$
if流程控制語句
if語句有三種使用方式:
格式如下:
單分支:
if 條件判斷語句
then 執行語句
fi
雙分支:
if 條件判斷語句
then 執行語句
else 執行語句
fi
多分支:
if 條件判斷語句
then 執行語句
elif 條件判斷語句
then 執行語句
else 執行語句
fi
實戰1:if單分支應用
vi if01.sh
#!/bin/bash
if [ ! -d /home/lu/cdrom ]
then
echo "/home/lu/cdrom is not exist"
fi
[lu@bogon ~]$ bash -x if01.sh
+ '[' '!' -d /home/lu/cdrom ']'
+ echo '/home/lu/cdrom is not exist'
/home/lu/cdrom is not exist
實戰2:if雙分支應用
ping -c 3 -i 0.2 -w 3 10.152.247.1
-c 發送數據包的個數
-i 數據包每次發送間隔時間,默認單位:秒(s)
-w 等待時間,超過返回失敗
#!/bin/bash
ping -c 3 -i 0.2 -w 3 $1 > /dev/null
if [ $? -eq 0 ]
then
echo "the host $! is ip"
else
echo "the host $1 is down"
fi
[lu@bogon ~]$ bash if02.sh 127.0.0.1
the host is ip
實戰3:if多分支應用
#!/bin/bash
read -p "please input your score (0-100):" num
if [ $num -ge 85 ] && [ $num -le 100 ]
then
echo "great !優秀!"
elif [ $num -ge 60 ] && [ $num -le 84 ]
then
echo "good 良好"
else
echo “不及格”
fi
[lu@bogon ~]$ bash -x if03.sh
+ read -p 'please input your score (0-100):' num
please input your score (0-100):86
+ '[' 86 -ge 85 ']'
+ '[' 86 -le 100 ']'
+ echo 'great !優秀!'
great !優秀!
實戰4,if嵌套使用
編寫腳本,監控服務運行狀態
啟動失敗后保存日志,並重啟服務
再次失敗,提示重啟主機
#!/bin/bash
systemctl status $1 >> /var/log/ser.log
if [ $? -eq 0 ]
then
echo "the $1 is running."
else
echo "the $1 is dead."
systemctl restart $1 >> /var/log/ser.log
if [ $? -eq 0 ]
then
echo "the $1 reboot finish!"
else
echo "warning you need reboot your system"
fi
fi
[root@bogon lu]# bash -x if04.sh httpd
+ systemctl status httpd
+ '[' 0 -eq 0 ']'
+ echo 'the httpd is running.'
the httpd is running.
實戰5:查詢內和版本,並輸出信息
#!/bin/bash
prime=$(uname -r | awk -F . '{print $1}')
if [ $prime -gt 2 ]
then
echo "the major version of system is $prime "
elif [ $prime -lt 1 ];then
echo "the system is too low "
else
echo "faild"
fi
[lu@bogon ~]$ bash if05.sh
the major version of system is 4
