bash - 在函数中导出的 bash 脚本中的变量
问题描述
我在脚本中有一个函数,我想在其中扩展一个变量。我该怎么做?
例如:
set_function.sh:
function test_funct {some_binary --configfile="$1"/folder/; }
export -f test_funct
我希望这样,如果我打电话:
./set_function.sh /some/path/here
传递的变量 /some/path/here 得到扩展,因此如果我再调用
test_funct --some-other-flag
我不明白:
some_binary --configfile=--some-other-flag/some_path/
反而
some_binary --configfile=/some/path/here/folder/ --some-other-flag
解决方案
似乎您需要使用变量,如下所示:
test_func() {
some_binary --config $CONFIG_PATH/folder/ "$@"
}
并称它为
CONFIG_PATH=~/config1 test_func --verbose
export CONFIG_PATH=~/config2
test_func --quiet
另请注意,您不能向上导出内容,即不能调用set_function.sh
以便test_func
导出到调用外壳。你需要. set_function.sh
或同样source set_function.sh
代替。
推荐阅读
- ruby-on-rails - Rails 5资源路由错误仅在生产中
- javascript - HTML5 + JS 转到下一个视频功能未激活
- c++ - 从类模板中提取模板模板参数和可变参数模板参数
- javascript - Select2 事件 on("change") 返回空数据参数
- javascript - 我想在javascript中将用户输入日期格式更改为字符串
- hibernate - 我们如何在 hibernate 5 和 payara 5 [glassfish] 中记录 sql 语句
- c# - 如何验证 TransactionScope 是否适用于 MySQL?
- android - 为什么要使用 setX 和 setY?
- react-native - 从屏幕传递数据到屏幕不刷新componentDidMount
- django - Django CSRF 错误:仅在本地运行服务器上