Comparing Negative Numbers with If/Else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing Negative Numbers with If/Else
# 1  
Old 07-20-2011
Comparing Negative Numbers with If/Else

ValA=-29344

if [ "$ValA" -lt "0" ] ; 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:

Code:
[: 179: Illegal number: 
[: 179: Illegal number:


Last edited by SkySmart; 07-20-2011 at 01:56 PM..
# 2  
Old 07-20-2011
Code:
[ ValA -lt 0 ] && echo "negative" || echo "positive"

# 3  
Old 07-20-2011
Or using builtin
Code:
((  ValA < 0 ))  && echo "negative" || echo "positive"

# 4  
Old 07-20-2011
thanks guys. i tried the suggestions i've received in this thread so far, but none of them seem to work. Smilie
# 5  
Old 07-20-2011
What's your system? What's your shell? Knowing that will help find an answer that works for you, not just us.
# 6  
Old 07-20-2011
Quote:
Originally Posted by Corona688
What's your system? What's your shell? Knowing that will help find an answer that works for you, not just us.


Linux Redhat and Ubuntu

Shell i'm using = /bin/sh


Thanks
# 7  
Old 07-20-2011
sh is "generic" today. More safety is tell the shell from group ksh, bash, dash. Then you have maybe that shell.
Code:
#!/usr/bin/ksh # or bash or dash

But next will work in any posix shell and bourne shell
Code:
[ "$ValA" -lt 0 ] && echo "negative" || echo "positive"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting a file based on negative and positive numbers

I have a file that is pipe delimited and in Column F they have number values, both positive and negative. I need to take the one file I am starting with and split it into two separate files based on negative and positive numbers. What is the command to do so? And then I need to also transfer... (4 Replies)
Discussion started by: cckaiser15
4 Replies

2. UNIX for Dummies Questions & Answers

Sorting numerically considering both negative and positve numbers

Dear Experts, I have an IP file which looks like below ---- 100 200 5.02 100 200 -2.99 100 200 -3.01 200 300 2.05 200 300 3.01 200 300 -5.06 I want an OP which looks like (decreasing numerically)-- 100 200 5.02 100 200 -2.99 100 200 -3.01 200 300 3.01 200 300 2.05 200 300 -5.06 (2 Replies)
Discussion started by: Indra2011
2 Replies

3. UNIX for Dummies Questions & Answers

Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't... (5 Replies)
Discussion started by: Twinklefingers
5 Replies

4. Shell Programming and Scripting

Splitting a file based on positive and negative numbers

Dear All, I have to split a tab delimited file in two files based on the presence of a positive or negative in column number 9 , for example file: A 1 5 erg + 6766 0.9889 0.9817 9.01882 erg inside upstream B 1 8 erg2 + 6766 0.9889 0.9817 -9.22 erg2 inside... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

5. Shell Programming and Scripting

addition of both positive and negative numbers

Let, I have three numbers +00123.25 -00256.54 +00489.23 I need to sum up all those three numbers, after storing them in three variables (say var1, var2, var3). I used both expr and BC, but they didn't work for me. But, I am not able to sum up them, as I don't have any idea how to... (13 Replies)
Discussion started by: mady135
13 Replies

6. Shell Programming and Scripting

Perl output with negative and positive numbers

Hello, For my weather station I have made a little perl script to put the data into cacti. The next problem I have. I can only get positive numbers or negative numbers. What do I do: Though a shell scrip I call the perl script. Shell script: #!/bin/sh cat data.txt | stats.pl Perl... (4 Replies)
Discussion started by: rbl-blacklight
4 Replies

7. UNIX for Dummies Questions & Answers

Negative Numbers for input parameters.

Hello, I have a command that I need to supply a negative number as a parameter; how do I do this? I have tried giving it with double quotes, "", but no avail. Thanks, Gussi (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

8. UNIX for Dummies Questions & Answers

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 ; then echo 'WEST NOT WITHIN BOUNDS' fi However, I believe the negative sign is causing errors. What is the proper BASH... (6 Replies)
Discussion started by: msb65
6 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Comparing two numbers

Hello, I kinda newbie in unix so I would like so help.I know that there is a command that compares two integer numbers test (eg. #$1=0 ).I would like to know if it is possible to compare any number with another (eg. 2.3=0 or 3.7!=0 4.5>2.2). Thank you in advance. (1 Reply)
Discussion started by: TabloMaxos
1 Replies
Login or Register to Ask a Question