【发布时间】:2015-03-04 22:17:29
【问题描述】:
我在 shell 脚本中解析了一个命令行参数,如下所示:
cmd --a=hello world good bye --b=this is bash script
我需要解析“a”的参数,即“hello world ...”,它们由空格分隔成一个数组。
即 a_input() 数组应包含“hello”、“world”、“good”和“bye”。
同样适用于“b”参数。
我试过如下:
--a=*)
a_input={1:4}
a_input=$@
for var in $a_input
#keep parsing until next --b or other argument is seen
done
但是上面的方法很粗糙。任何其他解决方法。我不能使用 getopts。
【问题讨论】:
-
更好的攻击途径是要求用户引用多词参数:
cmd --a='hello world good bye' --b='this is bash script'。那会更加地道。
标签: bash shell unix scripting scripting-language