I'm using this :
for example ./imgSorter.sh -d directory -f format
the scripts' content is :
#!/bin/bash
while getopts ":d:f:" opt; do
case $opt in
d)
echo "-d was triggered with $OPTARG" >&2
;;
f)
echo "-f was triggered with $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
use cases :
$ ./imgSorter.sh -d myDir
-d was triggered with myDir OK
$ ./imgSorter.sh -d -f myFormat
-d was triggered with -f NOK : how is it that a string beginning with - is not detected as a flag ?