Register
It is currently Thu Aug 21, 2014 5:57 pm

Getting value of time in a variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Nov 30, 2010 9:57 am   

Joined: Tue Nov 30, 2010 9:51 am
Posts: 1
Hello,

how do I get the value from "/usr/bin/time" in a variable?

For example if I use:
Code:
/usr/bin/time -f "%U" ls

It will return something like 0.00
This number I would like to save in a variable.

I tried:
Code:
variable1=$(/usr/bin/time -f "%U" ls)

But then $variable1 gets what the ls-command returned not how much time the command needed.

So how do I save the time? :-/


Top
 Profile  
 PostPosted: Tue Nov 30, 2010 12:36 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Assigning the output of a command to a variable like you do will save the stdout of the command. The stderr will still be shown on your error display (usually your tty). The 'time' command sends its own output to stderr and the output of the command to run to stdout. So you're grabbing the wrong output here.
My suggestion would be to redirect the output of 'time' to stdout and the output of 'ls' to /dev/null (or whereever you like :) )


Top
 Profile  
 PostPosted: Wed Dec 01, 2010 11:55 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Patsie wrote:
My suggestion would be to redirect the output of 'time' to stdout and the output of 'ls' to /dev/null (or whereever you like :) )

In other words,
Code:
t=$(/usr/bin/time -f '%U' ls  2>&1 > /dev/null)

Be warned, though, that the output of the shell keyword time cannot be captured like this. You would need to run the command in a subshell for that:
Code:
t=$( (time ls)  2>&1 > /dev/null )


Top
 Profile  
 PostPosted: Wed Dec 22, 2010 1:55 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Code:
# We can re-direct STDERR to STDOUT and run
# the output through awk to neatly snip out the
# number that we want...

t=$(/usr/bin/time -p ls 2>&1|awk '/^user/ {print $2}')

echo "User time: $t"  # results in "User time: 0.00"


I know this is late, but hopefully somebody finds it helpful :)

-J


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP