Check if a variable is zero


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a variable is zero
# 1  
Old 12-15-2010
Check if a variable is zero

I have seen many posts for this sort of problem but I just did not know how to use it for my issue. A number is assigned to a variable and I wanted to check if it is a zero or non zero.

Example of my numbers are below:
000000000000000000000000000000000000000000000000
574E47303030303030303030303030303030303135313937

So "000000000000000000000000000000000000000000000000" is considered as zero and the other as non zero right, but how would I do that using IF statement?

The following is my script

Quote:
function getCorrelID {

cd /home/mqm/ma01
CORRELID=`./q -i$QUEUENAME -L1 -ddf2 2>&1 | sed -n '/Correl/{n;p;}' | sed 's/^[ \t]*//'`
echo $CORRELID
}

while read LINE
do
QUEUENAME=`echo $LINE | cut -d "," -f1`
echo $QUEUENAME
getCorrelID
if [ $CORRELID -eq "0" ]; then
echo "Null"
else
echo "Not Null"
fi
done<QueueList
The following is my output saying that the number "574E47303030303030303030303030303030303135313937" is not valid for the command.

Quote:
Output:

000000000000000000000000000000000000000000000000
Null
000000000000000000000000000000000000000000000000
Null
000000000000000000000000000000000000000000000000
Null
574E47303030303030303030303030303030303135313937
./correlid.sh[13]: 574E47303030303030303030303030303030303135313937: The specified number is not valid for this command.
Not Null
Please let me know if there are any suggestions on this.
Thank you!!
# 2  
Old 12-15-2010
Code:
$ [ '000000' -eq 0 ] && echo zero || echo NONzero
zero

$ [ '100000' -eq 0 ] && echo zero || echo NONzero
NONzero

# 3  
Old 12-15-2010
Those numbers, if they were treated as number or hex numbers overflow the limits of POSIX shell arithmetic. However strings are different.
Code:
#!/bin/ksh
iszero()   # print 1 if zero,  0 else
{
     ok=$(echo "$1" | tr -d '0')
     if [ -z "$ok" ] ; then
        print 1
     else
        print 0
     fi
}
a=574E47303030303030303030303030303030303135313937
b=000000000000000000000000000000000000000000000000
echo "$a  result: $(iszero $a)"
echo "$b  result: $(iszero $b)"

# 4  
Old 12-15-2010
Check if a variable is zero

Quote:
Originally Posted by vgersh99
Code:
$ [ '000000' -eq 0 ] && echo zero || echo NONzero
zero

$ [ '100000' -eq 0 ] && echo zero || echo NONzero
NONzero

I have very random numbers like "574E47303030303030303030303030303030303135313937", it is not the same number all the time. It varies. These are the correlation Id's of the MQ messages. So I cant really Hard code the numbers. and I i use the code above I am still getting the same error.
# 5  
Old 12-15-2010
Quote:
Originally Posted by soujiv
I have very random numbers like "574E47303030303030303030303030303030303135313937", it is not the same number all the time. It varies. These are the correlation Id's of the MQ messages. So I cant really Hard code the numbers. and I i use the code above I am still getting the same error.
This was meant as a hint and not as a complete solution.
Follow Jim's suggestion as it deals with integer overflow.
# 6  
Old 12-15-2010
Thank you!!
# 7  
Old 12-15-2010
The problem is you are testing with -eq which denotes a number.

This works in ksh.
Code:
#!/bin/ksh
 
func1()
{
   var="574E47303030303030303030303030303030303135313937"
   echo "$var"
}
 
 
ret=$(func1)
if [ "$ret" != 0 ]           ## change your test to != instead of -eq
then
        echo "Null"
else
        echo "Not Null"
fi
return 0


Last edited by radoulov; 12-15-2010 at 05:48 PM.. Reason: Code tags, please!
This User Gave Thanks to BeefStu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check variable

Hi people, I would like to start a review of my config variable to check whether they have been changed and if not then there is only an echo. If they have been changed to my other commands are executed. I hope you can help me. with best regards JPad edit: here my code if ;... (8 Replies)
Discussion started by: JPad
8 Replies

2. Shell Programming and Scripting

How to check whether a variable is empty or contains some value?

hi, i want to check whether a a variable contains some value or is empty in a shell script. so if the variable contains some value i want to do some job and if the variable doesnt contain any value then i need to skip that job. here is a sample script read_filenames.sh contains ... (5 Replies)
Discussion started by: Little
5 Replies

3. Shell Programming and Scripting

Check if a variable is having value

I have a script /root/asas with following contents. #!/bin/bash ha=`cat /etc/passwd | grep sandra` if ; then echo "Sandra is in /etc/passwd" echo "variable ha is $ha" else echo "Sandra is NOT in /etc/passwd" echo "variable ha is $ha" fi What... (3 Replies)
Discussion started by: anil510
3 Replies

4. Shell Programming and Scripting

Check the value of a variable

#!/bin/sh echo "Running Script to capture ORACLE Erros" # Change Directory to the actual logs path cd /home/nv8510/lognew err_var=`grep -in "ORA-" *` if then echo "THESE ARE THE ORACLE ERROR OCCURED" echo "$err_var" echo... (7 Replies)
Discussion started by: neeraj617
7 Replies

5. UNIX for Dummies Questions & Answers

Check the value of a string variable

hi to all, i want to check the value of a variable that it contains characters. for example i try the following: if then ......... i just want to check that in the specific line that is a variable called "passline" has the entry "password". But it can contain also other characters in the... (4 Replies)
Discussion started by: omonoiatis9
4 Replies

6. Shell Programming and Scripting

check if a variable is not affected

Hi all, to check that a variable is not empty, I usually do: if ; then... or if ; then... what if I have a serie of variables and want to do the same test on all of them. How to do this in a single if statement? thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

7. Shell Programming and Scripting

How to check if a variable contains a .

Hi I am writing a bash script and would like to check is a variable contains a . or not ex. a=102 output ok a=1.02 output not ok Many thanks, (3 Replies)
Discussion started by: gekkos
3 Replies

8. Shell Programming and Scripting

How to check parameter variable?

Hello All, I have a script that will email out if the email address is specified as parameter 1. I am using ksh, and then tried the following : email=$1 Following did not work, I am getting error test -z $email test ${email:=" ") -eq " " test -n $email test ${?email} What... (4 Replies)
Discussion started by: negixx
4 Replies

9. Shell Programming and Scripting

check for NULL variable

Hello I want to check for NULL variable.. but this is not working..please help thanks in advance esham (2 Replies)
Discussion started by: esham
2 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question