我有以下 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
最佳答案
开始于
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/