源运算符在 bash 中的括号内不起作用
我想在 bash 脚本中包含一个配置文件,有两个条件:
- 配置文件名是即时构造的并存储在变量中,
- 如果配置文件不存在,脚本应该失败:
config .cfg:
CONFIGURED=yes
test.sh:
#!/bin/sh
$CFG=config.cfg
echo Source command doesn't work here:
[ -f $CFG ] && ( source $CFG ) || (echo $CFG doesnt exist; exit 127)
echo $CONFIGURED
echo ... but works here:
source $CFG
echo $CONFIGURED
[...] 语句有什么问题?
I'd like to include a config file in my bash script with 2 conditions:
- the config file name is constructed on-the-fly and stored in variable, and
- in case if config file doesn't exist, the script should fail:
config.cfg:
CONFIGURED=yes
test.sh:
#!/bin/sh
$CFG=config.cfg
echo Source command doesn't work here:
[ -f $CFG ] && ( source $CFG ) || (echo $CFG doesnt exist; exit 127)
echo $CONFIGURED
echo ... but works here:
source $CFG
echo $CONFIGURED
What's wrong in [...] statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
( ... )
在单独的子 shell 中运行命令。如果您想在同一个 shell 中运行命令(如果可能),请使用{ ... ; }
相反。( ... )
runs the commands in a separate subshell. If you want to run the commands in the same shell if possible then use{ ... ; }
instead.