johne
johne

Reputation: 3

Bash script to sort JPEGs after pixelWidth

I'm trying to sort a folder on a Mac with hundreds of jpgs into separate folders
after their pixelWidth with /usr/bin/sips. When I run the script it shows an
Error 4: no file was specified. The variable $f is empty in the first line?
I thought the "f" in for f in *jpg referred to the file as it loops through the folder.

imgWid=$( /usr/bin/sips -g pixelWidth $f | sed -n 2p | cut -d":" -f2- )  

for f in *jpg; do if [ "$1" == "$imgWid" ]; then; mv $f $dir1 2>/dev/null; fi; done  

I would appreciate help on how I can fix this.

Upvotes: 0

Views: 139

Answers (2)

Douglas Leeder
Douglas Leeder

Reputation: 53310

$() runs the command at the point specified. (So line 1), $f is only defined in the loop (line 3).

Maybe:

function determineImageWidth()
{
    /usr/bin/sips -g pixelWidth $1 | sed -n 2p | cut -dj":" -f2-
}

for f in *.jpg
do
    width=$(determineImageWidth $f)
    if [ -n $width ] 
    then
        mkdir -p $width
        mv $f $width/
    else
        echo "Can't determine the width for $f"
    fi
done

Upvotes: 2

ezdazuzena
ezdazuzena

Reputation: 6770

You didn't specify what $f is in the first line of your script. As far as I can see it is just specified within the for loop. You might want to change $f to $1 in the first line, to use an argument given to your script.

Upvotes: 0

Related Questions