Why does my shell script not execute sequentially? (might be imagemagick?)
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I made a simple bash shell script to perform three imagemagick commands on every file in a directory. I did not use & nor | to make each command run concurrently.
#!/bin/bash
jpg="$1/*.jpg"
jpeg="$1/*.jpeg"
JPG="$1/*.JPG"
png="$1/*.png"
#convert to png
to_png()
for file in $jpg; do mogrify -format png $file; rm $file; done
for file in $jpeg; do mogrify -format png $file; rm $file; done
for file in $JPG; do mogrify -format png $file; rm $file; done
#format for 4k
to_4k()
for file in $png; do convert $file -resize 3840x2160 $file; done
#put on transparent background
to_trans()
for file in $png; do composite -gravity center $file -geometry 3840x2160 /path/to/transparent/background $file; done
do_stuff()
to_png
to_4k
to_trans
if [ -d "$1" ];
then do_stuff
else echo "You didn't enter a directory. Please try again."
fi
When there are any .jpg files in the directory I get error messages. Is ImageMagick telling bash that the command is completed before the file is finished?
convert: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
convert: Read Exception `/path/to/picture/image.png' @ error/png.c/MagickPNGErrorHandler/1643.
convert: corrupt image `/path/to/picture/image.png' @ error/png.c/ReadPNGImage/3973.
convert: no images defined `/path/to/picture/image.png' @ error/convert.c/ConvertImageCommand/3210.
composite: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
Using sleep for a long duration between commands resolves this issue, but it's pretty sloppy.
side note: I stored directories in variables because using $1/*.jpg within a for loop fails to expand $1 and * apparently. Bash returns an error saying that /path/to/*.jpg doesn't exist.
I'm using Ubuntu 16.04 (x86_64), GNU bash 4.3.48, and ImageMagick 6.8.9-9
bash shell-script ubuntu imagemagick
add a comment |Â
up vote
1
down vote
favorite
I made a simple bash shell script to perform three imagemagick commands on every file in a directory. I did not use & nor | to make each command run concurrently.
#!/bin/bash
jpg="$1/*.jpg"
jpeg="$1/*.jpeg"
JPG="$1/*.JPG"
png="$1/*.png"
#convert to png
to_png()
for file in $jpg; do mogrify -format png $file; rm $file; done
for file in $jpeg; do mogrify -format png $file; rm $file; done
for file in $JPG; do mogrify -format png $file; rm $file; done
#format for 4k
to_4k()
for file in $png; do convert $file -resize 3840x2160 $file; done
#put on transparent background
to_trans()
for file in $png; do composite -gravity center $file -geometry 3840x2160 /path/to/transparent/background $file; done
do_stuff()
to_png
to_4k
to_trans
if [ -d "$1" ];
then do_stuff
else echo "You didn't enter a directory. Please try again."
fi
When there are any .jpg files in the directory I get error messages. Is ImageMagick telling bash that the command is completed before the file is finished?
convert: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
convert: Read Exception `/path/to/picture/image.png' @ error/png.c/MagickPNGErrorHandler/1643.
convert: corrupt image `/path/to/picture/image.png' @ error/png.c/ReadPNGImage/3973.
convert: no images defined `/path/to/picture/image.png' @ error/convert.c/ConvertImageCommand/3210.
composite: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
Using sleep for a long duration between commands resolves this issue, but it's pretty sloppy.
side note: I stored directories in variables because using $1/*.jpg within a for loop fails to expand $1 and * apparently. Bash returns an error saying that /path/to/*.jpg doesn't exist.
I'm using Ubuntu 16.04 (x86_64), GNU bash 4.3.48, and ImageMagick 6.8.9-9
bash shell-script ubuntu imagemagick
1
you need to do... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variables
â Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
I'm curious too, runbash +x
to see step by step execution, I'm running my own lab here
â BrenoZan
Dec 14 '17 at 17:21
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I made a simple bash shell script to perform three imagemagick commands on every file in a directory. I did not use & nor | to make each command run concurrently.
#!/bin/bash
jpg="$1/*.jpg"
jpeg="$1/*.jpeg"
JPG="$1/*.JPG"
png="$1/*.png"
#convert to png
to_png()
for file in $jpg; do mogrify -format png $file; rm $file; done
for file in $jpeg; do mogrify -format png $file; rm $file; done
for file in $JPG; do mogrify -format png $file; rm $file; done
#format for 4k
to_4k()
for file in $png; do convert $file -resize 3840x2160 $file; done
#put on transparent background
to_trans()
for file in $png; do composite -gravity center $file -geometry 3840x2160 /path/to/transparent/background $file; done
do_stuff()
to_png
to_4k
to_trans
if [ -d "$1" ];
then do_stuff
else echo "You didn't enter a directory. Please try again."
fi
When there are any .jpg files in the directory I get error messages. Is ImageMagick telling bash that the command is completed before the file is finished?
convert: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
convert: Read Exception `/path/to/picture/image.png' @ error/png.c/MagickPNGErrorHandler/1643.
convert: corrupt image `/path/to/picture/image.png' @ error/png.c/ReadPNGImage/3973.
convert: no images defined `/path/to/picture/image.png' @ error/convert.c/ConvertImageCommand/3210.
composite: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
Using sleep for a long duration between commands resolves this issue, but it's pretty sloppy.
side note: I stored directories in variables because using $1/*.jpg within a for loop fails to expand $1 and * apparently. Bash returns an error saying that /path/to/*.jpg doesn't exist.
I'm using Ubuntu 16.04 (x86_64), GNU bash 4.3.48, and ImageMagick 6.8.9-9
bash shell-script ubuntu imagemagick
I made a simple bash shell script to perform three imagemagick commands on every file in a directory. I did not use & nor | to make each command run concurrently.
#!/bin/bash
jpg="$1/*.jpg"
jpeg="$1/*.jpeg"
JPG="$1/*.JPG"
png="$1/*.png"
#convert to png
to_png()
for file in $jpg; do mogrify -format png $file; rm $file; done
for file in $jpeg; do mogrify -format png $file; rm $file; done
for file in $JPG; do mogrify -format png $file; rm $file; done
#format for 4k
to_4k()
for file in $png; do convert $file -resize 3840x2160 $file; done
#put on transparent background
to_trans()
for file in $png; do composite -gravity center $file -geometry 3840x2160 /path/to/transparent/background $file; done
do_stuff()
to_png
to_4k
to_trans
if [ -d "$1" ];
then do_stuff
else echo "You didn't enter a directory. Please try again."
fi
When there are any .jpg files in the directory I get error messages. Is ImageMagick telling bash that the command is completed before the file is finished?
convert: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
convert: Read Exception `/path/to/picture/image.png' @ error/png.c/MagickPNGErrorHandler/1643.
convert: corrupt image `/path/to/picture/image.png' @ error/png.c/ReadPNGImage/3973.
convert: no images defined `/path/to/picture/image.png' @ error/convert.c/ConvertImageCommand/3210.
composite: Expected 8 bytes; found 0 bytes `/path/to/picture/image.png' @ warning/png.c/MagickPNGWarningHandler/1669.
Using sleep for a long duration between commands resolves this issue, but it's pretty sloppy.
side note: I stored directories in variables because using $1/*.jpg within a for loop fails to expand $1 and * apparently. Bash returns an error saying that /path/to/*.jpg doesn't exist.
I'm using Ubuntu 16.04 (x86_64), GNU bash 4.3.48, and ImageMagick 6.8.9-9
bash shell-script ubuntu imagemagick
edited Dec 14 '17 at 21:42
asked Dec 14 '17 at 6:05
soundssilver
83
83
1
you need to do... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variables
â Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
I'm curious too, runbash +x
to see step by step execution, I'm running my own lab here
â BrenoZan
Dec 14 '17 at 17:21
add a comment |Â
1
you need to do... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variables
â Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
I'm curious too, runbash +x
to see step by step execution, I'm running my own lab here
â BrenoZan
Dec 14 '17 at 17:21
1
1
you need to do
... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variablesâ Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
you need to do
... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variablesâ Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
I'm curious too, run
bash +x
to see step by step execution, I'm running my own lab hereâ BrenoZan
Dec 14 '17 at 17:21
I'm curious too, run
bash +x
to see step by step execution, I'm running my own lab hereâ BrenoZan
Dec 14 '17 at 17:21
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
$1
inside function is not the same as $1
outside function.
so you need to do save it at script begin: dir="$1"
, ...and use $dir
everywhere else.
this way you will solve first strange thing you noticed yourself (bash: path doesn't exist)... but it will probably solve everything else.
Your way of resolving was not complete you must put vars in quotes, but then globe expanding will be wrong ... and the only thing you can do is to clean up your code because simplified version of your script works well for sure:
#!/bin/bash
shopt -s nullglob ; set -o xtrace #xtrace for debug
dir="$1" ; [ -d "$dir" ] || dir=.
for file in "$dir"/*.jpg,jpeg,JPG; do mogrify -format png "$file"; rm "$file"; done
for file in "$dir"/*.png; do convert "$file" -resize 3840x2160 "$file"; done
for file in "$dir"/*.png; do composite -gravity center "$file" -geometry 3840x2160 /home/d/bin/youtube_tools/4kclear.png "$file"; done
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
$1
inside function is not the same as $1
outside function.
so you need to do save it at script begin: dir="$1"
, ...and use $dir
everywhere else.
this way you will solve first strange thing you noticed yourself (bash: path doesn't exist)... but it will probably solve everything else.
Your way of resolving was not complete you must put vars in quotes, but then globe expanding will be wrong ... and the only thing you can do is to clean up your code because simplified version of your script works well for sure:
#!/bin/bash
shopt -s nullglob ; set -o xtrace #xtrace for debug
dir="$1" ; [ -d "$dir" ] || dir=.
for file in "$dir"/*.jpg,jpeg,JPG; do mogrify -format png "$file"; rm "$file"; done
for file in "$dir"/*.png; do convert "$file" -resize 3840x2160 "$file"; done
for file in "$dir"/*.png; do composite -gravity center "$file" -geometry 3840x2160 /home/d/bin/youtube_tools/4kclear.png "$file"; done
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
add a comment |Â
up vote
2
down vote
accepted
$1
inside function is not the same as $1
outside function.
so you need to do save it at script begin: dir="$1"
, ...and use $dir
everywhere else.
this way you will solve first strange thing you noticed yourself (bash: path doesn't exist)... but it will probably solve everything else.
Your way of resolving was not complete you must put vars in quotes, but then globe expanding will be wrong ... and the only thing you can do is to clean up your code because simplified version of your script works well for sure:
#!/bin/bash
shopt -s nullglob ; set -o xtrace #xtrace for debug
dir="$1" ; [ -d "$dir" ] || dir=.
for file in "$dir"/*.jpg,jpeg,JPG; do mogrify -format png "$file"; rm "$file"; done
for file in "$dir"/*.png; do convert "$file" -resize 3840x2160 "$file"; done
for file in "$dir"/*.png; do composite -gravity center "$file" -geometry 3840x2160 /home/d/bin/youtube_tools/4kclear.png "$file"; done
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
$1
inside function is not the same as $1
outside function.
so you need to do save it at script begin: dir="$1"
, ...and use $dir
everywhere else.
this way you will solve first strange thing you noticed yourself (bash: path doesn't exist)... but it will probably solve everything else.
Your way of resolving was not complete you must put vars in quotes, but then globe expanding will be wrong ... and the only thing you can do is to clean up your code because simplified version of your script works well for sure:
#!/bin/bash
shopt -s nullglob ; set -o xtrace #xtrace for debug
dir="$1" ; [ -d "$dir" ] || dir=.
for file in "$dir"/*.jpg,jpeg,JPG; do mogrify -format png "$file"; rm "$file"; done
for file in "$dir"/*.png; do convert "$file" -resize 3840x2160 "$file"; done
for file in "$dir"/*.png; do composite -gravity center "$file" -geometry 3840x2160 /home/d/bin/youtube_tools/4kclear.png "$file"; done
$1
inside function is not the same as $1
outside function.
so you need to do save it at script begin: dir="$1"
, ...and use $dir
everywhere else.
this way you will solve first strange thing you noticed yourself (bash: path doesn't exist)... but it will probably solve everything else.
Your way of resolving was not complete you must put vars in quotes, but then globe expanding will be wrong ... and the only thing you can do is to clean up your code because simplified version of your script works well for sure:
#!/bin/bash
shopt -s nullglob ; set -o xtrace #xtrace for debug
dir="$1" ; [ -d "$dir" ] || dir=.
for file in "$dir"/*.jpg,jpeg,JPG; do mogrify -format png "$file"; rm "$file"; done
for file in "$dir"/*.png; do convert "$file" -resize 3840x2160 "$file"; done
for file in "$dir"/*.png; do composite -gravity center "$file" -geometry 3840x2160 /home/d/bin/youtube_tools/4kclear.png "$file"; done
edited Dec 14 '17 at 17:09
answered Dec 14 '17 at 16:13
Asain Kujovic
939815
939815
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
add a comment |Â
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
Turns out that it does work with $1 inside the for loop. Your other points really helped out. Thanks.
â soundssilver
Dec 14 '17 at 23:32
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410800%2fwhy-does-my-shell-script-not-execute-sequentially-might-be-imagemagick%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
you need to do
... mogrify -format "$1/$file%.*.png" "$file"; ...
, next quote all your variablesâ Ã±ÃÂsýù÷
Dec 14 '17 at 6:50
I'm curious too, run
bash +x
to see step by step execution, I'm running my own lab hereâ BrenoZan
Dec 14 '17 at 17:21