简体   繁体   English

为imagemagick运行bash脚本时,mkdir卡住了

[英]mkdir stuck when running bash script for imagemagick

I am trying to write to a specific directory/folder on my drive when running an imagemagick bash script. 运行imagemagick bash脚本时,我试图写入驱动器上的特定目录/文件夹。

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. 到目前为止,我可以为脚本添加参数,以便使用参数值在要输出imagemagick文件的位置创建一个新目录(文件夹)。 I manage to create a new directory using the passed in arguments to the mkdir command in my bash script. 我设法在bash脚本中使用传递给mkdir命令的参数创建一个新目录。 However, the script gets stuck at the end of the execution of the mkdir command and never moves on. 但是,脚本会在执行mkdir命令的最后卡住,并且永远不会继续执行。

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. 当我在终端中调用脚本时,它会执行所有步骤,直到到达mkdir命令,然后终端冻结,就像它正在等待发生的事情一样,因此它可以继续前进-多次按ENTER也无济于事; )-而且我没有感觉它陷入了循环。

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. 我正在运行一个mkdir命令,作为对我尝试输出到输出目录时未在写入之前创建输出目录时收到的err msgs的响应。

I'm running bash on OSX El Capitan and below are selected fragments of the script relevant to my problem. 我在OSX El Capitan上运行bash,以下是与我的问题相关的脚本的选定片段。

#!/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. =)另外,正如我在下面的评论中提到的那样,按照ShellCheck的指示(根据评论中的@Cyrus的建议)清除shell脚本问题使imagemagick命令混乱,并且输出失败。 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. 因此,我已恢复到旧代码,以便从此处逐步纠正代码,同时保持所需的输出(生成的旧代码(根据ShellCheck,它是越野车))。 See code below. 请参见下面的代码。

The changes I made while debugging with ShellCheck did not fix the main problem with the hanging mkdir command. 我在使用ShellCheck进行调试时所做的更改无法解决mkdir挂起的主要问题。 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. $folder_path创建新目录时,我仍然失败,因为它永远不会继续使用最后一个convert命令实际输出图像。

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. 在用ShellCheck调试之后,我遇到的其他问题是,当我尝试在convert命令中使用$custpath它根本无法工作,这就是为什么我现在在convert命令中使用$custpath的整个值的$custpath 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. 我已尝试按照ShellCheck的建议使用$"{custpath[@]}"$"{custpath[*]}" ,但它们均未成功创建输出,因为-猜怎么-使用$custpath挂起脚本。

What I am trying to do is to put the output image inside $custpath$folder_path$output_dir folder. 我正在尝试将输出图像放入$custpath$folder_path$output_dir文件夹中。

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

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: 也许$logo_path已经是图像的名称,在这种情况下,您将需要:

convert "$logo_path" ...

By the way, @Jdamian's suggestion is a good one, in concrete terms, it means change your first line to: 顺便说一句,@ Jdamian的建议是一个好建议,具体来说,这意味着将您的第一行更改为:

#!/bin/bash -x

I solved the problem with the hanging mkdir by moving it to ~/.bash_profile and using a customized command for mkdir like so: 我通过将其挂到~/.bash_profile并使用自定义的mkdir命令来解决了挂起的mkdir的问题,如下所示:

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. 不必将问题向上推一级,因为我的意图是无论如何都要使用bash配置文件,以便同时创建许多图像。 =) =)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM