Problem with Greater Than Or Equal To | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Problem with Greater Than Or Equal To

Shell Programming and Scripting


Tags
bash, operators

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-17-2010
diex diex is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 November 2010, 3:28 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Problem with Greater Than Or Equal To

BASH problem with IS GREATER THAN OR EQUAL TO.

I have tried a dozen variations for this IF statement to work with IS GREATER THAN OR EQUAL TO. My code below WORKS.


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 
... i will work fine!!

I can not change : if [ $nthreads = 1.0 ] && [ $avproc > 5 ];
to work like if [ $nthreads >= 1 ] && [ $avproc >= 5 ];

I believe part of the problem is the decimal number. But I have tried -gt. I have tried using (( )) expressions. I have attempted to use bc. Many times I get the error 'unary operator expected'.

I have been looking for a few days now and have tried everything I can think of. It seems I have a fundamental problem with how BASH handles variable and data types.

Help appreicated. Thanks
Sponsored Links
    #2  
Old 11-17-2010
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 1 October 2014, 9:23 PM EDT
Location: Foxborough, MA
Posts: 7,657
Thanks: 150
Thanked 582 Times in 547 Posts
assuming integer arithmetic....

Code:
 if [ $nthreads -ge 1 -a $avproc -ge 5 ]; then

Sponsored Links
    #3  
Old 11-17-2010
diex diex is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 November 2010, 3:28 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by vgersh99 View Post
assuming integer arithmetic....

Code:
 if [ $nthreads -ge 1 -a $avproc -ge 5 ]; then

Thanks for reply.

I think that would have worked if my variable was not a decimal. I think thats part of the problem.

Error:
line 5: [: 0.0: integer expression expected
    #4  
Old 11-17-2010
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 1 October 2014, 9:23 PM EDT
Location: Foxborough, MA
Posts: 7,657
Thanks: 150
Thanked 582 Times in 547 Posts

Code:
function myGE
{
  echo "if (${1} >= ${2}) 1" | bc
}



 if [ "$(myGE "${nthreads}" 1)" -eq 1 -a "$(myGE "$avproc" 5)" -eq 1 ]; then

Sponsored Links
    #5  
Old 11-17-2010
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 1 October 2014, 12:25 PM EDT
Location: /dev/ph
Posts: 4,812
Thanks: 63
Thanked 425 Times in 394 Posts
Bash cannot directly handle decimal numbers like 4.5 because it does not have built-in support for floating point.
Sponsored Links
    #6  
Old 11-18-2010
diex diex is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 November 2010, 3:28 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fpmurphy View Post
Bash cannot directly handle decimal numbers like 4.5 because it does not have built-in support for floating point.
That said.

Can you convert 1.0 in to 1 without using a function? Strip off the decimal?

$x = 2.0
.... =2


Thanks
Sponsored Links
    #7  
Old 11-21-2010
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 October 2014, 3:01 PM EDT
Location: Amsterdam
Posts: 9,488
Thanks: 280
Thanked 2,398 Times in 2,150 Posts
Try:

Code:
echo ${x%%.*}


Code:
if [ ${nthreads%%.*} -eq 1 ] && [ $avproc -gt 5 ]; then


Last edited by Scrutinizer; 11-21-2010 at 05:18 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with awk,not able print the file that is greater than 3000 bytes. shrima.pratima Homework & Coursework Questions 1 09-02-2010 02:13 AM
Trying to find files equal to and greater than bbbngowc Shell Programming and Scripting 4 07-06-2010 11:34 AM
while [ $x -ge 50 ] + and equal to zero ; then losh Shell Programming and Scripting 1 06-10-2010 06:11 AM
Problem creating files greater than 2GB bobbyjohnz UNIX for Advanced & Expert Users 1 01-17-2007 11:19 AM



All times are GMT -4. The time now is 10:22 PM.