2016-08-04 128 views
0

我有以下bash代碼,並希望將該字符串轉換爲傳遞給不同程序的命令行參數。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 

回答

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 
+0

謝謝!那是我需要的 – user3896519