使用shell進行數學運算,shell數學運算

來源:互聯網
上載者:User

使用shell進行數學運算,shell數學運算

可以利用 let(())[]進行基本的算術操作,進階操作使用bc更加高效。

直接看代碼把,輸出很清楚。

let
[zhengweiwu 22:17 ~]$ no1=4[zhengweiwu 22:24 ~]$ no2=5[zhengweiwu 22:24 ~]$ let result=no1+no2[zhengweiwu 22:24 ~]$ echo $result9[zhengweiwu 22:24 ~]$ let result=result + 0.9 # 運算子兩側不能有空格-bash: let: +: syntax error: operand expected (error token is "+")[zhengweiwu 22:25 ~]$ let result=result+0.9 # 只能進行整數運算-bash: let: result=result+0.9: syntax error: invalid arithmetic operator (error token is ".9")[zhengweiwu 22:25 ~]$ echo $result9
[]
[zhengweiwu 22:26 ~]$ result=$[no1+no2][zhengweiwu 22:27 ~]$ echo $result9[zhengweiwu 22:27 ~]$ result=$[no1+no2+1][zhengweiwu 22:27 ~]$ echo $result10
(())
[zhengweiwu 22:28 ~]$ result=$((1+1))[zhengweiwu 22:28 ~]$ echo $result2[zhengweiwu 22:28 ~]$ result=$(( 10 +1))[zhengweiwu 22:29 ~]$ echo $result11
bc
[zhengweiwu 22:29 ~]$ echo "4*3.5" | bc14.0[zhengweiwu 22:29 ~]$ echo "4*3.54" | bc14.16[zhengweiwu 22:30 ~]$ result=$(echo "$no1*0.44" | bc)  # 作為子shell計算結果[zhengweiwu 22:30 ~]$ echo $result1.76
[zhengweiwu 22:30 ~]$ echo "scale=2;3/8" | bc # 設定小數精度.37[zhengweiwu 22:31 ~]$ echo "scale=4;3/8" | bc.3750[zhengweiwu 22:31 ~]$ echo "scale=6;3/8" | bc.375000
[zhengweiwu 22:31 ~]$ no=100[zhengweiwu 22:33 ~]$ echo "obase=2;$no" | bc # 輸出為2進位,預設輸入進位為10進位1100100[zhengweiwu 22:33 ~]$ no=1100100[zhengweiwu 22:33 ~]$ echo "obase=10;ibase=2;$no" | bc # 指明輸出進位和輸入進位100
[zhengweiwu 22:34 ~]$ echo "sqrt(100)" | bc10[zhengweiwu 22:35 ~]$ echo "10^10" | bc10000000000

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.