經常使用條件測試有 test 命令,[] [[]]等。mongodb
##-f 檢查是否爲文件 [root@promote ~]# ls anaconda-ks.cfg test.txt [root@promote ~]# test -f test.txt && (echo 0 || echo 1) 0 [root@promote ~]# test -f test1.txt && (echo 0 || echo 1) [root@promote ~]# test -f test.txt && echo 0 || echo 1 0 [root@promote ~]# test -f test.txt1 && echo 0 || echo 1 1 [root@promote ~]#
詳細信息請看。docker
文件表達式 -e:文件存在 -f:表示這個文件是個通常文件(regular file)(不是目錄或者設備文件) 文件表達式 -e filename 若是 filename文件存在,則爲真 -d filename 若是 filename爲目錄,則爲真 -f filename 若是 filename爲是般文件(regular file)(不是目錄或者設備文件),則爲真 -L filename 若是 filename爲符號連接,則爲真 -r filename 若是 filename可讀,則爲真 -w filename 若是 filename可寫,則爲真 -x filename 若是 filename可執行,則爲真 -s filename 若是文件長度不爲0,則爲真 -h filename 若是文件是軟連接,則爲真 filename1 -nt filename2 若是 filename1比 filename2新,則爲真。 filename1 -ot filename2 若是 filename1比 filename2舊,則爲真。 整數變量表達式 比較符號 (( ))中[[ ]]使用符號 說明 -eq 等於 ==或= equal -ne 不等於 != not equal -gt 大於 > greater than -ge 大於等於 >= greater equal -lt 小於 < less than -le 小於等於 <= less equal
條件判斷中test 和 [ ]等價。(( ))支持簡單運算,[ ] 與[[]]不支持,[[ ]]支持&&和||。比較運算符先後推薦添加空格。bash
詳細用法能夠查看幫助。less
#沒有全面列舉 [root@promote ~]# man test [root@promote ~]# man bash [root@promote ~]# info bash [root@promote ~]# man bash #部分幫助文件以下 -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file is a directory. -e FILE True if file exists. -f FILE True if file exists and is a regular file. -g FILE True if file is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read.
注意對比如下代碼輸出結果。socket
[root@promote ~]# [ -f /etc/passwd -a /etc/services ] && echo 0 || echo 1 0 [root@promote ~]# [[ -f /etc/passwd -a /etc/services ]] && echo 0 || echo 1 #錯誤使用-a [root@promote ~]# [[ -f /etc/passwd && /etc/services ]] && echo 0 || echo 1 0 [root@promote ~]# [ -f /etc/passwd && /etc/services ] && echo 0 || echo 1 #錯誤使用&&
本節內容經過一段簡單代碼結束。測試
#只能輸入數字一、二、3 #save a .sh file #!/bin/bash testdir=/root/testdir [! -d testdir ] && mkdir -p /root/testdir cat << END 1.[install lamp] 2.[install lnmp] 3.[exit] please input 1 or 2 or 3 : END read input expr $input + 0 &>/dev/null #Not necessarily 0, /dev//null means not output to standard output interface [ $? -ne 0 ] && { echo "please input {1|2|3}" exit 1 } [ $? -ne 1 ] && { echo "Start install lamp ..." exit 1 } [ $? -ne 2 ] && { echo "Start install lnmp ..." exit 1 } [root@promote ~]# ls anaconda-ks.cfg get-docker.sh install_mongodb.sh test testdir.sh test.txt users [root@promote ~]# #簡單目錄是否存在判斷 #查看文件內容 [root@promote ~]# cat testmkdir.sh #!/bin/bash if [ ! -d /root/var/testdir ] ;then mkdir -p /root/var/testdir fi #執行腳本 [root@promote ~]# bash testmkdir.sh [root@promote ~]# ls /root/var/testdir | grep t testdir [root@promote ~]#