私はいくつかの BASH スクリプトを書いていますが、エラー処理メカニズムが必要です。
function f()
{
command1 || { echo "command1 failed"; return 1; }
command2 || { echo "command2 failed"; return 1; }
command3 || { echo "command3 failed"; return 1; }
command4 || { echo "command4 failed"; return 1; }
}
関数を定義して、この繰り返し構造を読みやすくしたいと思います。
function print_and_return()
{
echo "$@"
# some way to exit the caller function
}
f
関数を次のように書くことができるように
function f()
{
command1 || print_and_return "command1 failed"
command2 || print_and_return "command2 failed"
command3 || print_and_return "command3 failed"
command4 || print_and_return "command4 failed"
}
これを達成するための最良の方法は何ですか?ありがとう。