shell 脚本传递参数 space
shell script pass argument with space
我有以下脚本(示例):
#!/bin/bash
while getopts a: opt; do
case "$opt" in
a) val="$OPTARG";;
?) echo "use the flag \"-a\""
exit 2;;
esac
done
echo "a specified with: ${val}"
当我现在用 test.sh -a "here is a string"
调用此脚本时,输出是:a specified with: here
但 不是 ,因为我希望 a specified with: here is a string
。
我知道我可以用 test.sh -a here\ is\ a\ string
或 test.sh -a "here\ is\ a\ string"
调用脚本,它会起作用。但在我的例子中,我无法操纵我想要传递的字符串。
那么我怎样才能改变我的 getopts
函数使其工作呢?
我也试过getopt
,但我工作得更糟:
commandsShort="a:"
commandsLong="aval:"
TEMP=`getopt \
-o $commandsShort \
-l $commandsLong \
-q \
-n "[=13=]" -- "$@"`
我做错了什么?
这已在对您的问题的评论中得到解决。 :-)
您正在调用脚本:
eval "test.sh $@"
此 "eval" 行的效果(如果 "here is a string" 是您的选项)是创建引号中的命令行:
test.sh here is a string
并评估评价它。
根据附加评论,如果可以避免 eval,则应该避免。
就是说,如果您需要它,您总是可以在 eval 中引用字符串:
eval "test.sh \"$@\""
或者,如果您不喜欢转义引号,请使用单引号,因为您的 $@
将由于外引号为双引号而扩展:
eval "test.sh '$@'"
最后,正如您在评论中提到的,直接 运行 可能是最好的选择:
test.sh "$@"
注意 如果您的 $@
包含 -a
选项,您可能会遇到新问题。考虑命令行:
test.sh "-a here is a string"
在这种情况下,从 -a
开始的整个字符串都在
中找到,并且您将没有 getopts 选项,也没有 OPTARG。
我有以下脚本(示例):
#!/bin/bash
while getopts a: opt; do
case "$opt" in
a) val="$OPTARG";;
?) echo "use the flag \"-a\""
exit 2;;
esac
done
echo "a specified with: ${val}"
当我现在用 test.sh -a "here is a string"
调用此脚本时,输出是:a specified with: here
但 不是 ,因为我希望 a specified with: here is a string
。
我知道我可以用 test.sh -a here\ is\ a\ string
或 test.sh -a "here\ is\ a\ string"
调用脚本,它会起作用。但在我的例子中,我无法操纵我想要传递的字符串。
那么我怎样才能改变我的 getopts
函数使其工作呢?
我也试过getopt
,但我工作得更糟:
commandsShort="a:"
commandsLong="aval:"
TEMP=`getopt \
-o $commandsShort \
-l $commandsLong \
-q \
-n "[=13=]" -- "$@"`
我做错了什么?
这已在对您的问题的评论中得到解决。 :-)
您正在调用脚本:
eval "test.sh $@"
此 "eval" 行的效果(如果 "here is a string" 是您的选项)是创建引号中的命令行:
test.sh here is a string
并评估评价它。
根据附加评论,如果可以避免 eval,则应该避免。
就是说,如果您需要它,您总是可以在 eval 中引用字符串:
eval "test.sh \"$@\""
或者,如果您不喜欢转义引号,请使用单引号,因为您的 $@
将由于外引号为双引号而扩展:
eval "test.sh '$@'"
最后,正如您在评论中提到的,直接 运行 可能是最好的选择:
test.sh "$@"
注意 如果您的 $@
包含 -a
选项,您可能会遇到新问题。考虑命令行:
test.sh "-a here is a string"
在这种情况下,从 -a
开始的整个字符串都在 中找到,并且您将没有 getopts 选项,也没有 OPTARG。