2011-12-11 18 views

答えて

5

はい、あなたはバッククォートや$()構文を使用することができます。

if [ $(echo test) = "test" ] ; then 
    echo "Got it" 
fi 

トン場合は、

"`echo test`" 

または

"$(echo test)" 

$(echo test)を置き換える必要があります実行するコマンドの出力は空にすることができます。

POSIXの "stings are equal" testオペレータは=です。

+0

bashの '[[$(echo test)==" test "]]'を使用する場合、引用符は必要ありません。 – choroba

1

あなたは前のプログラムのexit_codeを確認することができますように:

someprogram 
id [[ $? -eq 0 ]] ; then 
    someotherprogram 
fi 

は、通常0終了コードは成功した仕上がりを意味します。

あなたは短いそれを行うことができます:someprogramが正常に終了した場合は、上記のsomeotherprogram

someprogram && someotherprogram 

にのみ実行されます。あなたが失敗したの出口をテストしたい場合は:

someprogram || someotherprogram 

HTH

+0

彼はコマンドの実際の出力を意味し、終了コードは意味しないと思います。 –

+0

おそらく。不特定まで誰が知っていますか? –

+0

"test"という文字列がechoコマンドの出力として期待されるときには、その終了コードではないと仮定したので、私は推測しました。 –

2

を$(および)、またはバッククォートbetweeenコマンドを置く( `)コマンドの戻り値にその式を代用します。だから、基本的には:

if [ `echo test` == "test"]; then 
    echo "echo test outputs test on shell" 
fi 

または

if [ $(echo test) == "test"]; then 
    echo "echo test outputs test on shell" 
fi 

は、トリックを行います。

4

このようなものはありますか?

#!/bin/bash 

EXPECTED="hello world" 
OUTPUT=$(echo "hello world!!!!") 
OK="$?" # return value of prev command (echo 'hellow world!!!!') 

if [ "$OK" -eq 0 ];then 
    if [ "$OUTPUT" = "$EXPECTED" ];then 
     echo "success!" 
    else 
     echo "output was: $OUTPUT, not $EXPECTED" 
    fi 
else 
    echo "return value $OK (not ok)" 
fi 
関連する問題