Register
It is currently Sat Apr 19, 2014 10:33 pm

Pull 2 variables out of one command output?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Nov 11, 2010 9:13 am   

Joined: Thu Nov 11, 2010 8:58 am
Posts: 4
=; Hi,

I am editing an existing script which reads the output from the sar command. Right now the script just pulls one field from the output table.

Code:
queue=`/usr/bin/sar -q 1 5 |grep Average |awk '{print $3}'`
if [ $queue -ge 100 ]; then ....


The output of the sar command is this.

15:00:42 runq-sz %runocc swpq-sz %swpocc
15:00:43 2.0 1 0.0 0
15:00:44 0.0 0 0.0 0
15:00:45 0.0 0 0.0 0

Average 2.0 1 0.0 0

I need to pull another field out of here (eg. print $2) and get it into a variable for some further logic.

I cant do:
Code:
var1`/usr/bin/sar -q 1 5 |grep Average |awk '{print $3}'
var2=`/usr/bin/sar -q 1 5 |grep Average |awk '{print $2}'

Because that would product two different outputs. I know I can write this to a file and do it, but it has been suggested that this can be done more cleanly without having to write to a file.

This is only my second time coding in BASH, which I think I am going to love. Help appreciated.. Thanks


Top
 Profile  
 PostPosted: Thu Nov 11, 2010 12:30 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You can use
Code:
array=( $( /usr/bin/sar -q 1 5 |grep Average |awk '{print $2,$3}' ) )

Now, your two fields are stored in ${array[0]} and ${array[1]}.


Last edited by choroba on Tue Nov 16, 2010 3:40 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Tue Nov 16, 2010 1:28 pm   

Joined: Thu Nov 11, 2010 8:58 am
Posts: 4
Thanks for the reply, that did the trick.


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