Skip to content

Instantly share code, notes, and snippets.

@Pival81
Last active August 7, 2016 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pival81/f52ba9888f43f66519d2440637cff285 to your computer and use it in GitHub Desktop.
Save Pival81/f52ba9888f43f66519d2440637cff285 to your computer and use it in GitHub Desktop.
C2K
#!/usr/bin/env bash
# Valerio Pizzi (Pival81) <pival81@yahoo.com>
set -u
set -e
# Checks the system language, and stores it in the script_locale variable.
script_locale=$(locale | grep LANG | cut -d= -f2 | cut -d_ -f1)
# A function to set the the script language using the script_locale variable.
function setlocale () {
if [ $script_locale = it ]; then
if [ $(basename $0) = c2k ]; then
descri="Questo script serve a convertire da gradi Celsius a Kelvin."
elif [ $(basename $0) = k2c ]; then
descri="Questo script serve a convertire da gradi Kelvin a Celsius."
fi
dotcomma="SE DEVI UTILIZZARE NUMERI DECIMALI USA IL PUNTO, NON LA VIRGOLA."
exit="Se si vuole uscire, basta digitare 'esci' al posto del valore."
howtoconv="scegli come vuoi convertire:"
cvalue="Inserisci il valore dei °C."
kvalue="Inserisci il valore dei °K."
c2k="°C corrispondono a"
k2c="°K corrispondono a"
elif [ $script_locale = en ] || [ $script_locale != it ]; then
if [ $(basename $0) = c2k ]; then # The description changes according to how the script is invoked.
descri="This script converts °C to °K."
elif [ $(basename $0) = k2c ]; then
descri="This script converts °K to °C."
fi
dotcomma="IF YOU HAVE TO USE DECIMAL NUMBERS USE THE DOT, NOT THE COMMA."
exit="If you want to exit, just type 'exit' instead of the value."
howtoconv="choose how you want to convert:"
cvalue="Insert the value of °C."
kvalue="Insert the value of °K."
c2k="°C correspond to"
k2c="°K correspond to"
fi
}
# Initiate the script.
function executescript () {
echo -e "$descri"
echo
echo "$dotcomma"
echo "$exit"
echo
# Set the mode by checking how the script is invoked.
if [ $(basename $0) = c2k ]; then
echo "$cvalue"
echo -n "--> "
read value
if [ -z "$value" ] || [ "$value" = "esci" ] || [ "$value" = "exit" ]; then
exit
fi
result=$(echo "scale=2; $value+273.15" | bc)
echo "${value}$c2k ${result}°K"
elif [ $(basename $0) = k2c ]; then
echo "$kvalue"
echo -n "--> "
read value
if [ -z "$value" ] || [ "$value" = "esci" ] || [ "$value" = "exit" ]; then
exit
fi
result=$(echo "scale=2; $value-273.15" | bc)
echo "${value}$k2c ${result}°C"
fi
}
# If the script is invoked using the -l option, force the script to open using the language specified after -l .
while getopts ":l:" opt; do
case $opt in
l)
script_locale=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
setlocale # A call to the setlocale function, to set all the phrases to the preferred languages.
executescript # A call to the executescript function, to do all the math.
exit # Just exit and forget this hell.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment