Pixel count

Here's a second similar shell script that will display the number of pixels of an image file. It can also be used to select and copy images that are within a certain range, and to make size conversions based on final number of pixels while maintaining aspect ratio.

Getopts mini-tutorial

The bash shell built-in getopts is used to read command line parameters passed to a script. It can read single letter parameters, single letter parameters with a single argument, possibly repeated single letter parameters, possibly joined, but not long parameters. For instance, it can read: script -h -vv -f "my file" arg1 arg2, in that case the parameters are h, f and v (twice). But it can't read -long --long -f File1 File2... Here's how you can use it, with a simplified example:

#! /bin/bash
function usage {
cat << EOF
USAGE: $0 [-v] [-h] [-f File] [args...]
EOF
}
while getopts vf:h opt; do
# 'opt' is the variable that will contain each letter in turn.
# 'vf:h' describes the list of single letter parameters,
# with a : if there's an optional argument (which ends up in $OPTARG)
case $opt in
v) echo "parameter -v";; # the order of vgh doesn't matter
f) echo "parameter -f with argument $OPTARG";; # Here we make use of the argument passed along -f
h) usage; exit 1;;
?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $(($OPTIND - 1)) # This is necessary so that now the remaining arguments end up as $1, $2...
if [ $# -eq 0 ]; then usage; exit 1; fi # No other arguments, may or may not be a good thing
while [ "$1" != "" ]; do
echo "Argument: $1" # Now list all the remaining arguments
shift # Next argument
done

There are a few other options. For more info you can try help getopts since man doesn't usually work for builtins. And here's the result: