The easiest (best?) way to do this in bash is to just call date
and have it output it's time in seconds. The timer.sh
script will call your executable (randbegend.sh
for my testing purposes) and then look for the BEGIN and END lines to trigger the call to date. Once your executable quits, timer.sh
will then calculate and display the time delta in seconds.
timer.sh
#!/bin/bash
while read line; do
if [[ "$line" == "BEGIN" ]]; then
t1=$(date "+%s")
echo t1=$t1
elif [[ "$line" == "END" ]]; then
t2=$(date "+%s")
echo t2=$t2
fi
done < <(./randbegend.sh) # Change this to call your executable
echo "delta = $((t2 - t1)) seconds"
randbegend.sh
#!/bin/bash
sleep $((RANDOM % 5))
echo BEGIN
sleep $((RANDOM % 10))
echo END
Output
$ ./timer.sh
t1=1292451820
t2=1292451825
delta = 5 seconds
$ ./timer.sh
t1=1292451886
t2=1292451889
delta = 3 seconds
$ ./timer.sh
t1=1292451896
t2=1292451903
delta = 7 seconds