你能模拟“set -e”吗?和“设置-x”通过环境?
我的许多测试脚本都是这样开始的:
set -e test -n "$V" && set -x
我不是将这些行(或采购通用脚本)放入每个脚本中,而是将 喜欢通过环境获得该功能。是否有一种可移植的方法来使用环境设置来使 sh 的行为就像已调用“set -e”或“set -x”一样?是否有一种不可移植的(即特定于 shell 的)方法可以执行相同的操作?
(我已将这个问题标记为 automake,因为这是我目前所处的框架,并且希望能够在 TESTS_ENVIRONMENT 中放入一些内容,这将允许我从每个脚本中省略这些行,但显然问题不是 automake具体的。)
Many of my test scripts begin:
set -e test -n "$V" && set -x
Rather than putting those lines ( or sourcing a common script ) in each script, I'd
like to get that functionality through the environment. Is there a portable way to use environment settings to cause sh to behave as if "set -e" or "set -x" has been called? Is there a non-portable (ie shell-specific) way to do the same?
(I've tagged this question as automake because that's the framework I'm in at the moment and would like to be able to put something in TESTS_ENVIRONMENT that will allow me to omit those lines from each script, but clearly the question is not automake specific.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将其添加到您的脚本中:
现在您可以将环境变量
ENABLE_DEBUG
设置为set -e ;测试-n“$V”&&设置 -x
以启用调试,或者您可以不设置它。请注意,如果您激活了“未定义变量失败”选项(
set -u
或set -o nounset
),则此操作会失败。如果是这种情况,您需要检查变量是否已设置,或者使用 bash with:将变量设置为
:
,即“不执行任何操作”命令。Just add this to your scripts:
Now you can set the env variable
ENABLE_DEBUG
toset -e ; test -n "$V" && set -x
to enable debugging or you can leave it unset.Note that this fails if you have the option "fail for undefined variables" active (
set -u
orset -o nounset
). If that is the case, you either need to check that the variable is set or use bash with:that sets the variable to
:
, the "do nothing" command.回答automake部分。
如果您
生成了
Makefile
,将有一个test
目标,大致类似于您实际上可以将
TEST_ENVIRONMENT
设置为一个命令,该命令将使用 <代码>sh -xe 或sh -e
。如果所有测试都是 shell 脚本,这可以是一个简单的设置,
如果不是所有测试都是 shell 脚本,您可以
编写一个脚本
run
例如:Answering the automake part.
If you have
the
Makefile
generated will have atest
target roughly likeYou can actually set
TEST_ENVIRONMENT
to a command that will start your shell scripts withsh -xe
orsh -e
.If all tests are shell scripts, this can be a simple as setting
if not all tests are shell scripts, you can have
and write a script
run
such as: