请参阅下面的编辑,谢谢
我有以下测试脚本(重要提示:我无法更改这部分):
while (($#)); do
echo $1
shift
done
运行该命令
./test aaa "bbbb cccc" ddd
会产生以下输出:
aaa
bbbb cccc
ddd
这是任何理智的人都会期望的。
我还有另一个脚本:
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
# ----
PARAMS=""
// $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS="$PARAMS --$1 \"$2\""
fi
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
echo ./test $PARAMS
./test $PARAMS
的输出echo
是./test --mode "good" --status "okay" --name "bro" --description "very good man dood"
,所以我期望的输出./test $PARAMS
是
--mode
good
--status
okay
--name
bro
--description
very good man dood
但由于某种原因,我得到以下输出:
--mode
"good"
--status
"okay"
--name
"bro"
--description
"very
good
man
dood"
如果我复制 的输出echo ./test $PARAMS
并粘贴它,我会得到预期的输出./test
。所以我尝试删除执行的最后一行./test
并将该echo
行保留在最后,但显然$(./script)
仍然不起作用,而且我无法理解。
我究竟做错了什么?
编辑:@steeldriver 的解决方案有效,但还有另一个限制 - 我必须允许用户发送自己的参数。
所以有了这个脚本(感谢@steeldriver):
#!/bin/bash
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
arguments="--config \"blablabla=yes\" --config2 \"bla2=no problem\""
# ----
declare -a PARAMS
# $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS+=("--$1" "$2")
fi
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
# (1)
PARAMS+=("$arguments")
# (2)
PARAMS+=($arguments)
echo ./test "${PARAMS[@]}"
./test "${PARAMS[@]}"
期望的输出是:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
blablabla=yes
--config2
bla2=no problem
然而我得到的输出是:
和(1)
:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config "blablabla=yes" --config2 "bla2=no problem"
和(2)
:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
"blablabla=yes"
--config2
"bla2=no
problem"
非常感激!
答案1
使用数组而不是字符串变量:
#!/bin/bash
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
# ----
declare -a PARAMS
# $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS+=("--$1" "$2")
fi
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
echo ./test "${PARAMS[@]}"
./test "${PARAMS[@]}"
测试
$ ./other.sh
./test --mode good --status okay --name bro --description very good man dood
--mode
good
--status
okay
--name
bro
--description
very good man dood
答案2
感谢 @steeldriver 的提醒,我成功地正确添加了用户参数并获得了所需的输出。现在这是我的脚本:
#!/bin/bash
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
arguments="--config \"blablabla=yes\" --config2 \"bla2=no problem\""
# ----
declare -a PARAMS
# $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS+=("--$1" "$2")
fi
}
# This function right here
function addUserArguments {
while (($#)); do
PARAMS+=("$1")
shift
done
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
# And this line right here
eval addUserArguments $arguments
./test "${PARAMS[@]}"
输出是
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
blablabla=yes
--config2
bla2=no problem