簡體   English   中英

為imagemagick運行bash腳本時,mkdir卡住了

[英]mkdir stuck when running bash script for imagemagick

運行imagemagick bash腳本時,我試圖寫入驅動器上的特定目錄/文件夾。

到目前為止,我可以為腳本添加參數,以便使用參數值在要輸出imagemagick文件的位置創建一個新目錄(文件夾)。 我設法在bash腳本中使用傳遞給mkdir命令的參數創建一個新目錄。 但是,腳本會在執行mkdir命令的最后卡住,並且永遠不會繼續執行。

這將導致創建目錄,但是我再也沒有機會在新目錄中寫入文件。 以后我可以找到創建的目錄,但其中沒有任何內容。 我嘗試了兩種不同的方式來創建目錄,但都被卡住了。

當我在終端中調用腳本時,它會執行所有步驟,直到到達mkdir命令,然后終端凍結,就像它正在等待發生的事情一樣,因此它可以繼續前進-多次按ENTER也無濟於事; )-而且我沒有感覺它陷入了循環。

我正在運行一個mkdir命令,作為對我嘗試輸出到輸出目錄時未在寫入之前創建輸出目錄時收到的err msgs的響應。

我在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的指示(根據評論中的@Cyrus的建議)清除shell腳本問題使imagemagick命令混亂,並且輸出失敗。 因此,我已恢復到舊代碼,以便從此處逐步糾正代碼,同時保持所需的輸出(生成的舊代碼(根據ShellCheck,它是越野車))。 請參見下面的代碼。

我在使用ShellCheck進行調試時所做的更改無法解決mkdir掛起的主要問題。 $folder_path創建新目錄時,我仍然失敗,因為它永遠不會繼續使用最后一個convert命令實際輸出圖像。

在用ShellCheck調試之后,我遇到的其他問題是,當我嘗試在convert命令中使用$custpath它根本無法工作,這就是為什么我現在在convert命令中使用$custpath的整個值的$custpath 我已嘗試按照ShellCheck的建議使用$"{custpath[@]}"$"{custpath[*]}" ,但它們均未成功創建輸出,因為-猜怎么-使用$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

您的命令:

convert - ...

嘗試從其標准輸入讀取圖像。 因此,除非您在標准輸入上提供圖像,否則它將永遠掛起:

cat someImage.jpg | yourScript arg1 arg2

或之后命名圖片:

convert someImage.jpg ...

也許$logo_path已經是圖像的名稱,在這種情況下,您將需要:

convert "$logo_path" ...

順便說一句,@ Jdamian的建議是一個好建議,具體來說,這意味着將您的第一行更改為:

#!/bin/bash -x

我通過將其掛到~/.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]}"
}

不必將問題向上推一級,因為我的意圖是無論如何都要使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM