[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash scripting question



On 20100319_101928, Mike McClain wrote:
> I've written a function to print elapsed time similar to /usr/bin/time
> but can be called at the beginning and end of a script from within
> the script. Occasionally I get an error: '8-08: value too great for base'
> It's caused by the difference in these 2 command strings but I can't for 
> the life of me see what's going on.
> 
> now='09:07:16'; startHr=${now%%:*}; startHR=${startHr#*0}; echo $startHr; 
> 09
> 
> str=09; str=${str#*0}; echo $str; 
> 9

Try:

bgn=$(date +%s)
sleep 7
end=$(date +%s)
echo "elapsed seconds = " $(( end - bgn ))

Play with values other than '7'.

To actually use in a script, replace the sleep command by whatever you
want to measure the elapsed time for. 

Expressing elapsed time as HH:MM:SS requires a little more work. To 
write that script, use / for divide operator and % for remainder operator,
like in C.

-- 
Paul E Condon           
pecondon@mesanetworks.net


Reply to: