2016-05-22 2 views
1

次のスクリプトで 'elseif'行を修正して、終了ステータスをゼロにしてエラーなしで処理できるようにしてください。これは正しい構文を知らないためです。Bash else /終了ステータスをキャプチャする場合

次のコマンドからゼロをキャプチャする必要がありますが、スクリプト内のカッコ内です。

softwareupdate -l 2>&1 | grep restart 

マイスクリプト:

#!/bin/bash 
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH 
SWLOG=/var/log/swupdate.log 

softwareupdate -l 2>&1 | grep "No new software available." 

if [ $? -eq 0 ] 

then 
    echo "No new software was deemed to be available" 

elif [ $(softwareupdate -l 2>&1 | grep restart & $? = 0) ] 

then 
    echo "RESTART will be needed" 

else 
    echo "Updates needed, but not restart" 

fi 

はあなたの提案やアイデアを事前にありがとうございます!

ダン

答えて

2

あなたはgrep -qを使用して$?の使用を避けることができます。

#!/bin/bash 
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH 
SWLOG=/var/log/swupdate.log 

if softwareupdate -l 2>&1 | grep -q "No new software available." 
then 
    echo "No new software was deemed to be available"  
elif softwareupdate -l 2>&1 | grep -q restart  
then 
    echo "RESTART will be needed"  
else 
    echo "Updates needed, but not restart"  
fi 
0

if softwareupdate -l 2>&1 | grep restart >/dev/null; thenは仕事を行います。変数$?を使用して人工迂回を行う必要はありません。

2

コマンドの出力を2回評価するのではなく、出力を一時ファイルに送信するだけです。

softwareupdate -l > temp.out 2>&1 
grep "No new software available." temp.out > /dev/null 
if [ $? -eq 0 ] 
then 
    echo "No new software was deemed to be available" 
    exit 
fi 
grep "restart" temp.out > /dev/null 
if [ $? -eq 0 ] 
then 
    echo "RESTART will be needed" 
else 
    echo "Updates needed, but not restart" 
fi 
関連する問題