if 語句-條件選擇
-
if 是一個shell的關鍵字
[root@Centos6app]#type if if is a shell keyword
if 選擇執行
if 可以嵌套使用 - 用法
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ] ... [ else COMMANDS; ] fi
執行 “if COMMANDS” 列表。 如果它的退出狀態是零,那麼執行“然後命令”列表。 否則,依次執行每個“elif COMMANDS”列表,如果其退出狀態爲零,則執行相應的“then COMMANDS”列表,然後執行if命令。 否則,執行“else COMMANDS”列表(如果存在)。 整個結構的退出狀態是所執行的最後一個命令的退出狀態,如果條件不成立,則爲零。
if 支持單分支、雙分支、多分支用法: if 判斷條件1 ; then 條件爲真的分支代碼 elif 判斷條件2; then 條件爲真的分支代碼 elif 判斷條件3; then 條件爲真的分支代碼 else 以上條件都爲假的分支代碼 fi 逐條件進行判斷,第一次遇爲“真”條件時,執行其分支,而後結束整個if語句
- 示例
根據命令的退出狀態來執行命令 if ping -c1 -W2 station1 &> /dev/null ; then echo 'Station1 is UP' elif grep "station1" ~/maintenance.txt &> /dev/null ; then echo 'Station1 is undergoing maintenance‘ else echo 'Station1 is unexpectedly DOWN!' exit 1 fi
判斷年齡----->最簡單的示例 read -p "Please input your age: " age if [[ ! "$age" =~ ^[0-9]+$ ]] ; then echo "please input right number!" exit 2 elif [ "$age" -le 18 ] ; then echo "you are a child" elif [ "$age" -le 50 ] ; then echo "you are very young" elif [ "$age" -le 80 ] ; then echo "you can go home" elif [ "$age" -le 120 ] ; then echo "you need to feel life" else echo "Go to hell" fi
if嵌套最簡單示例 read -p "Please input your score: " score if [[ "$score" =~ ^[0-9]+$ ]]; then --------->注意書寫時 變量score 上下統一,**別上下寫的不統一**,就容易報錯 if [ "$score" -le 59 ] ; then echo "your score is so bad" elif [ "$score" -le 70 ] ; then echo "your score is good" elif [ "$score" -le 100 ] ; then echo "unbeliveable" else echo "Go to hell" fi else echo "Please input correct score!" fi
- 練習題
1、編寫腳本/root/bin/createuser.sh,實現如下功能:使用一個用戶名做爲參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息
#!/bin/bash
if id $1 &> /dev/null ; then
echo "$1 User already exists!"
else
useradd $1 > /dev/null &&
echo "$1 User created successfully,$1 info `id $1`"
fi
如果不用if語句,id $1 &> /dev/null && echo "$1 user already exists!" || useradd $1 ; echo "User created successfully,$1 info `id $1`"
2、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no, 並判斷用戶輸入的是yes還是no,或是其它信息
#!/bin/bash
read -p "Do you agree? (yes or no): " ans
if [[ "$ans" =~ ^[Yy]([Ee][Ss])?$ ]] ; then
echo "Ok"
elif [[ "$ans" =~ ^[Nn][Oo]?$ ]] ; then
echo "hehe"
else
echo "Input error"
fi
3、編寫腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)
#!/bin/bash
read -p "Please enter the file name: " file
if [ -f "$file" ] ; then
echo "The $file is ordinary documents"
elif [ -d "$file" ] ; then
echo "The $file is table of contents"
elif [ -L "$file" ] ; then
echo "The $file is link file"
else
echo "The $file is othe types"
fi
4、編寫腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否爲正整數
#!/bin/bash
1、
read -p "Please input a number: " num
if [[ "$num" =~ ^[[:digit:]]+$ ]] ; then
if [[ "$num" -gt 0 ]] ; then
echo "Entered correctly!"
else
echo "Input error!"
fi
else
echo "Not a positive integer"
fi
2、
read -p "please input :" nub
if [[ $arg =~ ^[[:digit:]]+$ ]] && [ $(expr $arg + 0) != 0 ] ; then
echo "Bingo!"
else
echo "Wrong!"
fi
case 語句-條件判斷
[root@Centos6app]#type case
case is a shell keyword
- 用法
case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac case 變量引用 in PAT1) 分支1 ;; PAT2) 分支2 ;; ... *) 默認分支 ;; esac
- case支持glob風格的通配符:
*: 任意長度任意字符 ?: 任意單個字符 []:指定範圍內的任意單個字符 a|b: a或b
最簡單示例 yes or no #!/bin/bash read -p "Do you agree? (yes or no): " ans case $ans in [Yy]|[Yy][Ee][Ss]) echo "Ok" ;; [Nn]|[Nn][Oo]) echo "hehe" ;; *) echo "What do you want to do !" ;; esac