首页 文章

mkdir在运行imagemagick的bash脚本时卡住了

提问于
浏览
1

我正在尝试在运行imagemagick bash脚本时写入驱动器上的特定目录/文件夹 .

我到目前为止,我可以为我的脚本添加参数,以便使用参数值创建一个新目录(文件夹)到我想输出imagemagick文件的位置 . 我设法使用我的bash脚本中mkdir命令的传入参数创建一个新目录 . 但是,脚本在执行mkdir命令结束时卡住,并且永远不会继续 .

这导致我的目录被创建,但我从未有机会写入我的新目录中的文件 . 我以后可以找到创建的目录,但其中没有任何内容 . 我已经尝试了两种不同的方法来创建我的目录并且都被卡住了 .

当我在终端中调用脚本时,它执行所有步骤,直到它到达mkdir命令然后我的终端冻结,就像它正在等待某些事情发生所以它可以继续 - 多次点击 ENTER 没有感觉到't help either ;) - and I don' t它被困在一个循环中 .

我正在运行一个mkdir命令作为对我在尝试输出到输出目录时收到的错误消息的响应,而没有在写入之前创建输出目录 .

我在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"

编辑:

在第一次评论和回复后,我觉得有必要给你们完整的代码,也许有些东西供你找 . =)另外,正如我在下面的评论中提到的,在ShellCheck的指示之后清除shell脚本问题(正如@Cyrus在评论中所建议的那样)搞砸了imagemagick命令并且输出失败了 . 因此,我已经恢复到旧代码,以便从那里一步一步地纠正代码,同时保持所需的输出旧的(并且根据ShellCheck的bug)生成的代码 . 见下面的代码 .

我在使用ShellCheck进行调试时所做的更改并未解决挂起mkdir命令的主要问题 . 我在 $folder_path 中创建一个新目录时仍然失败,因为它永远不会继续使用最后一个 convert 命令实际输出图像 .

我在使用ShellCheck进行调试后遇到的其他问题是 $custpath 在我尝试在 convert 命令中使用时根本不起作用,这就是为什么我现在在转换命令中使用 $custpath 的整个值 . 我试图使用 $"{custpath[@]}"$"{custpath[*]}" ,因为ShellCheck建议并且没有一个成功创建输出,因为 - 猜猜是什么 - 使用 $custpath 挂起脚本 .

我想要做的是将输出图像放在 $custpath$folder_path$output_dir 文件夹中 .

这是整个脚本:

#!/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 回答

  • 1

    你的命令:

    convert - ...
    

    尝试从标准输入中读取图像 . 因此,除非您在标准输入上提供图像,否则它将永远挂起:

    cat someImage.jpg | yourScript arg1 arg2
    

    或者之后命名图像:

    convert someImage.jpg ...
    

    也许 $logo_path 已经是您图像的名称,在这种情况下您需要:

    convert "$logo_path" ...
    

    顺便说一句,@ Jdamian的建议很好,具体而言,它意味着将你的第一行更改为:

    #!/bin/bash -x
    
  • 1

    我通过将它移动到 ~/.bash_profile 并使用mkdir的自定义命令来解决挂起_2649674的问题,如下所示:

    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]}"
    }
    

    无论如何我必须将问题推到一个级别,因为无论如何我的意图是使用bash配置文件以便同时创建许多图像 . =)

    该脚本现在看起来像:

    #!/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
    

相关问题