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

标签 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

最佳答案

开始于

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/

相关文章:

linux - 为具有管道的 watch 命令添加别名

bash - POSIX 风格操作系统中的命令行选项应该是下划线风格吗?

linux - 如何使用 shell 脚本等待另一个进程直到它在 Linux 中完成

shell - ${arr[@]} 在 sed 命令中

linux - 是否可以在 Pharo smalltalk 中编写 shell 命令?

linux - 启用 Systemd 的服务不再在启动时启动

regex - AWK 为逗号和引号设置多个分隔符

linux - 找到匹配字符串的模式并将整个模式发送到输出

bash - awk 命令获取进程名称

linux - 顶部 : counting the number of processes belonging to a user