shell中如何判斷一個變量是否為空
shell編程中,對參數的錯誤檢查項中,包含了變量是否賦值(即一個變量是否為空),判斷變量為空方法如下:
1.變量通過" "引號引起來
#!/bin/shpara1=if [ ! -n "$para1" ]; then echo "IS NULL"else echo "NOT NULL"fi
【輸出結果】"IS NULL"
2.直接通過變量判斷
#!/bin/shpara1=if [ ! $para1 ]; then echo "IS NULL"else echo "NOT NULL"fi
【輸出結果】"IS NULL"
3.使用test判斷
#!/bin/shdmin=if test -z "$dmin"then echo "dmin is not set!"else echo "dmin is set !"fi
【輸出結果】"dmin is not set!"
4.使用""判斷
#!/bin/sh dmin=if [ "$dmin" = "" ]then echo "dmin is not set!"else echo "dmin is set !"fi
【輸出結果】"dmin is not set!"
新聞熱點
疑難解答
圖片精選