![](/img/trans.png)
[英]Pass command line arguments to python script that is called through unix executable
[英]Pass an empty command line through to a source'd script
我的bash脚本期望我解析的命令行参数没有任何问题。 但是,该脚本随后调用另一个也接受位置参数的脚本,但是提供参数不是强制性的。
当我的脚本source
是另一个没有参数的脚本时,另一个脚本仍会尝试解析最初给我的脚本提供的那些参数。
我有两个解决方案,但对我来说都不好。
带shift
:
# now making this script to handle branches: OPTIND=1 while getopts "b:" opt; do case "$opt" in 'b' ) branch=$OPTARG ;; esac shift done
将空参数传递给第二个脚本:
source ${MINICONDA}/bin/activate ""
请注意,对于作为外部进程的脚本的正常调用,不需要此操作,而对于使用source
或的调用仅需要此操作.
。
为了向读者提供上下文,从bash的内置帮助到source
:
如果提供了任何参数,则在执行文件名时它们将成为位置参数。 否则,位置参数将保持不变。
控制参数列表的最简单方法是使用set -- arg1 arg2 ...
立即用新列表覆盖它。 从而:
orig_args=( "$@" ) # back up the original arguments
set -- # clear the argument list
source whatever
set -- "${orig_args[@]}" # if you ever need them back
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.