How to add two variable in bash?

How to add two time outputs in bash

  • Hy! I want to calculate in bash the average time spent by several commands. The output of time command is min:sec.milisec I don't know how to add two outputs of this kind in bash and in final to calculate the average. I tried to convert the output with date but the output is "date: invalid date `0:01.00'" Thanks

  • Answer:

    This is a three part answer Part one First, use the TIMEFORMAT variable to output only seconds elapsed. Then you can add this directly From man bash TIMEFORMAT The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions. Here is an example which outputs only seconds with a precision of 0, i.e. no decimal point. Read part three why that's important. TIMEFORMAT='%0R'; time sleep 1 1 Part two Second, how do we capture the output of time? It's actually a bit tricky, here's how you do capture the time from the command above TIMEFORMAT='%0R'; time1=$( { time sleep 1; } 2>&1 ) Part three How do we add the times together and get the average? In bash we use the $(( )) construct to do math. Note that bash does not natively support floating point so you will be doing integer division (hence the precision 0.) Here is a script that will capture the time from two commands and output each of the individual times and their average #!/bin/bash TIMEFORMAT='%0R' time1=$( { time sleep 1; } 2>&1 ) time2=$( { time sleep 4; } 2>&1 ) ave=$(( (time1 + time2) / 2)) echo "time1 is $time1 | time2 is $time2 | average is $ave" Output time1 is 1 | time2 is 4 | average is 2 If integer division is a non-starter for you and you want precision, as long as you don't mind calling the external binary bc, you can do this quite easily. #!/bin/bash TIMEFORMAT='%3R' time1=$( { time sleep 1; } 2>&1 ) time2=$( { time sleep 4; } 2>&1 ) ave=$( bc <<<"scale=3; ($time1 + $time2)/2" ) echo "time1 is $time1 | time2 is $time2 | average is $ave" Output time1 is 1.003 | time2 is 4.003 | average is 2.503

banuj at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

For the example i'll use a variable preinitialized: time="54:32.96"; minutes=$(echo "$time" | cut -d":" -f1) seconds=$(echo "$time" | cut -d":" -f2 | cut -d"." -f1) millis=$(echo "$time" | cut -d":" -f2 | cut -d"." -f2) #Total time in millis totalMillisOne=$(($millis+$seconds*1000+$minutes*60000)) You do this with every command and you save it in diferent vars, and then you do the average: let avMillis=$totalMillisOne+$totalMillisTwo let avMillis=$avMillis/2 And you output it in the same input format: let avSeconds=$avMillis/1000 let avMillis=$avMillis-$avSeconds*1000; let avMinutes=$avSeconds/60; let avSeconds=$avSeconds-$avMinutes*60; echo "${avMinutes}:${avSeconds}.${avMillis}"

elxordi

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.