【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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM