使用环境变量传递参数给命令 [英] Using an environment variable to pass arguments to a command
问题描述
我试图写一个bash脚本,需要一个环境变量,并将它传递给命令。
I'm trying to write a bash script that takes an environment variable and passes it along to a command.
所以,如果我有这样的事情:
So if I had something like:
export OUT="-a=arg1 -b=\"arg2.0 arg2.1\""
我想在我的bash脚本做这样的事情:
I want in my bash script to do something like:
<command> -a=arg1 '-b=arg2.0 arg2.1'
我有一个办法,似乎要做到这一点,但它涉及到使用eval:
I have one approach that seems to do this, but it involves using eval:
eval <command> ${OUT}
如果我包括设置-x
对的命令,我会看到:
If I include set -x
right about the command, I will see:
+ eval <command> a=arg1 'b="arg2.0' 'arg2.1"'
++ <command> -a=arg1 '-b=arg2.0 arg.1'
不过,我周围戳使用eval的危险,因为这将采取从用户的输入参数,它是不够理想。
However, I've poked around the dangers of using eval and since this will be taking the arguments from user input, it's less than ideal.
由于这是bash中,我已经使用数组来存储我的论点,并简单地说也算:&LT;指挥GT; $ ARRAY [@]
做我想做的。我一直在试图使用IFS,但我不知道我应该被拆分的。
Since this is bash, I've also considered using arrays to store my arguments and simply put: <command> "$ARRAY[@]"
to do what I want. I've been trying to use IFS, but I'm not sure what I should be splitting on.
推荐答案
如果你不是完全僵化关于 $ OUT
,一种可能是重复的格式在选项=
字符串,以便串联。然后,你会写:
If you're not completely inflexible about the format of $OUT
, one possibility would be to repeat the option=
string to allow for concatenation. Then you'd write:
export OUT="a=arg1 b=arg2.0 b=arg2.1"
如果是可以接受的,下面的脚本将工作
If that is acceptable, the following script will work
#!/bin/bash
# Parse $OUT into an associative array.
# Instead of using $OUT, it would be cleaner to use "$@".
declare -A args
for arg in $OUT; do
if [[ "$arg" =~ ^([[:alnum:]]+)=(.*)$ ]]; then
key=${BASH_REMATCH[1]}
val=${BASH_REMATCH[2]}
if [[ -z ${args[$key]} ]]; then
args[$key]=-$key="$val"
else
args[$key]+=" $val"
fi
fi
done
# Test, approximately as specified
command() { :; }
set -x
command "${args[@]}"
set +x
我不能说我喜欢它不多,但它是最接近我已经能来。
I can't say I like it much, but it's the closest I've been able to come.
下面是一个运行示例:
$ export OUT="a=foo b=bar b=glitch s9= s9=* "
./command-runner
+ command -a=foo '-b=bar glitch' '-s9= *'
+ :
+ set +x
如果您导入bash的功能(例如,在你的bash的启动文件),你可以更好地利用阵列。这里有一个方法:
If you import a bash function (for example, in your bash startup file), you can make much better use of arrays. Here's one approach:
# This goes into your bash startup file:
declare -a SAVED_ARGS
save_args() {
SAVED_ARGS=("$@")
}
do_script() {
/path/to/script.sh "${SAVED_ARGS[@]}" "$@"
}
为了进行说明, script.sh
:
#!/bin/bash
command() { :; }
set -x
command "${@/#/-}"
set +x
例如:
$ save_args x=3 y="a few words from our sponsor"
$ do_script a=3 b="arg2.0 arg2.1"
+ command -x=3 '-y=a few words from our sponsor' -a=3 '-b=arg2.0 arg2.1'
+ :
+ set +x
$ do_script a=42
+ command -x=3 '-y=a few words from our sponsor' -a=42
+ :
+ set +x
在情况下,它并不明显:
In case it's not obvious:
command() { :; }
定义命令
称为bash的功能,几乎没做什么(除了调用内置的:
它什么都不做),和
defines a bash function called command
which does almost nothing (except invoke the builtin :
which does nothing), and
"${@/#/-}"
扩展到位置参数,在每一个使用的开头插入一个破折号查找和替换替换。该模式#
实际上是一个空的模式,只有在字符串开始处匹配。
expands to the positional parameters, inserting a dash at the beginning of each one use a find-and-replace substitution. The pattern #
is actually an empty pattern which only matches at the beginning of the string.
这篇关于使用环境变量传递参数给命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!