Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Problem with Greater Than Or Equal To

Shell Programming and Scripting


Tags
bash, operators

Closed    
 
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: 2 December 2016, 12:57 PM EST
Location: Foxborough, MA
Posts: 7,988
Thanks: 194
Thanked 698 Times in 654 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: 2 December 2016, 12:57 PM EST
Location: Foxborough, MA
Posts: 7,988
Thanks: 194
Thanked 698 Times in 654 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: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 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: 2 December 2016, 2:51 AM EST
Location: Amsterdam
Posts: 11,096
Thanks: 440
Thanked 3,105 Times in 2,747 Posts
Try:

Code:
echo ${x%%.*}


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


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

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 03:13 AM
Trying to find files equal to and greater than bbbngowc Shell Programming and Scripting 4 07-06-2010 12:34 PM
while [ $x -ge 50 ] + and equal to zero ; then losh Shell Programming and Scripting 1 06-10-2010 07:11 AM
Problem creating files greater than 2GB bobbyjohnz UNIX for Advanced & Expert Users 1 01-17-2007 12:19 PM



All times are GMT -4. The time now is 06:15 PM.