Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Signed numbers

Status
Not open for further replies.

ojosVerdes

Technical User
Oct 10, 2002
50
US
How do you add signed number. for example when I do this I get an error;

expr +5 + +5 or expr 5 + +5

but I don't get an error when I do this:
expr -5 - -5

why?

Thanks
 
Hi:

I tried adding signed numbers with expr under Solaris 7 ksh and Linux 7.3 bash, and if fails - just as it failed for you. Why? must be a limitation of expr. It's probably that way on most any unix flavor.

However, if you use the ksh arithmetic expressions to add numbers, it works:

#!/bin/ksh

x=$((+10 + 5))
echo $x

Go figure.

Ed
 
How would you do if you are not using korn Shell?

I want to be able to add signed numbers. how do I remove the + sing off the mumber so I don't get an error number?

Thanks

Frank
 

prepend '-1+1' to the '+' sign
use the faster 'bc'
echo "-1+1+5 -1+1+ -1+1+5"|bc -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks Jamisar,

Question for you.
How do I translate this to unix?
If first character of a variable is equal to + then cut the first character, and store the the result into a varible.

ex:

echo +6 |cut -c2
yields 6

how do I check to see if the first character in a variable is a + sign?

Thanks
 
echo $string|sed -e 's/+//g' -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
#!/bin/ksh

a="+6"
#a="6293874"
#a="abcd"

if [[ $(expr "${a}" : '^[+][0-9]') -ne 0 ]] ; then
echo "[${a}] signed"
else
echo "[${a}] NOT signed"
fi;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad,
Is there any reason why this does not work in Borne Shell?

Frank
 
for Bourne, use:

if [[ `expr &quot;${a}&quot; : '^[+][0-9]'` -ne 0 ]] ; then vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks vlad. U R a genius.

Do U know any good book that can teach me about regular expresions?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top