470,614 Members | 1,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

test with "$integer"

why is it that when i used the value with zero,comparing two integers,it seems that i got the wrong ouput?

Please consider the ff:

#!/bin/ksh
echo "63918000009" > k
cat k
read x < k
(( aj=63918000009 ))
if
[ ! -d "$x" = "$aj" ]
then
echo "$aj"
elif
[ "$k" = "$aa" ]
then
echo "$aa"
else
echo "done"
fi
exit 99
--------------
OUTPUT:
63918000009
-506509431

********but if:***** consider the 000009 that i removed from the first script.

#!/bin/ksh
echo "63918" > k
cat k
read x < k
(( aj=63918 ))
(( aa=12346 ))
if
[ ! -d "$x" = "$aj" ]
then
echo "$aj"
elif
[ "$k" = "$aa" ]
then
echo "$aa"
else
echo "done"
fi
exit 99
-----------
OUPUT:
63918
63918

Thanks..
Jul 28 '07 #1
9 2525
Motoma
3,237 Expert 2GB
In the first $k and $aa are both unitialized. In the second, only $k is.
Aug 1 '07 #2
In the first $k and $aa are both unitialized. In the second, only $k is.

Hi Sir,


Don't Know exactly what you mean.but somehow i just figured it out. i'l give it a try.thanks..
Aug 3 '07 #3
This his how my question is about:

#!/bin/ksh
echo "1234567890" > k
cat k
read x < k
(( aj=1234567890 ))

if
[ ! -d "$x" = "$aj" ]
then
echo "$aj"

else
echo "done"
fi
exit 99
-----------
OUPUT:
1234567890


but when i added any number to my input "12345678901" which becomes 11 numbers it seems that the output doesn't print for any input of more than 10 numbers.

---------------
OUTPUT:
done
Aug 4 '07 #4
Motoma
3,237 Expert 2GB
I am not entirely familiar with ksh scripting, but when I try and load up the last script you posted, I got an error on the line that reads:

[ ! -d "$x" = "$aj" ]

What are you trying to test for with this line?
Aug 6 '07 #5
I am not entirely familiar with ksh scripting, but when I try and load up the last script you posted, I got an error on the line that reads:

[ ! -d "$x" = "$aj" ]

What are you trying to test for with this line?

Maybe you can remove the "! -d" from the syntax.

[ "$x" = "$aj" ]

--------------------------------------------------
I guess I'm having problem with zero (0).

sometimes when I have to compare it with the value 43918706 ,it doesn't give me true value. what i need to do is to cut 439187 and then cut 6 and then paste the result so i have the value 4391876, which doesn't have any zero.

Please consider the following. let say you have to input your value.


echo "input: \c" ----> my input is 4391876
read a
let x=4391876

if
[ "$a" = "$x" ]
then
echo "$x"
else
echo "wrong"
fi

OUTPUT
-------------
4391876

CASE #2:

echo "input: \c" ----> my input is 43918706
read a
let x=43918706

if
[ "$a" = "$x" ]
then
echo "$x"
else
echo "wrong"
fi

OUTPUT
-------------
wrong
Aug 6 '07 #6
Motoma
3,237 Expert 2GB
if you are sure that your input is going to be numbers, just use the -eq comparison.

[ $x -eq $aj ]
Aug 6 '07 #7
if you are sure that your input is going to be numbers, just use the -eq comparison.

[ $x -eq $aj ]
#!/bin/ksh
echo "input: \c"
read a
let x=6391800000706

if
[ "$a" -eq "$x" ]
then
echo "$x"
else
echo "wrong"
fi

*Try to use input ==>6391800000706
OUTPUT:
----------------
888664258
Aug 7 '07 #8
Motoma
3,237 Expert 2GB
Load up this script I wrote. Hopefully you will see where the error is occurring.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/ksh
  2. echo "input: \c"
  3. read a
  4. let x="6391800000706"
  5.  
  6. echo "a = $a; x = $x"
  7.  
  8. if
  9. [ $a -eq $x ]
  10. then
  11. echo "Shell script comparison passed."
  12. else
  13. echo "Shell script comparison failed."
  14. fi
  15.  
  16. if
  17. [ $(echo "$a == $x" | bc ) -eq 1 ]
  18. then
  19. echo "bc variable comparison passed."
  20. else
  21. echo "bc variable comparison failed."
  22. fi
  23.  
  24. if
  25. [ $(echo "$a == 6391800000706" | bc ) -eq 1 ]
  26. then
  27. echo "bc numerical comparison passed."
  28. else
  29. echo "bc numerical comparison failed."
  30. fi
  31.  
  32.  
Aug 7 '07 #9
Load up this script I wrote. Hopefully you will see where the error is occurring.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/ksh
  2. echo "input: \c"
  3. read a
  4. let x="6391800000706"
  5.  
  6. echo "a = $a; x = $x"
  7.  
  8. if
  9. [ $a -eq $x ]
  10. then
  11. echo "Shell script comparison passed."
  12. else
  13. echo "Shell script comparison failed."
  14. fi
  15.  
  16. if
  17. [ $(echo "$a == $x" | bc ) -eq 1 ]
  18. then
  19. echo "bc variable comparison passed."
  20. else
  21. echo "bc variable comparison failed."
  22. fi
  23.  
  24. if
  25. [ $(echo "$a == 6391800000706" | bc ) -eq 1 ]
  26. then
  27. echo "bc numerical comparison passed."
  28. else
  29. echo "bc numerical comparison failed."
  30. fi
  31.  
  32.  
---------------------------------------
Zero "0" is very sensitive in unix, so I thought about this while doing my script.:


echo "input: \c"
read a

if
[ $a = ' 6391800000706' ]
then
echo "6391800000706"
else
echo "wrong"
fi
Aug 10 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Vijay | last post: by
2 posts views Thread by Peter Ignarson | last post: by
7 posts views Thread by moonman | last post: by
20 posts views Thread by chutsu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.