BASH: - 将字符串解析为单独的命令行参数

时间:2016-08-04 03:47:45

标签: linux bash shell command-line scripting

我有以下bash代码,并希望将字符串转换为命令行参数以传递给其他程序。

所以我有GETVARS,想分开并做一些像

这样的事情

./ somecommand $ GETVARS [0] $ GETVARS [1]

等等

GETVARS将是任意长度的元素。

  GETVARS = ""
        for id in {100..500..10}
            do
                for letter in A B C D E F
                do
                    GETVARS=$GETVARS"\":${id}:${letter}\" "
                done
        done
  //GETVARS = "":100:A" "100:B" "100:C"" .. and so on

1 个答案:

答案 0 :(得分:0)

开始
getvars="" # no spaces around commas, use smaller case variable names

根据要求,您显然正在寻找像

这样的简单数组
getvars=() # or do declare -a getvars

我不清楚这个要求,但下面就是我想你应该做的事情

for id in {100..500..10}
  do
for letter in A B C D E F
  do
    getvars+=( \":${id}:${letter}\" ) # adding elements to array
 done
done
#and later do the following
./somecommand "${getvars[@]}" # Each element will be separated to a word