【发布时间】:2013-07-25 08:27:16
【问题描述】:
假设我在脚本“a.sh”中执行set -x,它调用了另一个脚本“b.sh”。
是否可以让“b.sh”从“a.sh”继承-x选项?
【问题讨论】:
-
使用
.或source调用b.sh。
假设我在脚本“a.sh”中执行set -x,它调用了另一个脚本“b.sh”。
是否可以让“b.sh”从“a.sh”继承-x选项?
【问题讨论】:
. 或source 调用b.sh。
export SHELLOPTS
例如:
echo date > b
chmod +x b
没有导出,我们只看到./a调用./b时的命令:
$ echo ./b > a
$ bash -xv a
./a
+ ./b
Sun Dec 29 21:34:14 EST 2013
但如果我们导出 SHELLOPTS,我们会看到 ./a 和 ./b 中的命令
$ echo "export SHELLOPTS; ./b" > a
$ bash -xv a
./a
+ ./b date
++ date
Sun Dec 29 21:34:36 EST 2013
【讨论】:
由于-x 不被子shell 继承,您需要更明确一点。您可以测试-x 何时与$- 特殊参数一起使用。
if [[ $- = *x* ]]; then
# Set the option, then *source* the script, in a subshell
( set -x; . b.sh )
else
# Simply run the script; subshell automatically created.
./b.sh
fi
【讨论】:
如果脚本 b sources 脚本 a,它们将被合并到脚本 b 中。这可能会或可能不会为您解决问题!
【讨论】:
就像@devnull 所说,您可以在脚本中使用. 操作。
在 a.sh 中
. SETVALUES
在 b.sh 中
. SETVALUES
在 SETVALUES 中
set -x
无论您在何处调用 SETVALUES,这些值都将在该子 shell 中设置。
【讨论】: