Skip to content

Instantly share code, notes, and snippets.

@manero6
Last active October 19, 2023 09:19
Show Gist options
  • Save manero6/aab0324322e04a1e080589855ecc12a1 to your computer and use it in GitHub Desktop.
Save manero6/aab0324322e04a1e080589855ecc12a1 to your computer and use it in GitHub Desktop.
Simple bash script to calculate total seconds from hours, minutes and seconds
#!/bin/bash
HOU=0
MIN=0
SEC=0
if [[ $# -ge 4 ]]
then
echo "Please provide from 0 to a maximum of 3 arguments"
exit 1
elif [[ $# == 0 ]]
then
read -e -i $HOU -p "Please input hours (default: 0) -> " HOU
read -e -i $MIN -p "Please input minutes (default: 0) -> " MIN
read -e -i $SEC -p "Please input seconds (default: 0) -> " SEC
elif [[ $# == 1 ]]
then
echo "You provided only hours, defaulting minutes and seconds to 0."
HOU=$1
elif [[ $# == 2 ]]
then
echo "You provided only hours and minutes, defaulting seconds to 0."
HOU=$1
MIN=$2
elif [[ $# == 3 ]]
then
HOU=$1
MIN=$2
SEC=$3
fi
echo "${HOU} hour(s) ${MIN} minute(s) ${SEC} second(s) are equal to:"
echo $((($HOU*3600) + ($MIN*60) + $SEC)) seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment