1

I run a program multible time using a bash script, and I want it's output to be written in a file. However, I couldn't make the operator ">filename.txt" work... Here is my script:

for i in {1..10000000..10000}
do
    time ./merge $i>nn
done

can somebody help me out?

Update:

The answer is:

{ time ./merge $i ; } 2>> time.txt
muru
  • 207,228
Jim Blum
  • 1,437
  • 5
  • 20
  • 24

4 Answers4

3

time is a bit funny. It's a Bash built-in so the first thing you need to do is limit it to the right command. You can do that by grouping it with braces or subshelling with parenthesis.

Then you need to note that it outputs to STDERR. This won't be redirected by default but we can fix this by redirecting STDERR into STDOUT and then redirecting that.

Finally (as choroba spotted before me) using > will overwrite by default. Doing it in a loop will result in just the last iteration showing in the file. You want >> which will append.

{ time ./merge $i ; } >> nn 2>&1

If you don't want any original STDOUT, and just want the time output, you could run this instead:

{ time ./merge $i >/dev/null 2>&1; } 2>> nn

This is junking all the output of the ./merge command and is just redirecting the STDERR from the wider block.


Just as a test harness to show this working:

$ for i in {1..10}; do { time echo $i >/dev/null 2>&1; } 2>> nn ; done
$ wc -l nn
40 nn

That's 10×4-line time blocks (the echo output is suppressed).

Oli
  • 299,380
1

You are overwriting the file in every iteration of the loop. Either, redirect the whole loop to the file

for i in {1..10000000..10000}
do
    time ./merge $i
done > nn

or, use append:

for i in {1..10000000..10000}
do
    time ./merge $i >> nn
done
choroba
  • 10,313
1

time prints its results to standar error, not standard output. You therefore need to redirect the former with 2> instead of the latter with >. Your next issue is that you are capturing the output of ./merge and not of time. So, you need to group the time and ./merge commands and then redirect their output. This can be done either by running them in a separate subshell or by grouping them in the current shell:

  1. Run in a separate subshell

    for i in {1..10000000..10000}
    do
      ( time ./merge $i ) 2>> nn
    done
    
  2. Run in the same shell

    for i in {1..10000000..10000}
    do
      { time ./merge $i; } 2>> nn
    done
    

In this particular case, there is no practical difference between the two approaches.

terdon
  • 104,119
1

The bash intrinsic time is not the same as /usr/bin/time. The latter will allow you to append to a file:

/usr/bin/time --append --outout=outputfile command
Jack
  • 11