shell判斷一個變量是否爲空方法總結
https://www.jb51.net/article/154835.htmlinux
1.判斷變量shell
read -p "input a word :" word
if [ ! -n "$word" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $word"
fi
2.判斷輸入參數bash
#!/bin/bash
if [ ! -n "$1" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $1"
fi
如下未驗證。app
3. 直接經過變量判斷url
以下所示:獲得的結果爲: IS NULLspa
#!/bin/sh
para1=
if [ ! $para1 ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi
4. 使用test判斷.net
獲得的結果就是: dmin is not set!命令行
#!/bin/sh
dmin=
if test -z "$dmin"
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
5. 使用""判斷code
#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
下面是我在某項目中寫的一點腳本代碼, 用在系統啓動時:htm
#! /bin/bash
echo "Input Param Is [$1]"
if [ ! -n "$1" ] ;then
echo "you have not input a null word!"
./app1;./app12;./app123
elif [ $1 -eq 2 ];then
./app12;./app123
elif [ $1 -eq 90 ];then
echo "yy";
fi