将多个参数传递给 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22120851/
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
Pass Multiple Arguments to Bash Script
提问by Kohjah Breese
I have a bash script, which I want to call like this:
我有一个 bash 脚本,我想这样调用它:
bash curl.sh http://www.google.co.uk/ -d Directory -a "Moz 123" -r http://localhost/
I can collect the first argument (http://www.google.co.uk/), with the following:
我可以收集第一个参数(http://www.google.co.uk/),如下:
url=
while getopts p:d:a:r: opt; do
case $opt in
p) proxy=$OPTARG ;;
d) dir=$OPTARG ;;
a) ua=$OPTARG ;;
r) ref=$OPTARG ;;
esac
done
However, it does not pick up the other -arguments. If I remove 'http://www.google.co.uk/' as the first argument, it picks up the -arguments.
但是,它不会选择其他参数。如果我删除“ http://www.google.co.uk/”作为第一个参数,它会选择 -arguments。
Due to logistics, I am not able to set the first argument, e.g. 'http://www.google.co.uk/' with -u etc.
由于物流原因,我无法设置第一个参数,例如“ http://www.google.co.uk/”和 -u 等。
How do you get this to work?
你如何让这个工作?
回答by user3360167
Why not pass arguments in, wrapped in quotes?
为什么不传入参数,用引号括起来?
e.g.
例如
script.shl "http://www.google.com" "/var/www/test" "option 3"
...then you can just access them directly in the script using $1 $2 $3. You can still alter the course of the script by just using if...else?
...然后您可以直接在脚本中使用 $1 $2 $3 访问它们。您仍然可以通过仅使用 if...else 来更改脚本的进程?
Unless, I've mis-read your question...
除非,我误读了你的问题......
回答by Kohjah Breese
In my case, this seems the best option:
就我而言,这似乎是最好的选择:
Call the script like:
调用脚本如下:
bash curl.sh -d Remote -a "Moz 123" -r http://localhost http://www.google.com/
You can pick the last argument up like:
您可以选择最后一个参数,例如:
url=${!#}
And then the other options using getopts, as above.
然后使用 getopts 的其他选项,如上所述。
回答by chepner
getopts
stops parsing as soon as it sees an argument that does not begin with a hyphen. You'll have to change the order in which you pass the arguments, or use the GNU version of the external getopt
program, which can handle regular arguments mixed in with options.
getopts
一旦看到不以连字符开头的参数,就会停止解析。您必须更改传递参数的顺序,或者使用 GNU 版本的外部getopt
程序,它可以处理与选项混合的常规参数。
I think the following should work (it's modeled on your code and an example at http://linuxaria.com/howto/parse-options-in-your-bash-script-with-getopt?lang=en). Essentially, getopt
just reorders the arguments and breaks apart any combine options (like changing -xyz
to -x -y -z
). Any non-optional arguments will be found after --
in the parsed option list.
我认为以下应该有效(它以您的代码和http://linuxaria.com/howto/parse-options-in-your-bash-script-with-getopt?lang=en 上的示例为模型)。本质上,getopt
只需重新排列参数并拆分任何组合选项(例如更改-xyz
为-x -y -z
)。--
在解析的选项列表中将找到任何非可选参数。
PARSED_OPTIONS=$( getopt -o "p:d:a:r:" -- "$@" )
eval set -- "$PARSED_OPTIONS"
while true; do
case in
p) proxy=; shift 2 ;;
d) dir=; shift 2 ;;
a) ua=; shift 2 ;;
r) ref=; shift 2;;
--) shift; break ;;
esac
done