テストせずにBashで「そうでない場合」を実行する方法は?

テストせずにBashで「そうでない場合」を実行する方法は?

"if not"ステートメントでbash関数の戻り値を使用したいと思います。以下はスクリプトの例です。

#!/bin/bash

function myfunction () {
 if [ $1 = "one" ]; then
  return 1
 elif [ $1 = "two" ]; then
  return 2
 else
  return 0
 fi
}

if myfunction "two"; then
 # just using echo as an example here
 echo yep $?
else
 # just using echo as an example here
 echo nope $?
fi

スクリプトが "yes 2"をエコーする方法で "if myfunction "two""部分を変更する方法はありますか?こんな醜い方法しか思わないですね。どうすればこの問題をよりよく解決できますか?

ベストアンサー1

if myfunction "two"擬似コードはですif the myfunction return code is zero when run with a single argument "two"。比較()を反転するには、との間にを追加しますis not zero!ifmyfunction

おすすめ記事