2013-03-07 36 views
1

我在尋找解決方案的建議以及處理找出多個IF是否爲空的最佳方法的建議。如果多個IF爲空,請執行下列操作

我:

if [ -n "$sfcompname" ]; then 
     echo $sfcompname 
fi 
if [ -n "$sfcompip" ]; then 
     echo $sfcompip 
fi 
if [ -n "$lacompname" ]; then 
     echo $lacompname 
fi 
if [ -n "$lacompip" ]; then 
     echo $lacompip 
fi 

。我敢肯定,可以做的更好,但我目前的主要問題是試圖然後說:

如果(所有IFS)=空

回聲「請檢查您輸入的名稱,然後再試一次」

回答

3

有點傻,但應該工作

if ! [[ ${sfcompname}${sfcompip}${lacompname}${lacompip} ]] 
then 
    echo "Please check the name you entered and try again" 
fi 
+0

確實很奇妙,很傻,但我只是在學習。它工作,我喜歡它。非常直截了當。謝謝 – TryTryAgain 2013-03-07 01:11:39

1

您可以使用另一個變量這個你初始化的值,然後如果有任何的改變聲明失火。然後在最後,如果它沒有改變,那麼你知道他們發射了沒有。像這樣的東西就足夠了:

fired=0 

if [ -n "$sfcompname" ]; then 
    echo $sfcompname 
    fired=1 
fi 
if [ -n "$sfcompip" ]; then 
    echo $sfcompip 
    fired=1 
fi 
if [ -n "$lacompname" ]; then 
    echo $lacompname 
    fired=1 
fi 
if [ -n "$lacompip" ]; then 
    echo $lacompip 
    fired=1 
fi 

if [[ ${fired} -eq 0 ]] ; then 
    echo 'None were fired' 
fi 
+0

非常偷偷摸摸,我喜歡它:D感謝您的教訓。 – TryTryAgain 2013-03-07 01:09:58

+1

「偷偷摸摸」是我的特長:-) – paxdiablo 2013-03-07 01:11:54

1

另一種可能性是使用變量檢查快捷:

name="$sfcompname$sfcompip$lacompname$lacompip"   
${name:?"Please check the name you entered and try again"} 

這將退出程序如果沒有設置變量。該消息是可選的,它覆蓋了標準的「參數null或未設置」。

相關問題