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

一个简单的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"


运行结果为

image

posted @ 2018-06-26 16:31  OLIVER_QIN  阅读(1923)  评论(0编辑  收藏  举报