It is currently Wed May 23, 2018 10:44 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
time ./my_program

and insert only the value near
in a variable.

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

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:
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

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

Joined: Thu Oct 11, 2007 7:12 am
Posts: 265
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.

 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 » )

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

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: Google [Bot] and 7 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:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group