有没有办法运行bash脚本X,以便如果X调用可执行的bash脚本Y然后Y由'sh -eux'开始?
X.sh:
./Y.sh
Run Code Online (Sandbox Code Playgroud)
Y.sh:
#!/bin/sh
echo OK
Run Code Online (Sandbox Code Playgroud)
Sha*_*hin 18
通过导出SHELLOPTS环境变量,可以使用父级中设置的相同shell选项来创建子shell运行.
在你的情况下X.sh,并Y.sh不能编辑,我想创建一个包装脚本,仅仅出口SHELLOPTS之前调用X.sh.
例:
#!/bin/sh
# example X.sh which calls Y.sh
./Y.sh
Run Code Online (Sandbox Code Playgroud)
.
#!/bin/sh
# example Y.sh which needs to be called using sh -eux
echo $SHELLOPTS
Run Code Online (Sandbox Code Playgroud)
.
#!/bin/sh -eux
# wrapper.sh which sets the options for all sub shells
export SHELLOPTS
./X.sh
Run Code Online (Sandbox Code Playgroud)
X.sh直接调用显示-eux未设置选项Y.sh
[lsc@aphek]$ ./X.sh
braceexpand:hashall:interactive-comments:posix
Run Code Online (Sandbox Code Playgroud)
通过它调用它wrapper.sh表明选项已传播到子壳.
[lsc@aphek]$ ./wrapper.sh
+ export SHELLOPTS
+ ./x.sh
+ ./y.sh
+ echo braceexpand:errexit:hashall:interactive-comments:nounset:posix:xtrace
braceexpand:errexit:hashall:interactive-comments:nounset:posix:xtrace
Run Code Online (Sandbox Code Playgroud)
在GNU bash上测试,版本3.00.15(1) - 发布.因人而异.