Show time elapsed with script

I would like to modify this to display how much time has elapsed. If someone can give me a start,
I will work at the problem.

#!/bin/bash
#
#  Sound alarm after specifying time and message 
# Must input time delay AND message in double quotes !!
#
#       
# ** sleep can also accept intergers ex. sleep 7.63
# Made alias for it type al and time
# 2> /dev/null suppresses messages for amixer AND (c)vlc
#
soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"
#originalVolume=$(amixer -D pulse get Master | grep -m 1 -o -E [[:digit:]]+%)
clear
amixer -D pulse sset Master 100% > /dev/null 2>&1
if [ -f "$soundfile" ];
then
     echo "Soundfile is present."
else
      
  echo "File $soundfile does NOT exist."
  echo
  echo "Program will now exit."
  exit
fi

[ -z "$2" ] && {
echo 
echo -e "   Error!! No time value given and/or message specified !!"
echo
echo -e "   Alarm Program 2018"
echo
echo -e "   alarm.sh [time value in seconds] Message in double Quotes"; 
echo -e "   alarm 5m   = 5 minute alarm"
echo -e "   alarm 5h   = 5 hour alarm"
echo -e "   alarm 5d   = 5 day alarm"
echo -e "   alarm 1.5m = 1 minute 30 seconds alarm"
echo 
echo -e "   alarm.sh 1m "\"Tea is ready."\""  

echo 
exit 1; }

echo  -e "\033[32;5mTIMER COUNTING DOWN to $1 \033[0m"
sleep $1
{
    for ((volume = 35; volume <= 85; volume += 2)); do
        amixer -D pulse sset Master ${volume}% > /dev/null
        sleep .5
    done
} &

cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
#set back to original volume
amixer -D pulse sset Master %85
echo $2

gxmessage -fg blue -font  'sans 20' -timeout 2 ' TIME IS UP !!'

I found something that may help. I don't know how to transfer the time from the command line to the hour and minute.

#!/bin/bash
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m'
hour=0
min=0
sec=11
tput civis
echo -ne "${GREEN}"
        while [ $hour -ge 0 ]; do
                 while [ $min -ge 0 ]; do
                         while [ $sec -ge 0 ]; do
                                 if [ "$hour" -eq "0" ] && [ "$min" -eq "0" ]; then
                                         echo -ne "${YELLOW}"
                                 fi
                                 if [ "$hour" -eq "0" ] && [ "$min" -eq "0" ] && [ "$sec" -le "10" ]; then
                                         echo -ne "${RED}"
                                 fi
                                 echo -ne "$(printf "%02d" $hour):$(printf "%02d" $min):$(printf "%02d" $sec)\033[0K\r"
                                 let "sec=sec-1"
                                 sleep 1
                         done
                         sec=59
                         let "min=min-1"
                 done
                 min=59
                 let "hour=hour-1"
         done
echo -e "${RESET}"
tput cnorm

I can't be sure I understand your question. But I think you are asking how to pass command line parameters into a bash script.

The first script includes the answer. "$1" in the script is the value of the first parameter, and "$2" is the second.

Yes, I want to pass command line parameters to the alarm script.

I know that $1 is the time while $2 is the message.

So what is the question?

echo displays text or variables to stdout--text is quoted, variables are preceeded with $.

You know how to get the time passed into the script.

So I think all the questions are answered--but clearly I'm not understanding what you're missing.

You may find what you want in this code snippet, which is in my .bashrc file. You could modify the parts inside the function into your script.

function countdown(){
   date1=$((`date +%s` + $1)); 
   while [ "$date1" -ge `date +%s` ]; do 
     echo -ne "$(date -u --date @$(($date1 - `date +%s`)) +%H:%M:%S)\r";
     sleep 0.1
   done
}

I didn't write it, and don't recall where I found it.

1 Like

On the date1 line, I get

syntax error: operand expected (error token is "+ ")

I pasted @charles-nix 's code to my terminal, then countdown 10 it counts down 10 seconds.

(I found the countdown() function here ... linux - Is there a way to display a countdown or stopwatch timer in a terminal? - Super User)

1 Like

Hmm. Some interesting comments here, but when I first read the question, "

My first thought was to just use the "time" command. If you call the script using the 'time' command it will then display time values on completion. For example, I have a script I run called "todaysfiles:

touch -t date +%m%d0000 /tmp/$$
find $1 -type f -newer /tmp/$$ -ls | sort -t ' ' -n -b +7
rm /tmp/$$

If I run it using the 'time ' command

'time todaysfiles' in my /tmp directory, after the display of files, I see

real 0m0.653s
user 0m0.013s
sys 0m0.006s

Is that helpful?

Your script times how long it takes your program to run.

I am looking to modify my alarm script so that it shows a countdown time as the time elapses.

Your script times how long it takes your program to run.

I am looking to modify alarm.sh to show the time remaining.

I can not get this to show my string?

