ほとんどすべての人が非常に便利な && および || 演算子を知っています。たとえば、次のようになります。
rm myf && echo "File is removed successfully" || echo "File is not removed"
質問があります: 関数を使用せずに && または || 演算子の後にコマンド ブロックを配置するにはどうすればよいでしょうか?
たとえば、次のことをやりたいです:
rm myf && \
echo "File is removed successfully" \
echo "another command executed when rm was successful" || \
echo "File is not removed" \
echo "another command executed when rm was NOT successful"
そのスクリプトの正しい構文は何ですか?
答え1
rm myf && {
echo "File is removed successfully"
echo "another command executed when rm was successful"
} || {
echo "File is not removed"
echo "another command executed when rm was NOT successful"
}
またはそれ以上
if rm myf ; then
echo "File is removed successfully"
echo "another command executed when rm was successful"
else
echo "File is not removed"
echo "another command executed when rm was NOT successful"
fi