Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法
先給大家分享一篇關(guān)于shell判斷一個變量是否為空方法總結(jié)內(nèi)容
shell判斷一個變量是否為空方法總結(jié)
http://m.fzitv.net/article/154835.htm
1.判斷變量
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.判斷輸入?yún)?shù)
#!/bin/bash
if [ ! -n "$1" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $1"
fi
以下未驗證。
3. 直接通過變量判斷
如下所示:得到的結(jié)果為: IS NULL
#!/bin/sh
para1=
if [ ! $para1 ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi
4. 使用test判斷
得到的結(jié)果就是: dmin is not set!
#!/bin/sh
dmin=
if test -z "$dmin"
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
5. 使用""判斷
#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
下面是我在某項目中寫的一點腳本代碼, 用在系統(tǒng)啟動時:
#! /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
相關(guān)文章
定時導(dǎo)出mysql本地數(shù)據(jù)替換遠(yuǎn)程數(shù)據(jù)庫數(shù)據(jù)腳本分享
這篇文章主要介紹了mysql每天定時倒出本地數(shù)據(jù),替換遠(yuǎn)程數(shù)據(jù)庫數(shù)據(jù)的腳本,需要的朋友可以參考下2014-03-03
Linux?Shell任務(wù)控制的實現(xiàn)示例
本文主要介紹了Linux?Shell任務(wù)控制的實現(xiàn)示例,包括向腳本發(fā)送信號、修改腳本的優(yōu)先級以及在腳本運行時從暫停切換到運行模式,感興趣的可以了解一下2024-01-01
linux下python3連接mysql數(shù)據(jù)庫問題
這篇文章主要介紹了linux下python3連接mysql數(shù)據(jù)庫問題,需要的朋友可以參考下2015-10-10

