-1

I have a script that has a loop that will take several days to execute, the loop includes commands that are timed using the $SECONDS variable.

SECONDS=0
    for f in *.extension
    do
      SECONDS=0 
        some command
        if (( $SECONDS > 3600 )) ; then
            let "hours=SECONDS/3600"
            let "minutes=(SECONDS%3600)/60"
            let "seconds=(SECONDS%3600)%60"
            echo -e " Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)" 
        elif (( $SECONDS > 60 )) ; then
            let "minutes=(SECONDS%3600)/60"
            let "seconds=(SECONDS%3600)%60"
            echo -e " Completed in $minutes minute(s) and $seconds second(s)"
        else
            echo -e " Completed in $SECONDS seconds"
         fi
some other command wrapped between another second timer
some other command wrapped between a 3rd timer
      done
    if (( $SECONDS > 3600 )) ; then
            let "hours=SECONDS/3600"
            let "minutes=(SECONDS%3600)/60"
            let "seconds=(SECONDS%3600)%60"
            echo -e " Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)" 
        elif (( $SECONDS > 60 )) ; then
            let "minutes=(SECONDS%3600)/60"
            let "seconds=(SECONDS%3600)%60"
            echo -e " Completed in $minutes minute(s) and $seconds second(s)"
        else
            echo -e " Completed in $SECONDS seconds"
         fi

I get no output for the last timer sitting outside the loop. Another way around it, that I have tried and failed at is setting a $beforedate at the top of the script and a $afterdate and calculating the difference to get the time but cant seem to figure it out.

so something like

var1='date'
some command
var2='date'

difference=$(( $(date "$var1" "+%s") - $(date "$var2" "+%s") ))

echo "scale=2 ; $difference/3600" | bc

But that doesnt work either.

Hani Umer
  • 21
  • 4
  • `var1='date'` sets `var1` to the literal string 'date', you want to evaluate the command `date`, so try `var1=$(date)`. Also, at least on my machine, `date` requires the `-d` flag prior to the date you want to output, so `difference=$(( $(date -d "$var1" "+%s") - $(date -d "$var2" "+%s") ))`. – lawruble13 Oct 02 '20 at 23:26
  • why don't you use the time command? What is the output format you want for every execution you time? HH:MM:SS? – thanasisp Oct 02 '20 at 23:27
  • 2
    `/usr/bin/time -f %E command` will output directly in H:M:S format – Léa Gris Oct 02 '20 at 23:31
  • 2
    Does this answer your question? [Custom format for time command](https://stackoverflow.com/questions/3683434/custom-format-for-time-command) – thanasisp Oct 02 '20 at 23:32

1 Answers1

0

Thanks lawruble for the comments I figured out the syntax error I was making. So this is how the Solution i used

#!/bin/bash

date1=$(date --date 'now' +%s) #date since epoch in seconds at the start of script
somecommand
date2=$(date --date 'now' +%s) #date since epoch in seconds at the end of script
difference=$(echo "$((date2-$date1))") # difference between to values
date3=$(echo "scale=2 ; $difference/3600" | bc) # define 3rd variable for echo
echo SCRIPT TOOK $date3 HRS TO COMPLETE
Hani Umer
  • 21
  • 4