How do I get just real time value from 'time' command? - bash

I would like to make a script that outputs only the real time value from the time command so I can plot the results. For example time command outputs
real 1m0.001s
user 1m0.000s
sys 0m0.001s
I want to write a script that outputs
60.001
How do I get just real time value from 'time' command in seconds?

If you're using the Bash builtin time, set the TIMEFORMAT variable to %R:
$ TIMEFORMAT=%R
$ time sleep 1
1.022

time can take an optional --format or -f parameter, but you have to use the full path to the time command, /usr/bin/time
/usr/bin/time -f "%e" sleep 3
3.00
Without the path, it'll use the time command of the shell which just treats all arguments as the command, so you'll get an -f: command not found.

If you write \time you enforce not to use the bash built in time.
So with \time -f '%e' command you are done.

Alternatively, use /usr/bin/time or take a look at the similar question
Using time command in bash script.

Related

How to invoke time command with parameter and same behavior as without parameter?

I want to specify the format for time, but it behaves differenty:
$ time echo hehe | sleep 1
real 0m1.03s
user 0m0.00s
sys 0m0.00s
$ time -p echo hehe | sleep 1
real 0.00
user 0.00
sys 0.00
So if I provide any switch to time, it will behave differently:
in first case the time measured the whole commnad's time (with pipe)
in the second case it measured only the echo's time
How can I specify -p and get the result from the first run?
You can't. In ksh, time can time a pipeline, while time -p can only time the first stage.
POSIX time on a pipeline is undefined. ksh's implementation chooses to time the entire pipeline.
ksh time does not support -p or any other flags, and will delegate to external time if it's invoked with any flags.
external time (e.g. /usr/bin/time) can only operate on the first stage of a pipeline (because pipelines are a shell construct and require shell cooperation).
Either switch to a shell that does allow this (like bash), or rewrite to something that external time can work with, such as a single stage with sh -c that internally runs a pipeline:
time -p sh -c 'echo hehe | sleep 1'

How to parse output of pipe in real time?

When I pipe two commands, it seems the first command must finish before the second command could parse the output.
For example,
$ ping -c 5 10.11.12.13 | while read line; do echo $line; done
I expect it would generate output every second, but not. Is it true or I miss something (e.g., buffering effect)?
The problem is: if the first command runs over long period of time and I want to parse the output in real time. How to do it using shell?
Thanks.
You can force the first command to become unbuffered by using a command like unbuffer (from the expect package) or stdbuf. This way, it will flush its output after each line, rather than after it outputs e.g. 4096 bytes.

Using time in a bash script: how to get the "normal" output?

If I use time in the shell, I get an output like
$ time sleep 1
real 0m1.003s
user 0m0.000s
sys 0m0.000s
However, if I use the same command in a bash script, the output is this
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2176maxresident)k
0inputs+0outputs (0major+179minor)pagefaults 0swaps
I already found this post, but what I want is to use time in a script, while still getting the same output as if I was using it in a shell.
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2176maxresident)k
0inputs+0outputs (0major+179minor)pagefaults 0swaps
...is the default output produced by the GNU version of the external time command.
real 0m1.003s
user 0m0.000s
sys 0m0.000s
...is the default output produced by the bash builtin time. This builtin version of time is a bash extension: a POSIX-compliant shell is not required to provide a builtin version of time.
However, POSIX does specify an external time utility, which can take -p option to produce yet another output format:
real 1.01
user 0.00
sys 0.00
...and the bash builtin also accepts the -p option to produce the same output format.
The bash builtin should work perfectly well in a shell script provided the script is actually being run by bash:
$ cat time.sh
#!/bin/bash
time sleep 1
$ ./time.sh
real 0m1.001s
user 0m0.000s
sys 0m0.000s
$
So it seems that your script is invoking the external utility rather than the bash builtin.
The most likely cause of this is that:
your script says #!/bin/sh rather than #!/bin/bash; and
the /bin/sh on your machine is not actually bash, but a more lightweight POSIX-compliant shell without the bash extensions (as found on some Linux distributions these days).
The solution is to ensure that the script specifically invokes bash by using #!/bin/bash if it relies on bash extensions.
Did you try the -p flag?
$ echo "time -p sleep 1" > x1
$ bash x1
real 1.01
user 0.00
sys 0.00
The difference is that output is not going to terminal when piped.
The terminal codes necessary to do the nice layout do not work on a filestream.
What did you want to achieve? If you intend to have the 'screenshot' have a look at script

put output of 'time ./a.out' to a variable

I have used time command to execute program (like "xxx$ time ./a.out"), with output as follows,
real 0m7.250s
usr 0m10.395s
sys 0m0.026s
What I want to get is 0 and 7.250 as in 0m7.250s. I have tried "awk '{print $2}'", but without success; nothing output there.
PS: I have tried put output of time command to a file by using ">", also without success.
Try:
pax$ tm=$((time sleep 1) 2>&1 | awk '/^real/{print $2}') ; echo $tm
0m1.002s
(substituting your own a.out command of course, sleep 1 was just used for an example).
It creates a subshell for the time command and ensures that its standard error is sent to standard output instead (time specifically outputs its information to standard error so that it's kept separate from normal output of the program it's timing).
The awk command the captures the line starting real and outputs the second argument (the time).
The time command sends its output to standard error, so as not to interfere with normal program output. You will want to use 2>&1 to redirect it where it can be captured.

Shell script to calculate time elapsed

I was trying to write a script to execute a C program and measure the time for execution using time command in Unix.
The script looks like follows
cc /home/pop/Daa/abc.c
r = `time /home/pop/Daa/./a.out`
echo "recursion" $r >> log.txt
cc /home/pop/Daa/xyz.c
d = `time /home/pop/Daa/./a.out `
echo "dynamic" $d >> log.txt
But after executing the script the log.txt file contains only words recursion and dynamic. The time values seem to be missing. But executing time command on the commandline terminal gave the following output
real 0m0.001s
user 0m0.000s
sys 0m0.000s
How can get this output formatted to contain only the 'real' time of execution and written in the log file?
The output of time goes to stderr. You have to redirect it.
TIMEFORMAT=%R
r=$( { time /home/pop/Daa/./a.out; } 2>&1 )
And you can't have spaces around the equal sign. It is preferable to use $() instead of backticks since it's easier to read, there's no confusion with single quotes and it's easier to nest them.
When you assign values to a variable you shouldn't have spaces around the equals sign. For example:
r=`time /home/pop/Daa/./a.out`
If you only want the "real" time, use grep:
r=`time /home/pop/Daa/./a.out | grep real`
or use %e in the time command:
r=`time -f '%e' /home/pop/Daa/./a.out`

Resources