bash expr 命令执行期间的错误消息:expr: non-integer argument
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20231362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error message during the expr command execution: expr: non-integer argument
提问by JavaRed
I try to assign two numbers (actually these are the outputs of some remote executed command) to 2 different variables, let say A and B.
我尝试将两个数字(实际上这些是某些远程执行命令的输出)分配给 2 个不同的变量,比如说 A 和 B。
When I echo A and B, they show the values:
当我回显 A 和 B 时,它们显示值:
echo $A
809189640755
echo $B
1662145726
sum=`expr $A + expr $B`
expr: non-integer argument
I also tried with typeset -i but didn't work. As much as I see, bash doesn't take my variables as integer. What is the easiest way to convert my variable into integer so I can add, subtract, multiply etc. them?
我也试过 typeset -i 但没有用。据我所知,bash 不会将我的变量视为整数。将我的变量转换为整数以便我可以对它们进行加、减、乘等的最简单方法是什么?
Thanks.
谢谢。
回答by startswithaj
You should be able to do
你应该能够做到
expr $A + $B
or
或者
$(( $A + $B ))
回答by Meligordman
First, you should not use expr twice. So
首先,您不应该两次使用 expr。所以
sum=`expr $A + $B`
should work. Another possibility is using pipeline
应该管用。另一种可能性是使用管道
sum=`echo "$A + $B" | bc -l`
which should work fine even for multiplications. I am not sure how would it behave if you have too large numbers, but worked for me using your values.
即使对于乘法,它也应该可以正常工作。如果您的数字太大,我不确定它会如何表现,但是使用您的值对我有用。
回答by PasteBT
Try in linux bash:
在 linux bash 中尝试:
A=809189640755
B=1662145726
echo $((A + B))
回答by Soft Technoes
You have to copy and paste this code and run. I hope it will be helpful for you.
您必须复制并粘贴此代码并运行。我希望它会对你有所帮助。
echo "enter first number"
read num1
echo "enter second number"
read num2
echo $((num1 + num2))
Save your file as file_name.sh and run it from your terminal
将文件另存为 file_name.sh 并从终端运行它