Register
It is currently Fri Nov 28, 2014 10:55 am

No Fun with Operators


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Nov 16, 2010 1:46 pm   

Joined: Thu Nov 11, 2010 8:58 am
Posts: 4
I have tried every variaton of this excpect the one that works. My IF/THEN statment in the code below works just fine, I need to change the comparison to 'EQUAL TO OR GREATER THAN'.


Code:
array=( $( /usr/bin/sar -q 1 30 |grep Average |awk '{print $2,$3}' ) )
nthreads="${array[0]}"
avproc="${array[1]}"
if [ $nthreads = 1.0 ] && [ $avproc > 5 ]; then
...


-gt never works. I thought >= would work but that errors with unary operator expected.
I even tried putting both in to $(( expression )) format. Still no dice.

I am lost. :((

Thanks


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

Top
 Profile  
 PostPosted: Tue Nov 16, 2010 2:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
Hi,

arithmetic evaluation should be done using (( )); check bash manpage /ARITHMETIC EVALUATION
know that bash doesn't deal at all with floating numbers, you'll have to use bc
Code:
nthreads=1 avproc=12
if (( $(bc <<<"$nthreads == 1.0") && ( avproc > 5 ) ))
then echo OK
else echo KO
fi


also, you could feed your array a simpler way:
Code:
array=( $( /usr/bin/sar -q 1 30 | awk '/Average/ {print $2,$3}' ) )

: no need to use grep with sed or awk


plus, you probably now have a file named 5
because > inside `test' (`[ ]') still means redirection!


Top
 Profile  
 PostPosted: Tue Nov 16, 2010 3:07 pm   

Joined: Thu Nov 11, 2010 8:58 am
Posts: 4
Actually,

My code sample above DOES work.

I am trying to change the = and > to EQUAL TO OR GREATER THAN. I am not sure if bc will help or not...


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 8 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
© 2003 - 2011 USA LINUX USERS GROUP