Page 1 of 1

Pass strings with spaces to bash script to use as text output and in filename

Posted: 2017-01-07T07:54:33-07:00
by rawd

Code: Select all

1	 #!/bin/bash
2	 # Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-05, running on macOSX 10.11.6 El Capitan
3	 # run the script in bash terminal like so:
4	 # ./rsvp.sh "12 dec" Christmas "The Golden Hall"
5	
6	 args=("$@")
7	
8	 echo
9	 echo my_date=${args[0]}
10	echo my_event=${args[1]}
11	echo my_venue=${args[2]}
12	
13	# date=my_date
14	# event=my_event
15	# venue=my_venue
16	
17	hardcoded_date="12_dec"
18	hardcoded_event="Christmas"
19	hardcoded_venue="The_Golden_Hall"
20	
21	echo
22	echo hardcoded_date=$hardcoded_date
23	echo hardcoded_event=$hardcoded_event
24	echo hardcoded_venue=$hardcoded_venue
25	echo
26	echo
27	
28	convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -10 -pointsize   33 -font /Library/Fonts/Roboto-Bold.ttf label:"Last\ndate\n$hardcoded_date" miff:- |
29	
30	# event-canvas0.png is just a rectangle of 1024x800 with white bg and 1px solid black border and a coloured and filled circle where the above date should go 
31	composite -gravity center -geometry -402-300 - event-canvas0.png miff:- |
32	
33	convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:$hardcoded_event' RSVP' -geometry +33-102 -composite miff:- |
34	
35	convert - -size 486x320 -background none -gravity west -stroke none -fill black -kerning 0.25 -interline-spacing -5 -font /Library/Fonts/Roboto-Regular.ttf caption:'Welcome to '$hardcoded_venue -geometry +32+87 -composite miff:- |
36	
37	convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' 'LN-xmas-golden-hall-12-dec-%[filename:dimensions].png'
38	# 'LN-$my_event-$my_venue-$my_date-%[filename:dimensions].png'
39	
40	open LN-xmas-golden-hall-12-dec-*.png
41	# open 'LN-$my_event-$my_venue-my_date-%[filename:dimensions].png'
42	
43	# remove output file when testing the result, remove this line when script is finished
44	sleep 4
45	rm LN-xmas-golden-hall-12-dec-*.png
Code without line numbers:

Code: Select all

#!/bin/bash
# Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-05, running on macOSX 10.11.6 El Capitan
# run the script in bash terminal like so:
# ./rsvp.sh "12 dec" Christmas "The Golden Hall"

args=("$@")

echo
echo my_date=${args[0]}
echo my_event=${args[1]}
echo my_venue=${args[2]}

# date=my_date
# event=my_event
# venue=my_venue

hardcoded_date="12_dec"
hardcoded_event="Christmas"
hardcoded_venue="The_Golden_Hall"

echo
echo hardcoded_date=$hardcoded_date
echo hardcoded_event=$hardcoded_event
echo hardcoded_venue=$hardcoded_venue
echo
echo

convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -10 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:'Last\ndate\n"${my_date}"' miff:- |

# event-canvas0.png is just a rectangle of 1024x800 with white bg and 1px solid black border and a coloured and filled circle where the above date should go
composite -gravity center -geometry -402-300 - event-canvas0.png miff:- |

convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:$hardcoded_event' RSVP' -geometry +33-102 -composite miff:- |

convert - -size 486x320 -background none -gravity west -stroke none -fill black -kerning 0.25 -interline-spacing -5 -font /Library/Fonts/Roboto-Regular.ttf caption:'Welcome to '$hardcoded_venue -geometry +32+87 -composite miff:- |

convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' -set filename:date "$my_date" 'LN-xmas-golden-hall-%[filename:date]-%[filename:dimensions].png'
# 'LN-$my_event-$my_venue-$my_date-%[filename:dimensions].png'

open LN-xmas-golden-hall-12-dec-*.png
# open 'LN-$my_event-$my_venue-my_date-%[filename:dimensions].png'

# remove output file when testing the result, remove this line when script is finished
sleep 4
rm LN-xmas-golden-hall-12-dec-*.png
I am trying to pass strings with spaces as arguments to a script that runs multiple 'convert' commands and it works well for simple echo commands but is not working with imagemagick convert commands. I want to use three strings when calling the script and get these strings as text outputs on the resulting image. I also want to use the input string args to name the resulting image file.

When replacing the hardcoded variables $hardcoded_date, $hardcoded_event and $hardcoded_venue currently inside the convert commands with $my_date, $my_event and $my_venue it doesn't work and the text output is empty. When taking the variable values of $my_date, $my_event and $my_venue and hardcoding those strings into the convert commands it works fine, ie the text output for the date displays "12 dec" without the double quotations and the entire text regarding the date looks like:

Last
date
12 dec

