一個簡單的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"
運行結果為