0

この問題は sh にあり、スタックしたスクリプト全体を終了するには関数が必要です。または、呼び出し元によって変数に設定される値をエコーし​​ます。

次の例が問題を最もよく説明していると思います。

#!/bin/sh

do_exit(){
 if [ "$1" = "1" ] ; then
        echo "name"
        return 0
 fi
 exit 1
}

echo "# That of course should pass"
echo before
var=$(do_exit 1)
echo $var
echo after
echo ""

echo "# Here I expect the func to terminate the script"
echo before
var=$(do_exit 2)
echo $var
echo after
echo ""

echo "# Here I also expect the func to terminate the script"
echo before
var=`do_exit 2`
echo $var
echo after
echo ""

echo "# And this is the only case it exit"
echo before
do_exit 2
echo after

上記の出力は次のとおりです。

# That of course should pass
before
name
after

# Here I expect the func to terminate the script
before

after

# Here I also expect the func to terminate the script
before

after

# And this is the only case it exit
before

それをどのように説明できますか。関数は、呼び出し元ではなく自己終了するフォークされたプロセスで実行されるように思えます

  1. それを実行するネイティブな方法はありますか?
  2. すでにここで説明されているように、参照によって引数を渡す以外にそれを行う方法はありますか: Bash - Passing arguments by reference ?

前もって感謝します!ヤニフ

4

0 に答える 0