Pass strings with spaces to bash script to use as text output and in filename
Posted: 2017-01-07T07:54:33-07:00
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: 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
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'
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