expr:在Shell脚本中执行算术运算时的非整数参数 [英] expr: non-integer argument while doing Arithmetic in Shell Script

查看:2382
本文介绍了expr:在Shell脚本中执行算术运算时的非整数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行以下代码行

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

我遇到错误

expr:非整数参数

expr: non-integer argument

有人可以告诉我这段代码哪里出错了,因为在每个教程中几乎都提到了在Shell Script中做算术的相同事情.

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.

对于那些正在使用CYGWIN终端FYI工作的用户.有什么区别吗?

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

如@ghoti的注释中所述,Windows文件以/r/n结尾,而Unix以/n结尾. m删除反勾号,让我出错

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':找不到命令

$'20\r': command not found

因此肯定$ a会更改为20,但 \ r 会导致错误.那么有什么想法可以缩短此错误?

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

推荐答案

我有类似的错误. 我做了以下并解决了问题:

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

转到记事本++中的编辑",然后选择"EOL转换",然后选择"UNIX/OSX格式"

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

这篇关于expr:在Shell脚本中执行算术运算时的非整数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