bash 如何访问函数内调用者的命令行参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2740906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:14:38  来源:igfitidea点击:

How to access command line arguments of the caller inside a function?

bashcommand-line-argumentsargv

提问by DonGar

I'm attempting to write a function in bash that will access the scripts command line arguments, but they are replaced with the positional arguments to the function. Is there any way for the function to access the command line arguments if they aren't passed in explicitly?

我试图在 bash 中编写一个函数来访问脚本命令行参数,但它们被替换为函数的位置参数。如果未显式传入命令行参数,该函数是否有任何方法可以访问它们?

# Demo function
function stuff {
  echo 
#!/bin/bash

function argv {
    for a in ${BASH_ARGV[*]} ; do
      echo -n "$a "
    done
    echo
}

function f {
    echo f   
    echo -n f ; argv
}

function g {
    echo g   
    echo -n g; argv
    f
}

f boo bar baz
g goo gar gaz
$* } # Echo's the name of the script, but no command line arguments stuff # Echo's everything I want, but trying to avoid stuff $*

采纳答案by mcarifio

My reading of the bash ref manual says this stuff is captured in BASH_ARGV, although it talks about "the stack" a lot.

我对 bash ref 手册的阅读说这些东西是在 BASH_ARGV 中捕获的,尽管它经常谈到“堆栈”。

$ ./f.sh arg0 arg1 arg2
f boo bar baz
farg2 arg1 arg0 
g goo gar gaz
garg2 arg1 arg0 
f
farg2 arg1 arg0 

Save in f.sh

保存在 f.sh

args=("$@")
echo $# arguments passed
echo ${args[0]} ${args[1]} ${args[2]}

回答by stigi

If you want to have your arguments C style (array of arguments + number of arguments) you can use $@and $#.

如果您想拥有 C 风格的参数(参数数组 + 参数数量),您可以使用$@and $#

$#gives you the number of arguments.
$@gives you all arguments. You can turn this into an array by args=("$@").

$#给你参数的数量。
$@给你所有的论据。您可以通过 将其转换为数组args=("$@")

So for example:

例如:

#!/usr/bin/env bash

echo name of script is 
my_function() {
    echo "stored arguments:"
    for arg in "${commandline_args[@]}"; do
        echo "    $arg"
    done
}

commandline_args=("$@")

my_function
echo first argument is echo second argument is echo seventeenth argument is echo number of arguments is $#

Note that here ${args[0]}actually is the 1st argument and not the name of your script.

请注意,这里${args[0]}实际上是第一个参数,而不是脚本的名称。

回答by Ravi Vyas

# Save the script arguments
SCRIPT_NAME=
#!/bin/bash
# script_name function_test.sh
function argument(){
for i in $@;do
    echo $i
done;
}
argument $@
ARG_1= ARGS_ALL=$* function stuff { # use script args via the variables you saved # or the function args via $ echo
./function_test.sh argument1 argument2
$* } # Call the function with arguments stuff 1 2 3 4

Edit: please see my comment on question

编辑:请参阅我对问题的评论

回答by Cascabel

Ravi's comment is essentially the answer. Functions take their own arguments. If you want them to be the same as the command-line arguments, you must pass them in. Otherwise, you're clearly calling a function without arguments.

Ravi 的评论基本上就是答案。函数接受自己的参数。如果您希望它们与命令行参数相同,则必须将它们传入。否则,您显然是在调用没有参数的函数。

That said, you could if you like store the command-line arguments in a global array to use within other functions:

也就是说,如果您喜欢将命令行参数存储在全局数组中以在其他函数中使用,则可以:

#!/bin/bash
function print()
{
    while [ $# -gt 0 ]
    do
        echo ;
        shift 1;
    done
}
print $*;

You have to access the command-line arguments through the commandline_argsvariable, not $@, $1, $2, etc., but they're available. I'm unaware of any way to assign directly to the argument array, but if someone knows one, please enlighten me!

你必须通过访问命令行参数commandline_args变量,而不是$@$1$2等,但他们提供。我不知道有什么方法可以直接分配给参数数组,但如果有人知道,请赐教!

Also, note the way I've used and quoted $@- this is how you ensure special characters (whitespace) don't get mucked up.

另外,请注意我使用和引用的方式$@- 这是您确保特殊字符(空格)不会被弄乱的方式。

回答by Peter Coulton

##代码##

回答by Vikash Singh

One can do it like this as well

一个人也可以这样做

##代码##

Now call your script like

现在调用你的脚本

##代码##

回答by cabonamigo

My solution:

我的解决方案:

Create a function script that is called earlier than all other functions without passing any arguments to it, like this:

创建一个比所有其他函数更早调用的函数脚本,而不向其传递任何参数,如下所示:

! /bin/bash

function init(){ ORIGOPT= "- $@ -" }

!/bin/bash

函数 init(){ ORIGOPT="-$@-"}

Afer that, you can call init and use the ORIGOPT var as needed,as a plus, I always assign a new var and copy the contents of ORIGOPT in my new functions, that way you can keep yourself assured nobody is going to touch it or change it.

之后,您可以根据需要调用 init 并使用 ORIGOPT 变量,另外,我总是分配一个新的变量并在我的新函数中复制 ORIGOPT 的内容,这样您就可以确保没有人会碰它或更改。

I added spaces and dashes to make it easier to parse it with 'sed -E' also bash will not pass it as reference and make ORIGOPT grow as functions are called with more arguments.

我添加了空格和破折号,以便使用“sed -E”更容易地解析它,而且 bash 不会将它作为参考传递,并使 ORIGOPT 随着使用更多参数调用的函数而增长。

回答by Marin Alcaraz

You can use the shift keyword (operator?) to iterate through them. Example:

您可以使用 shift 关键字(运算符?)来遍历它们。例子:

##代码##