Since I am reusing the convert command to create text output I will be using the first convert command and the 'label' part specifically for this question, since that is the part I am currently editing to find a solution. The command in question is the first convert command on line 28:

convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -10 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"Last\ndate\n$hardcoded_date" miff:- |

I am trying to pass it a string with a space "12 dec" to replace the hardcoded_date of "12_dec" which has an underscore. I would like to replace this underscore with a space in the text output but keep it in the file name, alternatively replace it with a dash ("-"). The same would go for the other strings I am passing.

TRYING THE TEXT OUTPUT

I am calling the script in bash terminal with the command:
./rsvp.sh "12 dec" Christmas "The Golden Hall"

Below are my attempts to edit the label part of the convert command on line 28 and the results I have gotten so far.

label:"Last\ndate\n$hardcoded_date" results in the date displayed as 12_dec
label:"Last\ndate\n$my_date" results in the date text output being empty, no output

label:"Last\ndate\n"$hardcoded_date results in the date displayed as 12_dec
label:"Last\ndate\n"$my_date results in the date text output being empty, no output

label:'Last\ndate\n$hardcoded_date' results in the date text displayed as $hardcoded_date
label:'Last\ndate\n"$hardcoded_date"' (single quotes wrapping around double quotes) results in the date text displayed as $hardcoded_date

label:"Last\ndate\n'$hardcoded_date'" (double quotes wrapping around single quotes) results in the date text displayed as '12_dec'
label:"Last\ndate\n'$my_date'" (double quotes wrapping around single quotes) results in the date text displayed as '' (two single quotes)

label:"Last\ndate\n"$hardcoded_date"" (double quotes wrapping around double quotes) results in the date text displayed as 12_dec
label:"Last\ndate\n"$my_date"" (double quotes wrapping around double quotes) results in empty output

label:"Last\ndate\n\"$hardcoded_date\"" results in the date text displayed as "12_dec"
label:"Last\ndate\n\"$my_date\"" results in the date text displayed as ""

label:"Last\ndate\n${my_date}" results in empty output
label:"Last\ndate\n'${my_date}'" results in the date text displayed as '' (two single quotes)

label:"Last\ndate\n"${my_date} results in empty output
label:"Last\ndate\n"{$my_date} results the date text displayed as {}

label:"Last\ndate\n{$my_date}" results the date text displayed as {}
label:"Last\ndate\n'${my_date}'" (single quotes inside) results in the date text displayed as '' (two single quotes)
label:"Last\ndate\n"${my_date}"" (double quotes around double quotes) results in empty output
label:"Last\ndate\n\"${my_date}\"" results in the date text displayed as "" (two double quotes)

label:'Last\ndate\n\"${my_date}\"' (single quotes around escaped double quotes) results in the date text displayed as {my_date}
label:'Last\ndate\n"${my_date}"' (double quotes around double quotes) results in the date text displayed as {my_date}

Going for an alternative approach replacing the label with the -draw and -annotate options for convert
-draw "text 0,0 Last\ndate\n"$hardcoded_date"" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-draw "text 0,0 Last\ndate\n\"$hardcoded_date\"" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-draw "text 0,0 Last\ndate\n'$hardcoded_date'" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.

