bashでシェルからコマンドを実行することは可能ですか?また、ある値(または空の値)がコマンドを実行した場合は返されますか?Bash:if ["echo test" == "test"]; "エコーテストはシェル上でテスト出力を出力する";可能?
if [ "echo test" == "test"]; then
echo "echo test outputs test on shell"
fi
bashでシェルからコマンドを実行することは可能ですか?また、ある値(または空の値)がコマンドを実行した場合は返されますか?Bash:if ["echo test" == "test"]; "エコーテストはシェル上でテスト出力を出力する";可能?
if [ "echo test" == "test"]; then
echo "echo test outputs test on shell"
fi
はい、あなたはバッククォートや$()
構文を使用することができます。
if [ $(echo test) = "test" ] ; then
echo "Got it"
fi
トン場合は、
"`echo test`"
または
"$(echo test)"
で$(echo test)
を置き換える必要があります実行するコマンドの出力は空にすることができます。
POSIXの "stings are equal" test
オペレータは=
です。
あなたは前のプログラムのexit_code
を確認することができますように:
someprogram
id [[ $? -eq 0 ]] ; then
someotherprogram
fi
注は、通常0
終了コードは成功した仕上がりを意味します。
あなたは短いそれを行うことができます:someprogram
が正常に終了した場合は、上記のsomeotherprogram
で
someprogram && someotherprogram
にのみ実行されます。あなたが失敗したの出口をテストしたい場合は:
someprogram || someotherprogram
HTH
彼はコマンドの実際の出力を意味し、終了コードは意味しないと思います。 –
おそらく。不特定まで誰が知っていますか? –
"test"という文字列がechoコマンドの出力として期待されるときには、その終了コードではないと仮定したので、私は推測しました。 –
を$(および)、またはバッククォート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
は、トリックを行います。
このようなものはありますか?
#!/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
bashの '[[$(echo test)==" test "]]'を使用する場合、引用符は必要ありません。 – choroba