Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

bash recursive xtrace

Is there any way to run bash script X so that if X call executable bash script Y then Y starts by 'sh -eux'?

X.sh:

./Y.sh

Y.sh:

#!/bin/sh
echo OK
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is possible to make a subshell run using the same shell options set in the parent by exporting the SHELLOPTS environment variable.

In your case where X.sh and Y.sh cannot be edited, I'd create a wrapper script that simply exports SHELLOPTS before calling X.sh.

Example:

#!/bin/sh
# example X.sh which calls Y.sh
./Y.sh

.

#!/bin/sh
# example Y.sh which needs to be called using sh -eux
echo $SHELLOPTS

.

#!/bin/sh -eux
# wrapper.sh which sets the options for all sub shells
export SHELLOPTS
./X.sh

Calling X.sh directly shows that -eux options are not set in Y.sh

[lsc@aphek]$ ./X.sh 
braceexpand:hashall:interactive-comments:posix

Calling it via wrapper.sh shows that the options have propagated to the subshells.

[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

Tested on GNU bash, version 3.00.15(1)-release. YMMV.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...