-annotate 0,0 "Last\ndate\n$hardcoded_date" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-annotate 0,0 'Last\ndate\n$my_date' gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-annotate 0,0 'Last\ndate\n"$my_date"' gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-annotate 0,0 "Last\ndate\n"${my_date}"" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.
-annotate 0,0 "Last\ndate\n\"${my_date}\"" gives err msg: convert: no images defined `miff:-' @ error/convert.c/ConvertImageCommand/3258.


TRYING THE FILE NAME OUTPUT
On line 37 the file name is written and I want to give use the variables my_date, my_event and my_venue in the output file name:

Code: Select all

37	convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' 'LN-xmas-golden-hall-12-dec-%[filename:dimensions].png'
38	# 'LN-$my_event-$my_venue-$my_date-%[filename:dimensions].png'
I have failed and managed to output the follwing file names:
LN-xmas-golden-hall-$hardcoded_date-1024x800.png
LN-xmas-golden-hall-${hardcoded_date}-1024x800.png
LN-xmas-golden-hall-{$hardcoded_date}-1024x800.png
LN-xmas-golden-hall-%$my_date-1024x800.png

...before trying this command:
convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' -set filename:date '$my_date' 'LN-xmas-golden-hall-%[filename:date]-%[filename:dimensions].png'

which resulted in: LN-xmas-golden-hall-$my_d1024x800.png

... and then this command

convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' -set filename:date "$my_date" 'LN-xmas-golden-hall-%[filename:date]-%[filename:dimensions].png'

which resulted in: LN-xmas-golden-hall--%[fi1024x800.png


And then I just gave up and asked you guys for help. I would be so grateful for your help after several weeks of making the entire script work.




Reference (for others who want to trace my steps and/or read up):
Pass strings to bash scripts http://stackoverflow.com/questions/1983 ... nt-in-bash
Passing strings with spaces to bash script for Imagemagick with the convert command https://imagemagick.org/discourse-serve ... hp?t=23861

Using the label option for the convert command to output text http://www.imagemagick.org/Usage/text/#pointsize
Using the draw option for the convert command to output text http://www.imagemagick.org/Usage/text/#draw
Using the annotate option for the convert command to output text http://www.imagemagick.org/Usage/text/#annotate

Manipulating file names viewtopic.php?t=17149

Other ref:
Piping commands multiple times http://studio.imagemagick.org/discourse ... =1&t=20906
Bash scripting for beginners http://www.tldp.org/LDP/Bash-Beginners- ... -Guide.pdf
Basic bash scripting https://linuxconfig.org/bash-scripting-tutorial

Re: Pass strings with spaces to bash script to use as text output and in filename

Posted: 2017-01-07T08:33:28-07:00
by snibgo
rawd wrote:When replacing the hardcoded variables $hardcoded_date, $hardcoded_event and $hardcoded_venue currently inside the convert commands with $my_date, $my_event and $my_venue it doesn't work and the text output is empty.
You are not setting the value of my_date etc. So it is empty, and using its value in "label:" or anywhere doesn't do much.

If we set the variable:

Code: Select all

my_date="12 Dec"

convert label:"last\ndate\n${my_date}" x.png
... results in x.png looking like this:

Code: Select all

last
date
12 Dec

Re: Pass strings with spaces to bash script to use as text output and in filename

Posted: 2017-01-07T15:48:54-07:00
by rawd
snibgo wrote:
rawd wrote:When replacing the hardcoded variables $hardcoded_date, $hardcoded_event and $hardcoded_venue currently inside the convert commands with $my_date, $my_event and $my_venue it doesn't work and the text output is empty.
You are not setting the value of my_date etc. So it is empty, and using its value in "label:" or anywhere doesn't do much.

If we set the variable:

Code: Select all

my_date="12 Dec"

convert label:"last\ndate\n${my_date}" x.png
... results in x.png looking like this:

Code: Select all

last
date
12 Dec

Thank you SO much snibgo for that crucial help! For some reason I had forgotten what I had done in my earlier and much simpler bash scripts where I tested how to pass strings without any imagemagick coding, and was now under the impression that the echoing was assigning the values as well. Lesson learned.

Below is the final code with changes made to the date input and with a working solution for naming the output file. I left some old code in comments for historial review.

Code: Select all

#!/bin/bash
# Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-05, running on macOSX 10.11.6 El Capitan
# run the script in bash terminal like so:
# ./rsvp.sh 12 12 "Christmas" "The Golden Hall"
# remember to chmod the file to be able to run it: chmod +x rsvp.sh

args=("$@")

echo
echo my_date1=${args[0]}
echo my_date2=${args[1]}
echo my_event=${args[2]}
echo my_venue=${args[3]}
echo

my_date1=${args[0]}
my_date2=${args[1]}
my_event=${args[2]}
my_venue=${args[3]}

convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -7 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"Last\ndate\n${my_date1}/${my_date2}" miff:- |

# below is an attempt to place the date input independently from the placeholder text above
# convert - -size 125x -background none -gravity center -stroke none -fill red -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"${my_date}" -composite miff:- |

# event-canvas0.png is just a rectangle of 1024x800 with white bg and 1px solid black border and a coloured and filled circle where the above date should go
composite -gravity center -geometry -402-300 - event-canvas0.png miff:- |

convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:"${my_event} RSVP" -geometry +33-102 -composite miff:- |

convert - -size 486x320 -background none -gravity west -stroke none -fill black -kerning 0.25 -interline-spacing -5 -font /Library/Fonts/Roboto-Regular.ttf caption:"Welcome to ${my_venue}" -geometry +32+87 -composite miff:- |

# keep below for historical reasons to remember that you can set a filename:date param and thus multiple set params can be used
# convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h' -set filename:date "${my_date}" "LN-${my_event}-${my_venue}-${my_date1}_${my_date2}-%[filename:dimensions].png"

convert - ~/Dropbox/+\ events/Royal\ Palace/Graphics/Logos/Xmas-event/xmas-logotype-rgb.jpg -gravity center -geometry 350x350+280+50 -composite -set filename:dimensions '%wx%h'  "LN-${my_event}-${my_venue}-${my_date1}_${my_date2}-%[filename:dimensions].png"


open LN-"${my_event}"-"${my_venue}"-"${my_date1}"_"${my_date2}"-*.png


# remove output file when testing the result, remove this line when script is finished
sleep 5
rm LN-"${my_event}"-"${my_venue}"-"${my_date1}"_"${my_date2}"-*.png