Bashと終了コードにスクリプトが含まれている必要があります。

Bashと終了コードにスクリプトが含まれている必要があります。

スクリプトbashを呼び出すスクリプトがあります。expect

Expectスクリプトにはいくつかの条件があります。ボックスにsshを接続してコマンドを実行すると、他のエラーが発生する可能性があります。次の終了コードを提供しました。

expect {
    "passwd: password updated successfully" {
    exit 0
    }
    "Password unchanged" {
    exit 1
    }
    "Bad: new and old password are too similar" {
    exit 2
    }
    "You must choose a longer password" {
    exit 3
    }
}

私はこれらの終了コードを取得し、次のようにExpectスクリプトを呼び出すbashスクリプトで使用する予定です。

            if [ $? -eq 0 ]; then
            echo -e "\n"
            echo "Password successfully changed on $host by $user"

            elif [ $? -eq 1 ]; then
                echo "Failure, password unchanged"
            elif [ $? -eq 2 ]; then
                echo "Failure, new and old passwords are too similar"
            elif [ $? -eq 3 ]; then
                echo "Failure, password must be longer"
            else
                echo "Password failed to change on $host"
        fi

しかし、これはうまくいきません。予想されるスクリプトは、失敗したかどうかにかかわらず、成功した場合は0、失敗した場合は1のみを返すようです。割り当てられた終了コードを検索してbashスクリプトで使用できますか?

それとも、エラーを説明するために予想されるスクリプトで "send_user"を使用する方が良いですか?それでは、期待どおりに「send_user」コマンドの出力を使用するようにbashスクリプトをどのように取得できますか?

ベストアンサー1

より簡単な方法は、反復検査の代わりにcaseステートメントを使用することです。$?

case "$?" in
    0) echo "Password successfully changed on $host by $user" ;;
    1) echo "Failure, password unchanged" ;;
    2) echo "Failure, new and old passwords are too similar" ;;
    3) echo "Failure, password must be longer" ;;
    *) echo "Password failed to change on $host" ;;
esac

おすすめ記事