How can i store commands as a variables and execute them randomly in bash?
command1="
convert -size 2000x1000 xc:none -gravity center \
-stroke yellow -pointsize 50 -font Courier-BoldOblique -strokewidth 3 -annotate +100+100 "${caption}" \
-blur 0x25 -level 0%,50% \
-fill white -stroke none -annotate +100+100 "${caption}" \
in.jpeg +swap -gravity center -geometry +0-3 \
-composite out.jpeg
"
command2="
convert -size 2000x1000 xc:none -gravity center \
-fill white -pointsize 50 -stroke none -annotate +100+100 "${caption}" -channel alpha -evaluate multiply 0.35 -trim +repage \
in.jpeg +swap -gravity center -geometry +0-3 \
-composite out.jpeg
"
What have i tried
COMMANDS=("command1" "command2")
$(eval $(shuf -n1 -e "${COMMANDS[@]}"))
Desired output is to randomly run any of the two convert commands. How can i get the desired result and where are the things going wrong ??
I have taken hints from -
how-to-store-a-command-in-a-variable-in-a-shell-script
Thanks in advance for help !!
"$(shuf -n1 -e command1 command2)"
. – Feb 21 '20 at 12:18C=("$command1" "$command2"); eval "$(shuf -n1 -e "${C[@]}")"
would probably work in your case. Notice the extra$
s and the extra"
s. – Feb 21 '20 at 12:29