我有以下bash代码,希望将字符串转换为命令行参数,以传递给其他程序。
所以我想分开做一些类似的事情
./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

最佳答案

首先

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

关于linux - BASH:-将字符串解析为单独的命令行参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38758175/

10-09 06:05