0

I have the following script. When it runs it prints the Start and end Times to the file 'result.txt'.

However, I also want to record the total runtime (end-start), but where I am doing echo runtine at the end, it just returns runtime: with nothing recorded next to it. Am I doing something incorrect?

#!/bin/bash
clear
echo "Test 001" > result.txt
echo "start time: " $(date +%T) >> result.txt
start=`date +%s`
#DO STUFF HERE
end=`date +%s`
echo "end time: " $(date +%T) >> result.txt
runtime=$((end-start))
echo "runtime: " $(runtime) >> result.txt
echo " - - - "

2 Answers 2

3

You have a typo in your echo statement. It should be

#!/bin/bash
#clear
echo "Test 001" > result.txt
echo "start time: " $(date +%T) >> result.txt
start=`date +%s`
#DO STUFF HERE
end=`date +%s`
echo "end time: " $(date +%T) >> result.txt
runtime=$((end-start))
echo "runtime: $runtime" >> result.txt
echo " - - - "
3
  • You beat me to it! Commented Apr 8, 2015 at 11:08
  • 1
    @PandaLion98 haha...sorry about that :) Commented Apr 8, 2015 at 11:09
  • Take an upvote. Commented Apr 8, 2015 at 11:09
2

First of all, you are reinventing the wheel. That's what the time command is for:

$ time script.sh
real    0m0.005s
user    0m0.000s
sys     0m0.004s

Then, you have a syntax error:

echo "runtime: " $(runtime) >> result.txt

The $(foo) syntax is command substitution, it will try to run foo. What you meant was

 echo "runtime: $runtime" >> result.txt

By the way, you should always include the error messages you get in your question.

2
  • cheers, that makes sense. I didn't use time as I wanted the result stored in the file. Also, I didn't get any error messages (not that I saw anyway). Commented Apr 8, 2015 at 12:51
  • @IGGt you can still use time, just use a subshell: ( time script.sh ) 2> log.txt Commented Apr 8, 2015 at 13:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.