如何拆分定界字符串以在 bash 中组成 dd 命令?

How to split a delimited string to compose a dd command in bash?

我想读取一个类似于下图所示的配置文件:

source/path:blocksize,offset,seek,count
source/path2:blocksize,offset,seek
source/path3:blocksize,offset

其中 source/pathsource/path2source/path3 是某些二进制文件的路径,而 offset、seek、count 和 blocksize 是 dd 命令的相应值。
请注意,变量可能会有所不同,例如某些二进制文件可能没有搜索或 dd 命令的搜索和计数值。

我应该如何拆分以上几行来编写这样的dd命令
dd if=${source/path} bs=${blocksize} seek=${seek} count=${count}
dd if=${source/path} bs=${blocksize} seek=${seek}
dd if=${source/path} bs=${blocksize}?

上面的格式如果需要修改一下方便解析也可以,因为我运行出于我幼稚的头脑能想到的所有可能性

希望对您有所帮助:

$ cat <<EOF | while read line; do arr=($(sed 's/[,:]/ /g' <<< $line)); echo "source:${arr[0]}  block:${arr[1]} offset:${arr[2]} seek:${arr[3]} count:${arr[4]}"; done
source/path:blocksize,offset,seek,count
source/path2:blocksize,offset,seek
source/path3:blocksize,offset
EOF
source:source/path  block:blocksize offset:offset seek:seek count:count
source:source/path2  block:blocksize offset:offset seek:seek count:
source:source/path3  block:blocksize offset:offset seek: count:

总体思路:

#!/usr/bin/env bash

your_command | while read line; do 
        arr=($(sed 's/[,:]/ /g' <<< $line)); 
        echo "source:${arr[0]}  block:${arr[1]} offset:${arr[2]} seek:${arr[3]} count:${arr[4]}"
        
        # Do whatever processing & validation you want here
        # access from array : ${arr[0]}....${arr[n]}
        #

 done

如果你有文件那么:

#!/usr/bin/env bash

while read line; do
       arr=($(sed 's/[,:]/ /g' <<< $line)); 
       echo "source:${arr[0]}  block:${arr[1]} offset:${arr[2]} seek:${arr[3]} count:${arr[4]}"
        
        # Do whatever processing & validation you want here
        # access from array : ${arr[0]}....${arr[n]}
        #

done < "path/to/your-file"