countdown() {
    start="$(( $(date '+%s') + $1))"
    while [ $start -ge $(date +%s) ]; do
        time="$(( $start - $(date +%s) ))"
        printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
        printf  β€œTea is ready."

        sleep 0.1
            done
}

stopwatch() {
    start=$(date +%s)
    while true; do
        time="$(( $(date +%s) - $start))"
        printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
        sleep 0.1
    done
}

The quote before Tea is different than the quote after ready. I added this to my .bashrc sourced it.
Also added a \n after Tea is ready. countdown 10 shows:

countdown() {
    start="$(( $(date '+%s') + $1))"
    while [ $start -ge $(date +%s) ]; do
        time="$(( $start - $(date +%s) ))"
        printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
        sleep 0.1
    done
    printf  "Tea is ready.\n"
}

user@lake:~$ countdown 5
Tea is ready.
user@lake:~$

Thanks pavlos_kairus.

.bashrc is like one big collection of scripts that can be run.

:slight_smile:

Hi fixit7,
I really like your script so I started to toy around a bit with it.
I edited your script a bit until it mostly did what you asked.

I skipped the fractions part, bash only supports integers.
There are several ways though (by using, for instance, the 'bc' command) but I'll leave that up to you for the moment.

I hope I didn't make a mess of it
( btw I used a line from @charles-nix 's example) :

#!/bin/bash
#
# "I am looking to modify my alarm script
# so that it shows a countdown time as the time elapses"
#
#  Sound alarm after specifying time and message 
# Must input time delay AND message in double quotes !!
#       
# ** sleep can also accept intergers ex. sleep 7.63
# Made alias for it type al and time
# 2> /dev/null suppresses messages for amixer AND (c)vlc

soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"

#set back to original volume when leaving this script, even on Ctrl-C
trap "amixer -D pulse sset Master %85" EXIT
trap "exit" INT TERM

#originalVolume=$(amixer -D pulse get Master | grep -m 1 -o -E [[:digit:]]+%)
clear
amixer -D pulse sset Master 100% > /dev/null 2>&1
if [ -f "$soundfile" ];
then
     echo "Soundfile is present."
else
  echo -e "File $soundfile does NOT exist.\n\nProgram will now exit.\n"
  exit
fi


# Below, I cleaned it up a bit by using a  "here document"

[ -z "$2" ] && cat<<ERRORMESSAGE && exit 1

   Error!! No time value given and/or message specified !!

   Alarm Program 2018

   alarm.sh [time value in seconds] Message in double Quotes 
   alarm 5m   = 5 minute alarm
   alarm 5h   = 5 hour alarm
   alarm 5d   = 5 day alarm
   alarm 1.5m = 1 minute 30 seconds alarm

   alarm.sh 1m "Tea is ready."  

ERRORMESSAGE

# alternative way to do colors
Reset=$( tput sgr0    ) ; Bl=$( tput blink   ) ; Gr=$( tput setaf 2 )

echo  -e "${Gr}${Bl}TIMER COUNTING DOWN to $1${Reset}"


# countdown display
# below is the part you are insterested in
# it replaces your "sleep" command


#this converts days,hours,months to seconds
case "$1" in
*d ) fact=86400 ;;
*h ) fact=3600  ;;
*m ) fact=60    ;;
*  ) fact=1     ;;
esac
(( period = ${1/[a-z]/} * fact ))


#this calculates the endtime based on 'now'
start=$( date +"%s" ) ; (( finish = start + period ))


# this is the counter,  based around charles-nix's counter format idea 
while (( period >= 0 ))
do
       echo -ne "$(date -u --date @${period} +%H:%M:%S)\t\t\r";
       (( period = finish - $( date +%s ) )) 2>/dev/null
done	    	 


# Fade-in process:
#you used curly brackets but you probably wanted  parenthesis
(
    for ((volume = 35; volume <= 85; volume += 2)); do
        amixer -D pulse sset Master ${volume}% > /dev/null
        sleep .5
    done
) &


cvlc --play-and-exit "$soundfile" &> /dev/null
echo $2

gxmessage -fg blue -font  'sans 20' -timeout 2 ' TIME IS UP !!'
1 Like

It worked well.

if [ ! -f "$soundfile" ];

This is what is needed.

if [ -f "$soundfile" ];

Oops, that was something I forgot to change back after some testing :blush:
I'll correct it.

No problem.

One other thing.

My string is getting chopped off.

Soundfile is present.
TIMER COUNTING DOWN to 1s
Tea00:00

Your 'tea is ready' is printed earlier than the 00:00 of the counter.
That means that the counter is backgrounded.
That is weird, this is my output:

Soundfile is present.
TIMER COUNTING DOWN to 1s
Tea is ready.

What happens if you place an 'echo' between counter and fade-in process ?

Well, I was not clear on what you wanted based on the phrasing of your question. I see others have already offered solutions, so I'll not confuse the issue.

1 Like

Well, OldStrummer I do appreciate your help.