mkdir stuck when running bash script for imagemagick

796 views Asked by At

I am trying to write to a specific directory/folder on my drive when running an imagemagick bash script.

I have come so far that I can giev arguments to my script in order to use the argument values to create a new directory (folder) into where I want to output an imagemagick file. I manage to create a new directory using the passed in arguments to the mkdir command in my bash script. However, the script gets stuck at the end of the execution of the mkdir command and never moves on.

This results in my dir being created, but I never get the chance to write to a file inside my new dir. I can later find the created dir but there is nothing in it. I have tried two different ways to create my dir and both get stuck.

When I call the script in my terminal it does all the steps until it gets to the mkdir command and then my terminal freezes, like it is waiting for something to happen so it can move on - hitting ENTER multiple times doesn't help either ;) - and I don't get the feeling it is stuck in a loop.

I am running a mkdir command as a response to err msgs I received when trying to output to the output dir without having the output dir created before writing to it.

I'm running bash on OSX El Capitan and below are selected fragments of the script relevant to my problem.

#!/bin/bash

args=("$@")

employer=${args[2]}
position=${args[3]}

output_dir="${employer} - ${position}"

# mkdir -pv -m u=rwx "$output_dir"
[ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"

# imagemagick script where a new image file gets written to my new output_dir
# the final string is the output path of my new imagemagick image output 
convert - $logo_path -gravity center -geometry 350x350+280+70 -composite -set 
filename:dimensions '%wx%h' 
"${output_dir}/LN-${employer}-${position}-%[filename:dimensions].png"

EDIT:

After the first comments and response I feel obliged to give you guys the full code, perhaps there is something for you there to find. =) Also, as I mention in my comment below, clearing up the shell scripting issues following ShellCheck's directions (as advised by @Cyrus in the comments) messed up the imagemagick commands and the output failed. Therefore I have reverted back to old code in order to correct the code step by step from there, while maintaining the desired output the old (and buggy, according to ShellCheck) code produced. See code below.

The changes I made while debugging with ShellCheck did not fix the main problem with the hanging mkdir command. I am still failing when creating a new dir inside my $folder_path, cause it never moves on to actually outputting the image with the last convert command.

Additional problems I ran into after debugging with ShellCheck was that $custpath doesn't work at all when I try to use it inside the convert commands, which is why I use the entire value of $custpath in the convert commands for now. I have tried to use $"{custpath[@]}" and $"{custpath[*]}" as ShellCheck suggests and none of them succeed in creating the output, because - guess what - using $custpath hangs the script.

What I am trying to do is to put the output image inside $custpath$folder_path$output_dir folder.

Here is the entire script:

#!/bin/bash -x
# 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:
# ./my_script.sh date1 date2 "employer" "position" "path to logo img"
# remember to chmod the file to be able to run it: chmod +x my_script.sh
# this script is partially debugged with http://www.shellcheck.net/

args=("$@")

echo
echo date1 = "${args[0]}"
echo date2 = "${args[1]}"
echo employer = "${args[2]}"
echo position = "${args[3]}"
echo logo_path = "${args[4]}"

custpath=($HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/)
echo custpath = "${custpath[@]}"

date1=${args[0]}
date2=${args[1]}
employer=${args[2]}
position=${args[3]}
logo_path=${args[4]}

# find the folder in the logo path
# see this article for how to replace a pattern from the end
# http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
folder_path=${logo_path/%\/*//}
echo folder_path = $folder_path
output_dir="${employer} - ${position}"
echo output_dir = $output_dir
echo

convert \( -draw 'circle 108.5,101.5 159.5,160' -stroke "rgb(241,142,0)" -fill "rgb(241,142,0)"  -size 1022x798 canvas:white -bordercolor black -border 1x1 -fill black -stroke black -draw 'rectangle 1,721 1022,798' -fill white -stroke none -background none -gravity south -page +93-11.5 -font /Library/Fonts/RobotoCondensed-Light.ttf -pointsize 35.5 label:'A Logo' -flatten \) $HOME/Dropbox/+dev/coding_images/imagemagick/a-logo-neg.png -geometry 207x+386+6.5 -composite miff:canvas0

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${date1}/${date2}" miff:- |

composite -gravity center -geometry -402-300 - canvas0 miff:- |

convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:"${employer}" -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:"${position}" -geometry +32+87 -composite miff:- |

# convert - $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/$logo_path -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h'  "$HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/${folder_path}LN-${employer}-${position}-${date1}_${date2}-%[filename:dimensions].png"

# mkdir -pv -m u=rwx "$output_dir"
# [ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"
# cd "$output_dir"

# below is the final version of the output path I want to have, if only the $custpath variable worked
# convert - $logo_path -gravity center -geometry 350x350+280+70 -composite -set
# filename:dimensions '%wx%h'
# "${custpath}/${folder_path}/${output_dir}/LN-${employer}-${position}-%[filename:dimensions].png"

convert - $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h'  $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png

convert - $"{custpath[@]}"/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h'  $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png

rm canvas0

# open $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-%[filename:dimensions].png

open $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png

# remove output file when testing the result, remove this line when script is finished
# sleep 5
# rm LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png
2

There are 2 answers

2
Mark Setchell On

Your command:

convert - ...

tries to read an image from its standard input. So, it will hang forever unless you provide an image on standard input:

cat someImage.jpg | yourScript arg1 arg2

or name an image afterwards:

convert someImage.jpg ...

Maybe $logo_path already is the name of your image, in which case you would need:

convert "$logo_path" ...

By the way, @Jdamian's suggestion is a good one, in concrete terms, it means change your first line to:

#!/bin/bash -x
0
Discofan On

I solved the problem with the hanging mkdir by moving it to ~/.bash_profile and using a customized command for mkdir like so:

mk () {
  case "$1" in /*) :;; *) set -- "./$1";; esac
  mkdir -p "$1" #&& cd "$1"
}

# call the socmed function like so
# socmed day month "employer" "position" "path to logo"
function socmed {
  args=("$@")

  echo
  echo date1 = "${args[0]}"
  echo date2 = "${args[1]}"
  echo employer = "${args[2]}"
  echo position = "${args[3]}"
  echo logo_path = "${args[4]}"


  employer=${args[2]}
  position=${args[3]}
  logo_path=${args[4]}
  folder_path=${logo_path/%\/*//}
  custpath="$HOME/Dropbox/+ B-folder/A Folder/Gfx/Logos/"
  output_dir="${employer} - ${position}"
  mk "$custpath"/"$folder_path"/"$output_dir"

  echo custpath = "$custpath"
  echo folder_path = "$folder_path"
  echo output_dir = "$output_dir"
  echo

  ./full-tw.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
  ./full-insta.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
  ./full-ln.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
  ./full-fb.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
}

It doesn't matter that I have to shove the problem upwards one level, cause my intention was to use the bash profile anyway in order to have many images be created at the same time. =)

The script now looks like:

    #!/bin/bash +x
# 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:
# ./full-ln.sh 12 12 "Employer" "Position" "path to logo"
# remember to chmod the file to be able to run it: chmod +x full-ln.sh
# this script is partially  debugged with http://www.shellcheck.net/
#
# this script was improved with the help of the nice ppl at StackOverflow
# http://stackoverflow.com/questions/41531443/mkdir-stuck-when-running-bash-script-for-imagemagick?noredirect=1#comment70275303_41531443

args=("$@")

# echo
# echo date1 = "${args[0]}"
# echo date2 = "${args[1]}"
# echo employer = "${args[2]}"
# echo position = "${args[3]}"
# echo logo_path = "${args[4]}"

# the new way for $custpath
custpath="$HOME/Dropbox/+ B-folder/A Folder/Gfx/Logos/"
# echo custpath = "$custpath"

# the old way below
# custpath=$HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/
# echo custpath = "${custpath[@]}"

date1=${args[0]}
date2=${args[1]}
employer=${args[2]}
position=${args[3]}
logo_path=${args[4]}

# find the folder in the logo path
# see this article for how to replace a pattern from the end
# http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
folder_path=${logo_path/%\/*//}
# echo folder_path = "$folder_path"
output_dir="${employer} - ${position}"
# echo output_dir = "$output_dir"
# echo

convert \( -draw 'circle 108.5,101.5 159.5,160' -stroke "rgb(241,142,0)" -fill "rgb(241,142,0)"  -size 1022x798 canvas:white -bordercolor black -border 1x1 -fill black -stroke black -draw 'rectangle 1,721 1022,798' -fill white -stroke none -background none -gravity south -page +93-11.5 -font /Library/Fonts/RobotoCondensed-Light.ttf -pointsize 35.5 label:'A Folder Karriär' -flatten \) $HOME/Dropbox/+dev/coding_images/imagemagick/ah-karriar-neg.png -geometry 207x+386+6.5 -composite miff:canvas0

convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -7 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"Sista\nansökan\n${date1}/${date2}" miff:- |

composite -gravity center -geometry -402-300 - canvas0 miff:- |

convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:"${employer} söker" -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:"${position}" -geometry +32+87 -composite miff:- |

# failed at solving the making of a dir inside this script, moved that to bash_profile
# mkdir -pv -m u=rwx "$custpath"/"$folder_path"/"$output_dir"
# [ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"

convert - "$custpath"/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h'  "$custpath/$folder_path"/"$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png

rm canvas0

echo "LinkedIn-image complete"
echo

# open "$custpath"/"$folder_path/$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png

# remove output file when testing the result, remove this line when script is finished
# sleep 5
# rm "$custpath"/"$folder_path/$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png