この例は、bash を使用して Mac El Capitan でテストされました
main_script.sh:
output
注: func_a と func_b は、ローカル変数が宣言されている行を除いて同一です。
func_a () {
local output
output="$(./external.sh some_function)"
if [ $? -eq 0 ];then
echo "A zero result ($?) -> $output <- end"
else
echo "A other result ($?) -> $output <- end"
fi
}
func_b () {
local output="$(./external.sh some_function)"
if [ $? -eq 0 ];then
echo "B zero result ($?) -> $output <- end"
else
echo "B other result ($?) -> $output <- end"
fi
}
func_a
func_b
外部.sh:
some_function () {
echo "this is the output"
return 1
}
"$@"
main_script を実行すると、出力は次のようになります。
A other result (1) -> this is the output <- end
B zero result (0) -> this is the output <- end
コマンド置換と同じ行でローカル変数を宣言すると、どのような理由で結果に影響しますか? これはバグでしょうか、それとも何か不足していますか?