I have created a Bash script and I have some rather fugly arguments validation. I know about getopt and getopts but thought they where overkill. This is how I want the usage:
Usage: flipfile [OPTION] inputfile outputfile
Options:
-f Force. Accept ANY inputfile, not just regular files.
And my current validation code is:
if [ $# -eq 2 ]; then
infile=$1
outfile=$2
elif [ $# -eq 3 -a $1 = "-f" ]; then
option=$1
infile=$2
outfile=$3
else
echo -e "Error. Usage: flipfile [OPTION] inputfile outputfile\n\n"
echo -e "Options:\n-f\tForce. Accept ANY inputfile, not just regular files."
exit 1
fi
The validation works. But as I'm doing this for fun and for the learning experience I appreciate any tips which may help me write cleaner Bash scripts.
How would you improve this validation code? Use getopt or getopts if you think it's the right decision. I want the optional -f to come as the first argument. If you think [[ conditional-expression ]]
is cleaner than what I have, feel free to change that too. I'll accept the answer I think is cleanest.