bash 从 ARGV 访问变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780682/
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
Accessing variable from ARGV
提问by robjmills
I'm writing a cPanel postwwwact script, if you're not familiar with the script its run after a new account is created. it relies on the user account variable being passed to the script which i then use for various things (creating databases etc). However, I can't seem to find the right way to access the variable i want. I'm not that good with shell scripts so i'd appreciate some advice. I had read somewhere that the value i wanted would be included in $ARGV{'user'} but this simply gives "root" as opposed to the value i need. I've tried looping through all the arguments (list of arguments here) like this:
我正在编写一个 cPanel postwwwact 脚本,如果您不熟悉它在创建新帐户后运行的脚本。它依赖于传递给脚本的用户帐户变量,然后我将其用于各种事情(创建数据库等)。但是,我似乎找不到访问我想要的变量的正确方法。我不太擅长 shell 脚本,所以我很感激一些建议。我在某处读到我想要的值将包含在 $ARGV{'user'} 中,但这只是给出了“root”而不是我需要的值。我试过像这样循环遍历所有参数(这里的参数列表):
#!/bin/sh
for var
do
touch /root/testvars/$var
done
and the value i want is in there, i'm just not sure how to accurately target it. There's info hereon doing this with PHP or Perl but i have to do this as a shell script.
我想要的价值就在那里,我只是不确定如何准确定位它。有信息在这里与PHP或Perl这样做,但我必须这样做,因为shell脚本。
EDITIdeally i would like to be able to call the variable by something other than $1 or $2 etc as this would create issues if an argument is added or removed
编辑理想情况下,我希望能够通过 $1 或 $2 等以外的其他方式调用变量,因为如果添加或删除参数,这会产生问题
..for example in the PHP code here:
..例如在此处的 PHP 代码中:
function argv2array ($argv) {
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
return $opts;
}
// allows you to do the following:
$opts = argv2array($argv);
echo $opts[‘user'];
Any ideas?
有任何想法吗?
采纳答案by Paused until further notice.
The parameters are passed to your script as a hash:
参数作为散列传递给您的脚本:
/scripts/$hookname user $user password $password
You can use associative arrays in Bash 4, or in earlier versions of Bash you can use built up variable names.
您可以在 Bash 4 中使用关联数组,或者在 Bash 的早期版本中,您可以使用构建的变量名称。
#!/bin/bash
# Bash >= 4
declare -A argv
for ((i=1;i<=${#@};i+=2))
do
argv[${@:i:1}]="${@:$((i+1)):1}"
done
echo ${argv['user']}
Or
或者
#!/bin/bash
# Bash < 4
for ((i=1;i<=${#@};i+=2))
do
declare ARGV${@:i:1}="${@:$((i+1)):1}"
done
echo ${!ARGV*} # outputs all variable names that begin with ARGV
echo $ARGVuser
Running either:
运行:
$ ./argvtest user dennis password secret
dennis
Note: you can also use shiftto step through the arguments, but it's destructive and the methods above leave $@($1, $2, etc.) in place.
注意:您还可以使用shift通过参数的步骤,但它的破坏性及以上产假的方法$@($1,$2到位,等等)。
#!/bin/bash
# Bash < 4
# using shift (can use in Bash 4, also)
for ((i=1;i<=${#@}+2;i++))
do
declare ARGV=""
# Bash 4: argv[}]=""
shift 2
done
echo ${!ARGV*}
echo $ARGVuser
回答by Tyler McHenry
Why not start off your script with something like
为什么不从你的脚本开始
ARG_USER=
ARG_FOO=
ARG_BAR=
And then later in your script refer to $ARG_USER, $ARG_FOOand $ARG_BARinstead of $1, $2, and $3. That way, if you decide to change the order of arguments, or insert a new argument somewhere other than at the end, there is only one place in your code that you need to update the association between argument order and argument meaning.
然后稍后在您的脚本中引用$ARG_USER, $ARG_FOOand$ARG_BAR而不是$1, $2, and $3。这样,如果您决定更改参数的顺序,或在末尾以外的其他位置插入新参数,则您的代码中只有一个地方需要更新参数顺序和参数含义之间的关联。
You could even do more complex processing of $*to set your $ARG_WHATEVERvariables, if it's not always going to be that all of the are specified in the same order every time.
你甚至可以做更复杂的处理$*来设置你的$ARG_WHATEVER变量,如果不是每次都以相同的顺序指定所有变量。
回答by unwind
If it's passed as a command-line parameter to the script, it's available as $1if it's first parameter, $2for the second, and so on.
如果它作为命令行参数传递给脚本,则它可以用作$1第一个参数,也可以$2用作第二个参数,依此类推。
回答by Gaurav Vaish
You can do the following:
您可以执行以下操作:
#!/bin/bash
for var in $argv; do
<do whatver you want with $var>
done
And then, invoke the script as:
然后,调用脚本为:
$ /path/to/script param1 arg2 item3 item4 etc

