This is an updated version of the script Fahrenheit / Celsius temperature converter. I have changed the script to be non-interactive, using positional parameters instead of asking for user input once the script is running. In my testing of the script, everything seems to work as it should. But I am quite new to using positional parameters and I'm wondering if this is the best way of using them or if there might be a more streamlined approach.
help shift
says:
shift: shift [n] Shift positional parameters. Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is not given, it is assumed to be 1. Exit Status: Returns success unless N is negative or greater than $#.
Which leaves me still uncertain as to what that means exactly or how to implement it correctly. The script seems to work but I'd like to know if I could / should do anything differently.
#!/bin/bash
usage() {
echo -e "usage: temp [-f][-c]|[number]|[-h]\nExample:\n\ttemp -f 54\n\ttemp -c 24.5\n"
}
re='^-?[0-9]+([.][0-9]+)?$'
if [ "$1" = "" ]; then
usage
exit 1
elif [[ ! "$2" =~ $re ]]; then
usage
exit 1
elif [[ "$3" != "" ]]; then
usage
exit 1
fi
while [ "$1" != "" ]; do
case $1 in
-f ) shift
RES="$(echo "scale=2; (${1}-32)*5/9" | bc -q -l; printf "\b˚C")"
echo "${1}˚F = "${RES}
exit
;;
-c ) shift
RES="$(echo "scale=2; ${1}*9/5+32" | bc -q -l; printf "\b˚F")"
echo "${1}˚C = "${RES}
exit
;;
-h ) usage
exit
;;
* ) usage
exit 1
esac
done