Reputation: 19
I have the script below which I run with from within a directory of images with ./checkres.sh * or even *.jpg, but it only works if there is a small number of files, less than 100 or so. Anything more and it stops with error "Argument list too long" and I have 100,000's of images to process.
I been looking all over the web for hours and tried dozens of different changes, using a while instead of a for loop and various other changes to variables but nothing seems to work.
Can anyone help solve this mystery as I'm new to bash scripting and have run out of ideas.
#!/bin/bash
for img in "$@"
do
export height=`identify -format %h "$img"`
export width=`identify -format %w "$img"`
let ratio=$width/$height
echo Image "$img" = [ $width x $height ] = $ratio
if [ $ratio == 0 ]
then
echo Portrait 0
convert "$img" -geometry 600x800 -format jpeg -quality 80 "$img"
else
echo Landscape 1
convert "$img" -geometry 800x600 -format jpeg -quality 80 "$img"
fi
Upvotes: 1
Views: 1183
Reputation: 295825
If you want to take a directory argument:
for img in "$1"/*; do
Quoting correctly this way will ensure that your code can deal with directory names containing spaces.
Upvotes: 0
Reputation: 295825
You don't need to change anything about your script; just change how you invoke it:
find . -maxdepth 1 -name '*.jpg' -exec ./checkres.sh '{}' +
This will put only as many filenames on each invocation as will fit, running the script more than once as necessary.
In fact -- unless you change the calling convention, you can't fix this bug by changing anything about your script, since the problem is happening while the shell is trying to start it!
However, there's another workaround -- instead of defining this as an external script, you could define it as a shell function (in your ~/.bashrc
or similar), which means it doesn't need to be started as an external command, meaning the operating system limits in question don't apply.
checkres() {
for img; do
...
done
}
checkres *.jpg
...is executed entirely within the shell, mooting any limits on the length of the command line which can be passed to an external command during its execution.
Upvotes: 3