Like this to avoid if forest ('if' means a kind of tree in French):
#!/bin/bash
case $1 in
*[cC])
echo "$((${1%[a-zA-Z]} + 273))K"
;;
*[kK])
echo "$((${1%[a-zA-Z]} - 273))C"
;;
*)
echo "error arg [$1]" >&2
exit 1
;;
esac
expr
is a program used in ancient shell code to do math.
In Posix shells like bash, use $(( expression ))
.
In bash, ksh88+, mksh/pdksh, or zsh, you can also use (( expression ))
A float precision capable version, using bc
and the real float 273.16:
#!/bin/bash
case $1 in
*[cC])
echo "$(bc <<< "scale=2; ${1%[a-zA-Z]} + 273.16")K"
;;
*[kK])
echo "$(bc <<< "scale=2; ${1%[a-zA-Z]} - 273.16")C"
;;
*)
echo "error arg [$1]" >&2
exit 1
;;
esac