Visit Our UNIX and Linux User Community


Storing output of "time" command to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing output of "time" command to a variable
# 1  
Old 01-21-2011
Storing output of "time" command to a variable

Hi all,

I am new to Linux/shell scripting having moderate knowledge.

In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save the output of time command in a variable. The format of the command is like "time ls -R /opt"

Going further, the o/p of 'time' command is SmilieExSmilie
real 0m0.003s
user 0m0.004s
sys 0m0.000s
Here, in my script, I would like to use only middle line "user 0m0.004s" saved to the variable but unable to find out the way.

Can someone help me out with this issue. Thanks a lot.

Last edited by happening_linux; 01-21-2011 at 08:52 AM..
# 2  
Old 01-21-2011
not sure what are you asking for? but anyways try this..

variable=`command`
# 3  
Old 01-21-2011
Hi Tuxidow.

Thanks for the response.
I had tried the format that u posted but didn't work in case of time command. Command, the result of which I would like to save in a variable is : "time ls -R /opt"

Hope this may clear my question more.
# 4  
Old 01-21-2011
Which shell are you using?
This should work with bash:

Code:
_utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )"

Example:

Code:
$ _utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )"
$ echo "$_utime"
0m0.015s

Note that, as per your request, the output will include only the time spent in user mode!
# 5  
Old 01-21-2011
radoulov makes a valid point. The output from most versions of "time" goes to STDERR not STDOUT.

If you haven't got bash:
Code:
utime=`time ls -R 2>&1 | grep \^user`

echo $utime
user 0.0

# 6  
Old 01-23-2011
Thanks a lot radoulov & methyl for your replies.

The soloution posted by radoulov seems to be working as desired for my script and I'll continue with that.
Thanks again. Smilie

---------- Post updated 01-23-11 at 07:06 AM ---------- Previous update was 01-22-11 at 02:38 PM ----------

Going further with my post earlier, I am little confused about the output of time command. I mean I can't understand the difference between "real time", "user time" and "sys time" shown by "time" command. Can someone please explain the difference between these outcomes?

I need the execution time of commands like " ls -R /<PATH>" that means, the time taken to display all the files under that PATH recursively.

I have gone through some google searches and found that the required time should be "user time" & therefore, I am using "user time" displayed by "time" command for calculating the total time taken ny "ls -R /<PATH>" command. Please let me know if I am doing right ?? Thanks again.
# 7  
Old 01-23-2011
It's hard to improve on the explanation in "man time".

If you just want to know how long a process took to run, use "real time".
If you want to know how much CPU time the process used while in "user mode" use "user".
If you want to know how much CPU time the process used while in "kernel mode", use "sys".

One facinating aspect of unix is that the statistics from the "time" command may not be the same for the second and subsequent invocations of the same command. A well-tuned system will cache data blocks, directory blocks and program pages as it continuously re-tunes itself.
The difference is most evident after a cold start of the Operating System.

Previous Thread | Next Thread
Test Your Knowledge in Computers #80
Difficulty: Easy
Many operating systems today, including Android, Chrome OS, Steam OS, and myriad embedded systems for devices, are based on Linux.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

3. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

4. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

5. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

6. Shell Programming and Scripting

passing the output of cmd from "eval" to a variable

Hello, I need help with the eval command. I have been building a lengthy cmd using eval, and I need to create $var from the output of the cmd. Here is what I have. Out=/dfezz1/output.txt Node="'LPAR Info:'" Gr3p0=" |grep" Printc=" prtconf" Output1=" 1>>$Out 0>&1" Cat1="cat... (8 Replies)
Discussion started by: dfezz1
8 Replies

7. Shell Programming and Scripting

How to store the output of "time dd if= of=" in a variable

Hi All, I need to store the real seconds of the following command in a variable. How could it be done? time $(dd if=/dev/zero of=/dev/sda1 bs=512 count=2048;sync) Thanks, Amio (12 Replies)
Discussion started by: amio
12 Replies

8. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

10. Shell Programming and Scripting

Please help formatting bash "time" variable to HH:MM:SS format

Ok, this is going to be hard to describe, but here it goes. I have written a bash script that, while executing starts a timer, and when done stops the timer. The $RUNTIME variable value is in seconds, so the variable usually equals a number like 126 (equals 2 minutes 6 seconds). In my script I... (3 Replies)
Discussion started by: vikingshelmut
3 Replies

Featured Tech Videos