[英]Pass an empty command line through to a source'd script
My bash script expects command line arguments that I parse without any problem. 我的bash脚本期望我解析的命令行参数没有任何问题。 However the script later calls an other script which also accepts positional parameters, but providing parameters is not mandatory.
但是,该脚本随后调用另一个也接受位置参数的脚本,但是提供参数不是强制性的。
When my script source
s the other script without arguments the other script still tries to parse those arguments that were given to my script in the first place. 当我的脚本
source
是另一个没有参数的脚本时,另一个脚本仍会尝试解析最初给我的脚本提供的那些参数。
I have two solutions, but none of them looks good to me. 我有两个解决方案,但对我来说都不好。
With shift
: 带
shift
:
# now making this script to handle branches: OPTIND=1 while getopts "b:" opt; do case "$opt" in 'b' ) branch=$OPTARG ;; esac shift done
Passing empty argument to the second script: 将空参数传递给第二个脚本:
source ${MINICONDA}/bin/activate ""
Note that this isn't needed for normal calls of a script as an external process, but only for invocations with source
or .
请注意,对于作为外部进程的脚本的正常调用,不需要此操作,而对于使用
source
或的调用仅需要此操作.
. 。
To provide context to readers, from bash's built-in help to source
: 为了向读者提供上下文,从bash的内置帮助到
source
:
If any arguments are supplied, they become the positional parameters when filename is executed.
如果提供了任何参数,则在执行文件名时它们将成为位置参数。 Otherwise the positional parameters are unchanged.
否则,位置参数将保持不变。
The easiest way to control the argument list is using set -- arg1 arg2 ...
to immediately overwrite it with a new list. 控制参数列表的最简单方法是使用
set -- arg1 arg2 ...
立即用新列表覆盖它。 Thus: 从而:
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.