http://bbs.chinaunix.net/thread-396805-1-1.html
shell 判斷語句
流程控制 "if" 表達式 如果條件為真則執行then后面的部分:
if ....; then .... elif ....; then .... else .... fi 大多數情況下,可以使用測試命令來對條件進行測試。比如可以比較字符串、判斷文件是否存在及是否可讀等等…
通常用" [ ] "來表示條件測試。注意這里的空格很重要。要確保方括號的空格。
[ -f "somefile" ] :判斷是否是一個文件
[ -x "/bin/ls" ] :判斷/bin/ls是否存在並有可執行權限
[ -n "$var" ] :判斷$var變量是否有值
[ "$a" = "$b" ] :判斷$a和$b是否相等
-r file 用戶可讀為真
-w file 用戶可寫為真
-x file 用戶可執行為真
-f file 文件為正規文件為真
-d file 文件為目錄為真
-c file 文件為字符特殊文件為真
-b file 文件為塊特殊文件為真
-s file 文件大小非0時為真
-t file 當文件描述符(默認為1)指定的設備為終端時為真
-n variable 判斷一個變量是否有值
-z variable 判斷一個變量是否為非空字符串
#########################################################
含條件選擇的shell腳本 對於不含變量的任務簡單shell腳本一般能勝任。但在執行一些決策任務時,就需要包含if/then的條件判斷了。shell腳本編程支持此類運算,包括比較運算、判斷文件是否存在等。 基本的if條件命令選項有: -eq —比較兩個參數是否相等(例如,if [ 2 –eq 5 ]) -ne —比較兩個參數是否不相等 -lt —參數1是否小於參數2 -le —參數1是否小於等於參數2 -gt —參數1是否大於參數2 -ge —參數1是否大於等於參數2 -f — 檢查某文件是否存在(例如,if [ -f "filename" ]) -d — 檢查目錄是否存在 幾乎所有的判斷都可以用這些比較運算符實現。腳本中常用-f命令選項在執行某一文件之前檢查它是否存在。 ################################################################# # 判斷文件是否存在 #!/bin/sh today=`date -d yesterday +%y%m%d` file="apache_$today.tar.gz" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" >error.log mail -s "fail backup from test" chenshuo@soufun.com <error.log fi =================================================
1.shell判斷文件,目錄是否存在或者具有權限 2. #!/bin/sh 3. 4.myPath= "/var/log/httpd/" 5.myFile= "/var /log/httpd/access.log" 6. 7.#這里的-x 參數判斷$myPath是否存在並且是否具有可執行權限 8.if [ ! -x "$myPath" ]; then 9.mkdir "$myPath" 10.fi 11. 12.#這里的-d 參數判斷$myPath是否存在 13.if [ ! -d "$myPath" ]; then 14.mkdir "$myPath" 15.fi 16. 17.#這里的-f參數判斷$myFile是否存在 18.if [ ! -f "$myFile" ]; then 19.touch "$myFile" 20.fi 21. 22.#其他參數還有-n,-n是判斷一個變量是否有值 23.if [ ! -n "$myVar" ]; then 24.echo "$myVar is empty" 25.exit 0 26.fi 27. 28.#兩個變量判斷是否相等 29.if [ "$var1" = "$var2" ]; then 30.echo '$var1 eq $var2' 31.else 32.echo '$var1 not eq $var2' 33.fi
/****************************************************************************/
1 判斷一個變量是否被定義
if [ -z $EDITOR ]
2 判斷交互模式
if [ -t ]
3 測試文件權限
if [ ! -w "$LOGFILE"]
4 測試SHELL命令
if echo $list | grep "Peter" > /dev/null 2>&1
5 測試數值
if [ "10" -lt "12" ]
* -b file = True if the file exists and is block special file. 如果該文件存在並且是塊特殊文件。
* -c file = True if the file exists and is character special file.如果該文件存在並且是字符特殊文件
* -d file = True if the file exists and is a directory. 如果該文件存在並且是一個目錄。
* -e file = True if the file exists. 如果該文件存在
* -f file = True if the file exists and is a regular file 如果該文件存在並且是一個普通文件
* -g file = True if the file exists and the set-group-id bit is set. 如果該文件存在並且設置了組ID位。
* -k file = True if the files’ “sticky” bit is set. 如果文件的sticky “粘性”位被設置。
* -L file = True if the file exists and is a symbolic link. 該文件存在並且是一個符號鏈接。
* -p file = True if the file exists and is a named pipe. 該文件存在並且是一個命名管道。
* -r file = True if the file exists and is readable. 文件存在並且是可讀的
* -s file = True if the file exists and its size is greater than zero. 文件存在,它的大小是大於零
* -S file = True if the file exists and is a socket. 文件存在並且是一個套接字
* -t fd = True if the file descriptor is opened on a terminal. 文件描述符是在一個終端上打開的
* -u file = True if the file exists and its set-user-id bit is set. 文件存在,它的設置用戶ID位被設置了
* -w file = True if the file exists and is writable. 文件存在並且可寫
* -x file = True if the file exists and is executable. 文件存在並且是可執行的
* -O file = True if the file exists and is owned by the effective user id. 文件存在並且是所擁有的有效用戶ID
* -G file = True if the file exists and is owned by the effective group id. 文件存在並且擁有有效的gruop id。
* file1 -nt file2 = True if file1 is newer, by modification date, than file2. 如果file1更新
* file1 ot file2 = True if file1 is older than file2. 如果file1更舊
* file1 ef file2 = True if file1 and file2 have the same device and inode numbers.file1和file2有相同的設備和節點號
* -z string = True if the length of the string is 0. 字符串的長度為0
* -n string = True if the length of the string is non-zero. 字符串的長度不為零
* string1 = string2 = True if the strings are equal.
*string1 != string2 = True if the strings are not equal.
!expr = True if the expr evaluates to false.
* expr1 -a expr2 = True if both expr1 and expr2 are true. 且為真
* expr1 -o expr2 = True is either expr1 or expr2 is true. 或
兩個檔案之間的判斷與比較 ;例如『 test file1 -nt file2 』 * -nt 第一個檔案比第二個檔案新 * -ot 第一個檔案比第二個檔案舊 * -ef 第一個檔案與第二個檔案為同一個檔案( link 之類的檔案)
邏輯的『和(and)』『或(or)』 * && 邏輯的 AND 的意思 * || 邏輯的 OR 的意思
運算符號 代表意義 = 等於 != 不等於 < 小於 > 大於 -eq 等於 -ne 不等於 -lt 小於 -gt 大於 -le 小於或等於 -ge 大於或等於 -a 雙方都成立(and) -o 單方成立(or) -z 空字符串 -n 非空字符串
/************************************************************/
格式如下,在比較時,數字和字符串用不同的比較符號
1.如果a>b且a<c
if (( a > b )) && (( a < c )) 或者
if [[ $a > $b ]] && [[ $a < $c ]]
或者 if [ $a -gt $b -a $a -lt $c ]
2.如果a>b或a<c
if (( a > b )) || (( a < c )) 或者 if [[ $a > $b ]] || [[ $a < $c ]]
或者 if [ $a -gt $b -o $a -lt $c ]
3. -o = or , -a = and , 但我一向只用 || 或者 &&
4."||"和"&&"在SHELL里可以用嗎?也就是第一個寫成if [ a>b && a<c ]也可以嗎?
可用, 但是要兩個獨立的 [ ] , [[ ]] 或 (( )) 看 1
5 -ne 比較數字 (numberic) ; != 比較字符 (string), 但后者拿來 比較數字也可,只是不是標准用法 -lt 是等同 < , 但 < 只能在 shell 的數值操作符 (( )) 或 者 邏緝操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge -le , 這些是 test , 就是 [ ] 這個內建命令使用的條件操 作符, 數字使用; = , != 字符用, == 這個該是 [[ ]] 用的, 可用來比對正規表示式, 但用在 [ ] 也可,只是不太正統用法 /************************************************************/
test命令用法。功能:檢查文件和比較值
1)判斷表達式 if test (表達式為真) if test !表達式為假
-----
