如何将传递给我的 bash 脚本的所有参数传递给我的函数? [英] How to pass all arguments passed to my bash script to a function of mine?

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

问题描述

假设我已经定义了一个 函数 abc(),它将处理与分析传递给我的脚本的参数相关的逻辑.

Let's say I have defined a function abc() that will handle the logic related to analyzing the arguments passed to my script.

如何将我的 bash 脚本收到的所有参数传递给它?参数的数量是可变的,所以我不能像这样对传递的参数进行硬编码:

How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode the arguments passed like this:

abc $1 $2 $3 $4

编辑.更好的是,我的函数有什么方法可以访问脚本参数的变量吗?

Edit. Better yet, is there any way for my function to have access to the script arguments' variables?

推荐答案

$@ 变量扩展到所有以空格分隔的命令行参数.这是一个例子.

The $@ variable expands to all command-line parameters separated by spaces. Here is an example.

abc "$@"

当使用 $@ 时,你应该(几乎)总是把它放在双引号中以避免错误解析包含空格或通配符的参数(见下文).这适用于多个参数.它还可以移植到所有符合 POSIX 标准的 shell.

When using $@, you should (almost) always put it in double-quotes to avoid misparsing of arguments containing spaces or wildcards (see below). This works for multiple arguments. It is also portable to all POSIX-compliant shells.

还值得注意的是,$0(通常是脚本的名称或路径)不在 $@ 中.

It is also worth noting that $0 (generally the script's name or path) is not in $@.

Bash 参考手册特殊参数部分$@ 扩展为从 1 开始的位置参数.当扩展发生在双引号内时,每个参数都扩展为一个单独的词.即 "$@" 等价于 "$1"$2"$3"....

The Bash Reference Manual Special Parameters Section says that $@ expands to the positional parameters starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is "$@" is equivalent to "$1" "$2" "$3"....

如果您想传递除第一个之外的所有参数,您可以先使用 shift 来消费";第一个参数,然后通过 "$@" 将剩余的参数传递给另一个命令.在 bash(以及 zsh 和 ksh,但不是像 dash 这样的普通 POSIX shell)中,您可以使用数组切片的变体来执行此操作,而不会弄乱参数列表:"${@:3}" 将为您提供以 "$3" 开头的参数."${@:3:4}" 将为您提供从 "$3" 开始的最多四个参数(即 "$3";$4"、$5"、$6"),如果传递了那么多参数.

If you want to pass all but the first arguments, you can first use shift to "consume" the first argument and then pass "$@" to pass the remaining arguments to another command. In bash (and zsh and ksh, but not in plain POSIX shells like dash), you can do this without messing with the argument list using a variant of array slicing: "${@:3}" will get you the arguments starting with "$3". "${@:3:4}" will get you up to four arguments starting at "$3" (i.e. "$3" "$4" "$5" "$6"), if that many arguments were passed.

"$*" 将所有参数放在一个字符串中(用空格分隔,或者 $IFS 的第一个字符是什么).这消除了in 参数中的空格和 参数之间的空格之间的区别,因此通常是一个坏主意.尽管打印参数可能没问题,例如echo "$*",前提是您不关心保留区分内/之间的空间.

"$*" gives all of the arguments stuck together into a single string (separated by spaces, or whatever the first character of $IFS is). This looses the distinction between spaces within arguments and the spaces between arguments, so is generally a bad idea. Although it might be ok for printing the arguments, e.g. echo "$*", provided you don't care about preserving the space within/between distinction.

将参数分配给常规变量(如在 args=$@" 中)将所有参数混合在一起,就像 $*" 所做的那样.如果要将参数存储在变量中,请使用带有 args=("$@") 的数组(括号使其成为数组),然后将它们引用为例如"${args[0]}" 等等.注意在bash和ksh中,数组索引从0开始,所以$1会在args[0] 等等.另一方面,zsh 从 1 开始数组索引,所以 $1 将在 args[1] 中.而像 dash 这样的更基本的 shell 根本没有数组.

Assigning the arguments to a regular variable (as in args="$@") mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc. Note that in bash and ksh, array indexes start at 0, so $1 will be in args[0], etc. zsh, on the other hand, starts array indexes at 1, so $1 will be in args[1]. And more basic shells like dash don't have arrays at all.

使用 $@$* 去掉双引号,将尝试将每个参数拆分为单独的单词(基于空格或 $IFS),并尝试将任何看起来像文件名通配符的内容扩展为匹配的文件名列表.这可能会产生非常奇怪的效果,并且几乎总是应该避免的.(除了在 zsh 中,默认情况下不会进行此扩展.)

Leaving off the double-quotes, with either $@ or $*, will try to split each argument up into separate words (based on whitespace or whatever's in $IFS), and also try to expand anything that looks like a filename wildcard into a list of matching filenames. This can have really weird effects, and should almost always be avoided. (Except in zsh, where this expansion doesn't take place by default.)

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

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