首页 > 系统服务 > 详细

【Linux】通过传入变量进行数学运算

时间:2018-06-26 16:43:37      阅读:278      评论:0      收藏:0      [点我收藏+]

一个简单的sum求和

#! /bin/bash

## For get the sum of tow numbers
## Writen by Qinys
## Date:2018-06-26

a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"


上述是一个简单的shell脚本求和

a与b分别是变量,上述脚本的意思是对a,b两个变量分别赋值,然后求和

数学计算要用[]括起来并且外面加一个“$

根据输入值进行求和

#! /bin/bash
## Input param to sum
## Writen by Qinys
## Date:2018-06-26

read -p "Please input a number:" x
read -p "Please input another number:" y
sum=$[$x+$y]
echo "The sum of the two numbers is:$sum"

使用read –p输入相应的参数,注意:在x前边是有引号的


shell中预设变量的使用

#! /bin/bash
## Writen by Qinys
## Date:2018-06-22

sum=$[$1+$2]
echo "sum‘s value is : $sum"


运行结果为

技术分享图片

【Linux】通过传入变量进行数学运算

原文:https://www.cnblogs.com/OliverQin/p/9229802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!