Non-integer argument in expr

i wrote this simple shell script

#!/bin/bash
read  N1
read  N2
expr $N1 + $N2

it work fine in bash and i add it on xinetd for some test but when i try to use in with telnet i got this error :

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
12
23
expr: non-integer argument
Connection closed by foreign host.

please help me to solve this non-integer error

What is N1 and N2 ?

If any of those is IP address, then it won't work of course.
Integer - Wikipedia, the free encyclopedia

How did you execute this? Arguments are 12 and 23 I guess, seems fine.

Try this, its all the same though

#!/bin/bash 
read  N1; read  N2
((sum=N1+N2))
echo $sum

regards,
Ahamed

thanks but i got this with this script

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
12 
23
")syntax error: invalid arithmetic operator (error token is "

Connection closed by foreign host.

both of this scripts work on my machine fine but when i want to use in on network with telnet they wont work , is there any config for telnet or is telnet change the argument to something else from integer ?

try including this

#!/bin/bash
typeset -i N1
typeset -i N2
read  N1; read  N2
 ((sum=N1+N2)) 
echo $sum

regards,
Ahamed

still doesn't work

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
12
")syntax error: invalid arithmetic operator (error token is "
0
Connection closed by foreign host.

can you tell me how you are executing this? where have you included the arguments? try echo-ing N1 and N2 and see they have the desired values.

regards,
Ahamed

i try this as you said

#!/bin/bash
read  N1; read  N2
typeset -i N1
typeset -i N2
echo $N1
echo $N2
((sum=N1+N2)) 
echo $sum

and i got this

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
12
23
12
23
")syntax error: invalid arithmetic operator (error token is "

Connection closed by foreign host.

i put this script under xinetd as a service and i execute it whit telnet

the machine where you are trying to telnet, does this script run on that machine?

#!/bin/bash
set -x
#and the rest of the code

i try telnet on the same machine with script and another machine but there is same results

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
+ read N1
1
+ read N2
12
+ typeset -i N1
+ typeset -i N2
+ echo $'1\r'
1
+ echo $'12\r'
12
+ (( sum=N1+N2 ))
")syntax error: invalid arithmetic operator (error token is "
+ echo

Connection closed by foreign host.

---------- Post updated at 03:44 AM ---------- Previous update was at 03:44 AM ----------

i try telnet on the same machine with script and another machine but there is same results

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
+ read N1
1
+ read N2
12
+ typeset -i N1
+ typeset -i N2
+ echo $'1\r'
1
+ echo $'12\r'
12
+ (( sum=N1+N2 ))
")syntax error: invalid arithmetic operator (error token is "
+ echo

Connection closed by foreign host.

---------- Post updated at 03:48 AM ---------- Previous update was at 03:44 AM ----------

is ( \r ) make thing wrong ?

There is a \r and that's the issue I guess
Try this now, should work

#!/bin/bash
set -x
read  N1; read  N2
typeset -i N1
typeset -i N2
n1=$( echo $N1 | tr -s "\r" )
n2=$( echo $N2 | tr -s "\r" )
echo $n1" "$n2
((sum=n1+n2)) 
echo $sum

same error

ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
+ read N1
12
+ read N2
13
+ typeset -i N1
+ typeset -i N2
++ echo $'12\r'
++ tr -s '\r'
+ n1=$'12\r'
++ echo $'13\r'
++ tr -s '\r'
+ n2=$'13\r'
'13cho '12
 13
+ (( sum=n1+n2 ))
")syntax error: invalid arithmetic operator (error token is "
+ echo

Connection closed by foreign host.

try different combinations

n1=$( echo $N1 | tr -d '\r' )
n2=$( echo $N2 | tr -d '\r' )
#or
n1=$( echo $N1 | sed 's/\r//g' )
n2=$( echo $N2 | sed 's/\r//g' )
n1=$( echo $N1 | tr -d '\r' )
n2=$( echo $N2 | tr -d '\r' )

worked , thanks a lot , and one more things , what is this error ?

Escape character is '^]'.
ehsan@debian:~$ telnet 192.168.1.4 1234
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.
+ read N1
12
+ read N2
45
+ typeset -i N1
+ typeset -i N2
++ echo $'12\r'
++ tr -d '\r'
+ n1=12
++ echo $'45\r'
++ tr -d '\r'
+ n2=45
+ echo '12 45'
12 45
+ (( sum=n1+n2 ))
+ echo 57
57
Connection closed by foreign host.

That is not an error, it is a message from from terminal or telnet indicating the esc seq. Not very sure.

regards,
Ahamed

1 Like