expr: non-integer argument while doing Arithmetic in Shell Script [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-12 04:55:16

问题


When i'm trying to execute following line of code

#!/bin/sh
a=20
b=10
sum=`expr $a + $b` #(i thought i did wrong with ` so i put (') also to check but its becomes literals which gets printed as usual)
echo $sum

i'm getting the error

expr: non-integer argument

Can someone tell me where it went wrong in this code because almost on every tutorial same thing was mentioned to do Arithmetic in Shell Script.

Edit: for those whose working i'm using CYGWIN terminal FYI. IS there any difference ?

Edit 2 : As mentioned in the comment by @ghoti Windows file ends with /r/n while unix ends with /n .. Since i'm writing my Script in windows platform while executing in Unix Platform , So when i'm removing back-tick its giving me error

$'20\r': command not found

so definitely $a is changed into 20 but \r is resulting into error. So any ideas how to Short out this error ?


回答1:


Your original code should work correctly. Even though, to make sure you don't have any issue with quotes, you can slightly modify it like that:

#!/bin/sh
a=20
b=10
sum=$(expr $a + $b)
echo $sum



回答2:


I had similar error. I did the following and issue resolved:

Go to Edit in notepad++, then selected EOL Conversion, then selected UNIX/OSX Format



来源:https://stackoverflow.com/questions/28965805/expr-non-integer-argument-while-doing-arithmetic-in-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!