jac*_*cek 0 command-line bash options shell-script arguments
我想编写一个脚本来读取文件并将每一行作为选项(或“选项参数”)传递给命令,如下所示:
command -o "1st line" -o "2nd line" ... -o "last line" args
Run Code Online (Sandbox Code Playgroud)
这样做的最简单方法是什么?
# step 1, read the lines of the file into a shell array
mapfile -t lines < filename
# build up the command
cmd_ary=( command_name )
for elem in "${lines[@]}"; do
cmd_ary+=( -o "$elem" )
done
cmd_ary+=( other args here )
# invoke the command
"${cmd_ary[@]}"
Run Code Online (Sandbox Code Playgroud)