从数组提供的命令行参数
command line arguments fed from an array
我想执行一个复杂的 bash 命令,使用从长数组提供的数据作为参数。我想它必须以某种方式使用子外壳。
例如,而不是可行的
convert -size 100x100 xc:black -fill white -draw "point 1,1" -draw "point 4,8" -draw "point 87,34" etc etc etc image.png
我想采用不同的逻辑,在同一命令中给出参数,更像是
convert -size 100x100 xc:black -fill white $(for i in 1,1 4,8 87,34 etc etc; -draw "point $i"; done) image.png
这是行不通的,因为 $i 被解释为代替参数的命令。
请注意,"for i in ...; do convert ...$i...; done" 将不起作用。 -draw "point x,y"
系列参数必须在同一个 运行 convert 命令中,因为 convert 不接受现有图像中的 -draw 参数。
使用printf
扩展内容怎么样?
points=(1,1 4,8 87,34)
printf -- '-draw "point %s" ' ${points[@]}
returns 以下字符串(末尾没有换行):
-draw "point 1,1" -draw "point 4,8" -draw "point 87,34"
你可以说:
points=(1,1 4,8 87,34)
convert ... "$(printf -- '-draw "point %s" ' ${points[@]})" image.png
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这样,您将点存储在一个数组中,然后 printf
"delivers" 将其传递给 convert
命令。
尝试使用扩展而不是像这样的 subshell:
echo -draw\ \"point\ {1\,1,2\,2,3\,3}\"
产生这个输出:
-draw "point 1,1" -draw "point 2,2" -draw "point 3,3"
首先构建一个包含 -draw
个参数的数组。
for pt in 1,1 4,8 87,34; do
points+=(-draw "point $pt")
done
convert -size 100x100 xc:black -fill white "${points[@]}" image.png
您可以通过 stdin
将 MVG 绘图命令泵入 convert
并使用 @
文件说明符后跟代表 [=13 的破折号来保持命令行简洁明了=] 像这样:
for i in 1,1 4,8 87,34; do
echo point $i
done | convert -size 100x100 xc:red -draw @- result.png
或者,如果您有一个名为 points
的数组:
points=(1,1 4,8 87,34)
printf "point %s\n" ${points[@]} | convert -size 100x100 xc:red -draw @- result.png
我想执行一个复杂的 bash 命令,使用从长数组提供的数据作为参数。我想它必须以某种方式使用子外壳。
例如,而不是可行的
convert -size 100x100 xc:black -fill white -draw "point 1,1" -draw "point 4,8" -draw "point 87,34" etc etc etc image.png
我想采用不同的逻辑,在同一命令中给出参数,更像是
convert -size 100x100 xc:black -fill white $(for i in 1,1 4,8 87,34 etc etc; -draw "point $i"; done) image.png
这是行不通的,因为 $i 被解释为代替参数的命令。
请注意,"for i in ...; do convert ...$i...; done" 将不起作用。 -draw "point x,y"
系列参数必须在同一个 运行 convert 命令中,因为 convert 不接受现有图像中的 -draw 参数。
使用printf
扩展内容怎么样?
points=(1,1 4,8 87,34)
printf -- '-draw "point %s" ' ${points[@]}
returns 以下字符串(末尾没有换行):
-draw "point 1,1" -draw "point 4,8" -draw "point 87,34"
你可以说:
points=(1,1 4,8 87,34)
convert ... "$(printf -- '-draw "point %s" ' ${points[@]})" image.png
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这样,您将点存储在一个数组中,然后 printf
"delivers" 将其传递给 convert
命令。
尝试使用扩展而不是像这样的 subshell:
echo -draw\ \"point\ {1\,1,2\,2,3\,3}\"
产生这个输出:
-draw "point 1,1" -draw "point 2,2" -draw "point 3,3"
首先构建一个包含 -draw
个参数的数组。
for pt in 1,1 4,8 87,34; do
points+=(-draw "point $pt")
done
convert -size 100x100 xc:black -fill white "${points[@]}" image.png
您可以通过 stdin
将 MVG 绘图命令泵入 convert
并使用 @
文件说明符后跟代表 [=13 的破折号来保持命令行简洁明了=] 像这样:
for i in 1,1 4,8 87,34; do
echo point $i
done | convert -size 100x100 xc:red -draw @- result.png
或者,如果您有一个名为 points
的数组:
points=(1,1 4,8 87,34)
printf "point %s\n" ${points[@]} | convert -size 100x100 xc:red -draw @- result.png