简体   繁体   English

将空命令行传递到源脚本

[英]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. 我有两个解决方案,但对我来说都不好。

  1. 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 
  2. 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.

相关问题 将命令行参数传递给通过unix可执行文件调用的python脚本 - Pass command line arguments to python script that is called through unix executable 如何通过shell脚本中的命令行在Expect中传递参数 - How to pass argument in Expect through the command line in a shell script Shell脚本:如何将脚本的命令行参数传递给它调用的命令? - Shell Script :How to pass script's command-line arguments through to commands that it invokes? 如何将 arguments 传递给 source 命令调用的脚本? - How to pass arguments to a script invoked by source command? mv命令可在命令行上单独运行,但不能通过脚本运行 - mv command works individually on command line but not through script 传递 bash 脚本的 output 作为另一个脚本的命令行参数 - Pass output of a bash script as command line argument for another script 如何通过-d选项传递小时和分钟到日期命令 - How to pass hours and minutes to date command through -d option 当进程通过脚本运行时,jobs 命令结果为空 - jobs command result is empty when process is run through script 如何通过命令行将变量传递给 composer.json - How to pass a variable to composer.json through the command line 如何通过bash中的变量传递带有空格的命令行参数 - How to pass command line arguments with spaces through a variable in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM