Register
It is currently Thu Dec 18, 2014 6:41 am

modify string in bash


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Oct 19, 2010 4:08 pm   

Joined: Tue Oct 19, 2010 3:56 pm
Posts: 1
I have to write down a script that execute this command
Code:
time ./my_program


and insert only the value near
Code:
user
in a variable.

I will explain more exactly it:
when I execute the command "time ./my_program" the shell print this answer:

Code:
Random number from 1 to 100: 1078793234

real   0m0.095s
user   0m0.000s
sys   0m0.008s


In the first line there is the result making by my_program
and then there are 3 lines reporting the time that the OS spends to execute it.
My problem is :

I have to use only the value reported near "user".
I tried to do it in this way:
Code:
var=$(time ./my_program)
var2=$(cut -f 1,2 -d'r' ${var})
echo "ciao ${var2}"

but it doesn't work!
Can you help me please?

Thank you very much


Top
 Profile  
 PostPosted: Wed Oct 20, 2010 4:51 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Check the man page for the time command, it should indicate a way to capture the timing output, then consider using grep or awk to extract the correct data from the output.


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 7:00 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
The time keyword has some magical powers. It's not really a command since it can works on multiple pipes.

time ( « help time » ) != /usr/bin/timeman time » )

Code:
TIMEFORMAT=%3lU var=$({ time ./my_program > /dev/null 2>&1; } 2>&1)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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:  


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