Comparing Negative #'s in IF Statements


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing Negative #'s in IF Statements
# 1  
Old 10-17-2008
Comparing Negative #'s in IF Statements

Hi,

I am writing a BASH shell script. I would like to construct an IF statement that allows me to determine if a variable is between -180 and 180. Example:

if [ $WEST -lt -180.0 -o $WEST -gt 180.0 ]; then
echo 'WEST NOT WITHIN BOUNDS'
fi

However, I believe the negative sign is causing errors. What is the proper BASH syntax to make this tyoe of comparison? Thanks a lot.

Mike
# 2  
Old 10-17-2008
Hammer & Screwdriver Check on your brackets

See the following:
(I set coord1 and coord2 as variables earlier.)

Code:
> valuex=13
> echo $valuex $coord1 $coord2 ;if [ $valuex -lt $coord1 ] || [ $valuex -gt $coord2 ]; then echo "*** out of bounds ***"; fi
13 -180 180

> valuex=-181
> echo $valuex $coord1 $coord2 ;if [ $valuex -lt $coord1 ] || [ $valuex -gt $coord2 ]; then echo "*** out of bounds ***"; fi
-181 -180 180
*** out of bounds ***

> valuex=181
> echo $valuex $coord1 $coord2 ;if [ $valuex -lt $coord1 ] || [ $valuex -gt $coord2 ]; then echo "*** out of bounds ***"; fi
181 -180 180
*** out of bounds ***
>

# 3  
Old 10-17-2008
Hi,

Please correct me if I am wrong, but it seems that your solution is too simply define the bounds first, and then use those variables in the IF statement. I got that to work for me. But it doesn't seem to accept the comparison of decimals. Is that true on your end?

Also is that "check your brackets" directed at me?

Thanks again.
# 4  
Old 10-17-2008
Hammer & Screwdriver

I did not check for decimals. I think the decimal will cause a problem since the -gt or -lt is expecting integers.

I only worked on you comment about negatives.

My comment about brackets was in the writing of the or statement.
You had [ this -o that ]
I had [ this ] || [that ]
# 5  
Old 10-17-2008
Ah, yes I have seen both ways of doing an OR statement. Any ideas on how I can compare negative integers AND decimals?

Thanks
Mike
# 6  
Old 10-17-2008
Hammer & Screwdriver This can be cleaned up some

Because of the issue where unix by default only wants to do integer math, the following works for decimals greater than 0. Need to think about logic to fix when a negative number -- must subtract one. (Can be done, just tired this afternoon!)

Essentially, was experimenting with using cut to break up the value into pieces.

Maybe this can get you thinking about an approach to the matter.


Code:
#! /usr/bin/bash

value1=182.1
pre=`echo $value1 | cut -d"." -f1`
dec=`echo $value1 | cut -d"." -f2`
echo $pre $dec

if [ `echo $value1 | cut -d"." -f2` -eq 0 ]
   then
     value2=`echo $value1 | cut -d"." -f1`
   else
     value2=`echo $value1 | cut -d"." -f1`
     value3=`echo $value2 + 1 | bc`
fi

if [ $value3 -lt -180 ] || [ $value3 -gt 180 ]
   then
   echo "Value is out of bounds"
fi

# 7  
Old 10-17-2008
You have to use awk or perl or python... etc.
Code:
result=$(echo "$WEST" | awk ' ($1> -180.0 && $1 < 180.0)?$0="1":$0="0"')
if [ $result -eq 1 ]; then
   # success
else
   # failure
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sum the value with negative values?

Hi Gurus, I have requirement need to sum the value, the logic is if the value is negative then time -1, I tried below two ways. one is failed, another one doesn't work. awk -F"," '{if($8< 0 $8*-1 else $8) sum+=$8}{print sum, $8} END{printf("%.2f\n",sum)}' awk -F","... (4 Replies)
Discussion started by: ken6503
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Bits in Negative

Hi All, I have received a notification that I have posted a question double times. But I have not done all this intentionally. I have just joined this site and was not aware of the rules. Also I have my bits in negative. what does that mean. Thanks (1 Reply)
Discussion started by: Palak Sharma
1 Replies

3. Shell Programming and Scripting

Negative testing in unix

how can we do negative testing in unix ? (2 Replies)
Discussion started by: eshalife
2 Replies

4. Shell Programming and Scripting

change value to negative

How to change value in column 5 to negative based on value in column 2 ? Example: For all records with A in column 2 change value in column 5 to negative. file Code: 1234~A~b~c~10~e~f~g~h~09/10/09 1234~A~b~c~75~e~f~g~h~11/12/10 1234~A~b~c~40~e~f~g~h~12/06/10 5678~B~b~c~2~e~f~g~h~01/11/11... (4 Replies)
Discussion started by: sigh2010
4 Replies

5. Shell Programming and Scripting

Comparing Negative Numbers with If/Else

ValA=-29344 if ; then echo "NEGATIVE" else echo "POSITIVE" fi Can someone please tell me how else they would go about doing the above? When i do it, i get errors such as: (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

report negative value from file

Hello, I need help to write a script to do the following: 1) read the following file vol_check.out 2) report any negative value with host and volume name vol_check.out file contents: ---------------------------------- prod_filer1 ---------------------------------... (2 Replies)
Discussion started by: za_7565
2 Replies

7. UNIX for Dummies Questions & Answers

Negative Numbers and If Statements

Hi, Can anyone explain what is going on here: michael-browns-powerbook-g4-15:~ msb65$ start=-1 michael-browns-powerbook-g4-15:~ msb65$ stop=1 michael-browns-powerbook-g4-15:~ msb65$ if ; then echo hello; fi -bash: ; then echo hello; fi hello Are you not allowed to compare negative... (6 Replies)
Discussion started by: msb65
6 Replies

8. Programming

Negative Offset

Function: int fcntl(int fd, int cmd, struct flock * lock) Data Type: struct flock This structure is used with the fcntl function to describe a file lock. It has these members: off_t l_start This specifies the offset of the start of the region to which the lock applies, and... (1 Reply)
Discussion started by: DNAx86
1 Replies

9. UNIX for Dummies Questions & Answers

negative permissions

Hi. I want to know how can I negate a write permission for a file to an expecific user when that user have that permission becouse he belongs to a group what have a write permission for the file. (4 Replies)
Discussion started by: sickoboy
4 Replies
Login or Register to Ask a Question