Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Problem with Greater Than Or Equal To

Shell Programming and Scripting


Tags
bash, operators

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 11-17-2010
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 6 July 2015, 2:36 AM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 Posts
assuming integer arithmetic....

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

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 11-17-2010
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 6 July 2015, 2:36 AM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 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 Unix and Linux 11-17-2010
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 3 July 2015, 10:36 PM EDT
Location: /dev/ph
Posts: 4,938
Thanks: 70
Thanked 460 Times in 426 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 Unix and Linux 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 Unix and Linux 11-21-2010
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 5 July 2015, 11:42 PM EDT
Location: Amsterdam
Posts: 10,309
Thanks: 346
Thanked 2,725 Times in 2,437 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:06 AM.