通过 bash 脚本将参数传递给 /bin/bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12059528/
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-18 03:05:36  来源:igfitidea点击:

Passing arguments to /bin/bash via a bash script

bash

提问by JCOidl

I am writing a bash script that takes a number of command line arguments (possibly including spaces) and passes all of them to a program (/bin/some_program) via a login shell. The login shell that is called from the bash script will depend on the user's login shell. Let's suppose the user uses /bin/bash as their login shell in this example... but it might be /bin/tcsh or anything else.

我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将所有这些参数传递给一个程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取决于用户的登录 shell。让我们假设用户在这个例子中使用 /bin/bash 作为他们的登录 shell……但它可能是 /bin/tcsh 或其他任何东西。

If I know how many arguments will be passed to some_program, I can put the following lines in my bash script:

如果我知道将有多少参数传递给 some_program,我可以在我的 bash 脚本中加入以下几行:

#!/bin/bash
# ... (some lines where we determine that the user's login shell is bash) ...
/bin/bash --login -c "/bin/some_program \"\" \"\""

and then call the above script as follows:

然后调用上面的脚本如下:

my_script "this is too" cool

With the above example I can confirm that some_program receives two arguments, "this is too" and "cool".

通过上面的例子,我可以确认 some_program 收到两个参数,“这太”和“酷”。

My question is... what if I don't know how many arguments will be passed? I'd like to pass all the arguments that were sent to my_script along to some_program. The problem is I can't figure out how to do this. Here are some things that don't work:

我的问题是...如果我不知道将传递多少个参数怎么办?我想将发送到 my_script 的所有参数传递给 some_program。问题是我不知道如何做到这一点。以下是一些不起作用的事情:

/bin/bash --login -c "/bin/some_program $@"     # --> 3 arguments: "this","is","too"
/bin/bash --login -c /bin/some_program "$@"     # --> passes no arguments

回答by themel

Quoting the bash manual for -c:

引用 bash 手册-c

If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

如果存在 -c 选项,则从字符串中读取命令。如果字符串后面有参数,则将它们分配给位置参数,从 $0 开始

Works for me:

对我有用:

$ cat x.sh
#!/bin/bash
/bin/bash --login -c 'echo 1: 2: 3:' echo "$@"
$ ./x.sh "foo bar" "baz" "argh blargh quargh"
1:foo bar 2:baz 3:argh blargh quargh

I don't know how you arrived at the "passes no arguments" conclusion, maybe you missed the $0bit?

我不知道你是如何得出“不通过论证”的结论的,也许你错过了这$0一点?

回答by geirha

Avoid embedding variables into other scripts, pass them on as arguments instead. In this case:

避免将变量嵌入到其他脚本中,而是将它们作为参数传递。在这种情况下:

bash --login -c 'some_program "$@"' some_program "$@"

The first argument after -c '...' is taken as $0, so I just put in some_program there.

-c '...' 之后的第一个参数被视为 $0,所以我只是把 some_program 放在那里。

On a side note, it's an odd requirement to require a login shell. Doesn't the user log in?

附带说明一下,需要登录 shell 是一个奇怪的要求。用户没有登录吗?