LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2003, 12:41 PM   #1
decates
LQ Newbie
 
Registered: Jan 2003
Location: UK
Distribution: Gentoo
Posts: 14

Rep: Reputation: 0
Angry Bash Variable Problem


I'm trying to save the time it takes to run a command to a variable on a bash shell.

What I've tried is:

PINGTIME=$( time -f '%e' ping -c 5 -q www.google.com )

I am expecting the output of the 'time' command to be saved to the variable, but only seems to save the output of the 'ping' command, and the output of the 'time' command escapes to the console and is displayed. Similarly, if I try a different command, such as 'clock', it saves clock's output, not time's. i.e.

[user@linux user]# TESTTIME=$( time -f '%e' clock )
0.09
[user@linux user]# echo $TESTTIME
Mon Jan 27 17:35:09 2003 -0.087824 seconds

What I want to be saved to TESTTIME there is '0.09', but alas no.
I can't think of the syntax to get the right output saved. Any help appreciated.
 
Old 01-27-2003, 01:01 PM   #2
mhearn
LQ Guru
 
Registered: Nov 2002
Location: Durham, England
Distribution: Fedora Core 4
Posts: 1,565

Rep: Reputation: 47
the time you're invoking is the bash builtin. I'm not sure how you'd trap that output. man time will tell you about the program in /usr/bin/time, which is probably what you want, and that will write the output of it to stdout.
 
Old 01-27-2003, 01:23 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
TTIME=$(/usr/bin/time -f '%e' <cmd+args> > /dev/null)
should do as the output from <cmd+args> goes to stderr.
 
Old 01-27-2003, 01:25 PM   #4
decates
LQ Newbie
 
Registered: Jan 2003
Location: UK
Distribution: Gentoo
Posts: 14

Original Poster
Rep: Reputation: 0
thanks unspawn, I tried that before, but the output STILL goes to the console, and the variable is left empty!

I've also tried various other combinations but can never seem to trap or redirect time's output - it seems to get to the console whatever I do. If it won't work this way, is there any other way to find out the elapsed time to run a command (other than writing a small C/C++ program)?

Thanks for your help.

Last edited by decates; 01-27-2003 at 01:55 PM.
 
Old 01-27-2003, 02:58 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Yeah, I see. And a construction like "| tee /dev/stderr 2>&1" doesn't work too, I suggest you use time with "-o <tempfile>" until a real guru gives the answer.
 
Old 01-27-2003, 04:11 PM   #6
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 48
Here's something that may work for you:
Code:
export PT=`/usr/bin/time -o /dev/stdout -f%e /bin/ping -c5 -q localhost | grep -vi ping | grep -vi pack | grep -vi round`
The greps are just to remove the output from ping, and the -o
/dev/stdout option to time is the only way to get time to output to
STDOUT instead of STDERR. . . There is still an issue with a
newline in the resulting variable, but I'm sure you can deal with
it. . .
 
Old 01-27-2003, 07:05 PM   #7
decates
LQ Newbie
 
Registered: Jan 2003
Location: UK
Distribution: Gentoo
Posts: 14

Original Poster
Rep: Reputation: 0
That worked a treat, thanks for the help all.

Last edited by decates; 01-27-2003 at 07:11 PM.
 
Old 01-27-2003, 08:32 PM   #8
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 48
If you are worried about milisecond accounting, you might check into
whether or not time is counting all the greps. . . I haven't bothered, as
it doesn't take a lot of time, but it may throw off your statistics a bit.
 
Old 01-28-2003, 05:22 AM   #9
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
time pushes results of "command" thru stdout and then outputs the time on stderr. Assignment of a command in a shell gets the data from the commands stdout. Try this in bash

PINGTIME=$(/usr/bin/time -f %e 2>&1 ping -c 5 -q xxx >/dev/null )

The 2>&1 redirects time stderr into stdout and pings stdout goes to /dev/null.
 
Old 01-28-2003, 08:55 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Excellent solution Moses!

Btw, I think I've found another solution:
Code:
#!/bin/sh
cmd() {
read line << EOF
`/usr/bin/time -f '%e' ping -c 2 -q www.google.com 2>&1 >/dev/null`
EOF
 }
cmd; echo "var \"line\" is ${line}..."
]$ sh pingtest.sh
var "line" is 1.21...
 
Old 01-28-2003, 11:47 PM   #11
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 48
Very cool! I now find my solution gross. . . =-}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash, using variable in sed fur Programming 3 11-12-2005 08:41 AM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 05:38 PM
Variable with bash pinkysioux Programming 6 04-15-2004 09:46 PM
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 05:09 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM


All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration