如何在bash脚本中将可选参数传递给另一个命令? [英] How can I pass optional parameters to another command within a bash script?

查看:47
本文介绍了如何在bash脚本中将可选参数传递给另一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含一些可选参数的bash脚本.我想翻译它们并将它们传递给另一个脚本.但是,我很难优雅地传递可选参数.

I am writing a bash script that takes in some optional parameters. I want to translate them and pass them to another script. However, I'm having a hard time passing the optional parameters gracefully.

以下是我设法使用伪代码进行工作的概述:

Here's a outline of what I managed to get working in pseudocode:

a.sh:

if arg1 in arguments; then
    firstArg="first argument"
fi
if arg2 in arguments; then
    secondArg="second argument"
fi

./b.sh $firstArg $secondArg "default argument"

请注意参数中的空格.

b.sh:

for arg in "$@"
do
    echo $arg
done

我想调用 b.sh ,可以选择使用 firstArg secondArg 以及默认参数,例如:

I want to call b.sh, optionally with firstArg and secondArg and a default argument like so:

./b.sh $firstArg $secondArg "default argument"

这个问题是,如果 $ firstArg $ secondArg 是带空格的字符串,它们将被表示为多个参数,并且输出将类似于:

The problem with this is that if $firstArg or $secondArg are strings with spaces, they will be represented as multiple arguments, and the output will be something like:

first
argument
second
argument
default argument

好的,这很容易解决,让我们通过在引号周围加上引号来捕获参数的整个字符串,如下所示:

Okay, that's easy to fix, let's capture the entire string of the arguments by adding quotes around it like so:

./b.sh "$firstArg" "$secondArg" "defaultArg"

问题是,例如,如果未设置 firstArg ,则会导致空白行(因为它将把" 解释为参数),因此输出会是这样的:

Problem is if, for example, firstArg is not set, it results in a blank line (as it will interpret "" as a parameter), so the output will be something like:

(blank line here)
second argument
defaultArg

我也尝试过构造一个字符串并将其传递给shell脚本,但是它似乎也不起作用(它将整个字符串解释为一个参数,即使我用引号添加了单独的参数).

I've also tried constructing a string and passing it to the shell script, but it doesn't seem to work that way either (it interprets the whole string as an argument, even if I add separate the arguments with quotes).

请注意,从命令行使用引号将参数调用 b.sh 可以正常工作.有没有一种方法可以在bash脚本中模拟它的工作原理?

Note that calling b.sh from my command line with the arguments quoted works fine. Is there a way to mimic how this works from within a bash script?

推荐答案

如果您确实想复制给定的所有参数,但又添加一个:

If you literally want to copy all arguments given, but add one more:

# this works in any POSIX shell
./b.sh "$@" defaultArg

或者,显式地传递 firstArg secondArg ,但前提是它们存在(请注意,此处的set-to-empty-value计为"existing"):

Alternately, to explicitly pass firstArg and secondArg, but only if they exist (note that set-to-an-empty-value counts as "existing" here):

# this works in any POSIX shell
./b.sh ${firstArg+"$firstArg"} ${secondArg+"$secondArg"} defaultArg

如果您想将设置为空值的值视为不存在:

If you want to treat set-to-an-empty-value as not existing:

# this works in any POSIX shell
./b.sh ${firstArg:+"$firstArg"} ${secondArg:+"$secondArg"} defaultArg

另一种方法是建立参数数组:

An alternate approach is to build up an array of arguments:

# this requires bash or another shell with arrays and append syntax
# be sure any script using this starts with #!/bin/bash
b_args=( )
[[ $firstArg ]] && b_args+=( "$firstArg" )
[[ $secondArg ]] && b_args+=( "$secondArg" )
b_args+=( "default argument" )
./b.sh "${b_args[@]}"


如果您想要具有与数组方法相同的灵活性,但又没有兼容性问题的东西,请定义一个函数;在其中,您可以安全地覆盖"$ @" ,而不会影响脚本的其余部分:


If you want something with the same flexibility as the array method, but without the compatibility issues, define a function; within it, you can safely override "$@" without impacting the rest of the script:

runB() {
  set --
  [ -n "$firstArg" ]  && set -- "$@" "$firstArg"
  [ -n "$secondArg" ] && set -- "$@" "$secondArg"
  ./b.sh "$@" "default argument"
}

这篇关于如何在bash脚本中将可选参数传递给另一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