學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

linux計(jì)算命令

時(shí)間: 佳洲1085 分享

  linux下當(dāng)我們要計(jì)算大數(shù)據(jù)時(shí)可以通過bc命令來執(zhí)行。那么它的具體語法是什么呢?下面由學(xué)習(xí)啦小編為大家整理了linux計(jì)算命令的相關(guān)知識(shí),希望大家喜歡!

  一、linux計(jì)算命令bc命令詳解

  用途說明

  Bash內(nèi)置了對(duì)整數(shù)四則運(yùn)算的支持,但是并不支持浮點(diǎn)運(yùn)算,而bc命令可以很方便的進(jìn)行浮點(diǎn)運(yùn)算,當(dāng)然整數(shù)運(yùn)算也不再話下。手冊(cè)頁上說bc是An arbitrary precision calculator language,即一個(gè)任意精度的計(jì)算語言,注意是一種語言,它提供了一些語法結(jié)構(gòu),比如條件判斷、循環(huán)等,可以說是很強(qiáng)大的,但是我在實(shí)際中還沒有找到需要這個(gè)用途的場(chǎng)合 。另外一個(gè)用途就是用來進(jìn)行進(jìn)制轉(zhuǎn)換。

  常用參數(shù)

  一般情況下,我們使用不帶任何參數(shù)的bc命令。

  bc

  如果需要bc不輸出提示信息,可以加上-q參數(shù):

  bc -q

  如果要使用強(qiáng)大的數(shù)學(xué)庫,比如計(jì)算三角函數(shù),需要加上-l參數(shù):

  bc -l

  因?yàn)閎c本身是一個(gè)命令解釋器,要退出它只要直接輸入quit回車或者按Ctrl+D終止。

  二、linux計(jì)算命令使用示例

  示例一 命令行方式使用bc

  [root@localhost centos39]# bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  3+4

  7

  3-4

  -1

  3*4

  12

  3/4

  0

  scale=2;3/4 # 保留小數(shù)點(diǎn)精度只對(duì)除法、取余、乘冪有效

  .75

  3/4

  .75

  3%4

  0

  scale=0

  3%4

  3

  3^4

  81

  Ctrl+D

  [root@localhost centos39]#

  示例二 通過管道使用bc來計(jì)算

  [root@localhost centos39]# echo 3 * 4 | bc

  (standard_in) 1: parse error

  [root@localhost centos39]# echo "3 * 4" | bc

  12

  [root@localhost centos39]# echo "scale=7; 355/113" | bc

  3.1415929

  [root@localhost centos39]#

  示例三 進(jìn)制轉(zhuǎn)換

  [root@rhel55 ~]# echo "ibase=16; FFFF" | bc

  65535

  [root@rhel55 ~]# echo "obase=16; 1000" | bc

  3E8

  [root@rhel55 ~]#

  示例四 將多個(gè)表達(dá)式寫在一個(gè)文件中一起計(jì)算

  [root@rhel55 ~]# cat test.bc

  123*321

  123/321

  scale=4;123/321

  [root@rhel55 ~]# bc test.bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  39483

  0

  .3831

  Ctrl+D

  [root@rhel55 ~]#

  [root@rhel55 ~]# cat test.bc | bc

  39483

  0

  .3831

  [root@rhel55 ~]#

  示例五 一個(gè)計(jì)算三角形面積的Bash腳本

  先復(fù)習(xí)一下初中的知識(shí):b表示三角形的底,h表示三角形的高,那么三角形的面積計(jì)算公式是b*h/2 。

  文件 :area_of_triangle.sh

  Bash代碼

  #!/bin/bash

  # Shell program/script to read the base and height of a traingle and find its area

  # -------------------------------------------------------------------------

  # Copyright (c) 2005 nixCraft project

  # This script is licensed under GNU GPL version 2.0 or above

  # -------------------------------------------------------------------------

  # This script is part of nixCraft shell script collection (NSSC)

  # Visit http://bash.cyberciti.biz/ for more information.

  # -------------------------------------------------------------------------

  # Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html

  # Area=(1/2) x Base x Height

  echo -n "Enter base of a triangle : "

  read b

  echo -n "Enter height of a triangle : "

  read h

  # calculate it and display back

  area=$(echo "scale=2;(1/2) * $b * $h"|bc)

  echo "Area of a triangle is $area"

  [root@smsgw academic]# ./area_of_triangle.sh

  Enter base of a triangle : 123

  Enter height of a triangle : 321

  Area of a triangle is 19741.50

  [root@smsgw academic]#

  示例六 使用bc命令的腳本片段

  Bash代碼

  # usage: calc_sum

  # 計(jì)算兩個(gè)數(shù)的和

  calc_sum()

  {

  bc -q <

   class="main">

linux計(jì)算命令

時(shí)間: 佳洲1085 分享
  linux下當(dāng)我們要計(jì)算大數(shù)據(jù)時(shí)可以通過bc命令來執(zhí)行。那么它的具體語法是什么呢?下面由學(xué)習(xí)啦小編為大家整理了linux計(jì)算命令的相關(guān)知識(shí),希望大家喜歡!

  一、linux計(jì)算命令bc命令詳解

  用途說明

  Bash內(nèi)置了對(duì)整數(shù)四則運(yùn)算的支持,但是并不支持浮點(diǎn)運(yùn)算,而bc命令可以很方便的進(jìn)行浮點(diǎn)運(yùn)算,當(dāng)然整數(shù)運(yùn)算也不再話下。手冊(cè)頁上說bc是An arbitrary precision calculator language,即一個(gè)任意精度的計(jì)算語言,注意是一種語言,它提供了一些語法結(jié)構(gòu),比如條件判斷、循環(huán)等,可以說是很強(qiáng)大的,但是我在實(shí)際中還沒有找到需要這個(gè)用途的場(chǎng)合 。另外一個(gè)用途就是用來進(jìn)行進(jìn)制轉(zhuǎn)換。

  常用參數(shù)

  一般情況下,我們使用不帶任何參數(shù)的bc命令。

  bc

  如果需要bc不輸出提示信息,可以加上-q參數(shù):

  bc -q

  如果要使用強(qiáng)大的數(shù)學(xué)庫,比如計(jì)算三角函數(shù),需要加上-l參數(shù):

  bc -l

  因?yàn)閎c本身是一個(gè)命令解釋器,要退出它只要直接輸入quit回車或者按Ctrl+D終止。

  二、linux計(jì)算命令使用示例

  示例一 命令行方式使用bc

  [root@localhost centos39]# bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  3+4

  7

  3-4

  -1

  3*4

  12

  3/4

  0

  scale=2;3/4 # 保留小數(shù)點(diǎn)精度只對(duì)除法、取余、乘冪有效

  .75

  3/4

  .75

  3%4

  0

  scale=0

  3%4

  3

  3^4

  81

  Ctrl+D

  [root@localhost centos39]#

  示例二 通過管道使用bc來計(jì)算

  [root@localhost centos39]# echo 3 * 4 | bc

  (standard_in) 1: parse error

  [root@localhost centos39]# echo "3 * 4" | bc

  12

  [root@localhost centos39]# echo "scale=7; 355/113" | bc

  3.1415929

  [root@localhost centos39]#

  示例三 進(jìn)制轉(zhuǎn)換

  [root@rhel55 ~]# echo "ibase=16; FFFF" | bc

  65535

  [root@rhel55 ~]# echo "obase=16; 1000" | bc

  3E8

  [root@rhel55 ~]#

  示例四 將多個(gè)表達(dá)式寫在一個(gè)文件中一起計(jì)算

  [root@rhel55 ~]# cat test.bc

  123*321

  123/321

  scale=4;123/321

  [root@rhel55 ~]# bc test.bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  39483

  0

  .3831

  Ctrl+D

  [root@rhel55 ~]#

  [root@rhel55 ~]# cat test.bc | bc

  39483

  0

  .3831

  [root@rhel55 ~]#

  示例五 一個(gè)計(jì)算三角形面積的Bash腳本

  先復(fù)習(xí)一下初中的知識(shí):b表示三角形的底,h表示三角形的高,那么三角形的面積計(jì)算公式是b*h/2 。

  文件 :area_of_triangle.sh

  Bash代碼

  #!/bin/bash

  # Shell program/script to read the base and height of a traingle and find its area

  # -------------------------------------------------------------------------

  # Copyright (c) 2005 nixCraft project

  # This script is licensed under GNU GPL version 2.0 or above

  # -------------------------------------------------------------------------

  # This script is part of nixCraft shell script collection (NSSC)

  # Visit http://bash.cyberciti.biz/ for more information.

  # -------------------------------------------------------------------------

  # Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html

  # Area=(1/2) x Base x Height

  echo -n "Enter base of a triangle : "

  read b

  echo -n "Enter height of a triangle : "

  read h

  # calculate it and display back

  area=$(echo "scale=2;(1/2) * $b * $h"|bc)

  echo "Area of a triangle is $area"

  [root@smsgw academic]# ./area_of_triangle.sh

  Enter base of a triangle : 123

  Enter height of a triangle : 321

  Area of a triangle is 19741.50

  [root@smsgw academic]#

  示例六 使用bc命令的腳本片段

  Bash代碼

  # usage: calc_sum

  # 計(jì)算兩個(gè)數(shù)的和

  calc_sum()

  {

  bc -q <

  $1+$2

  EOF

  }

  # usage: calc_free

  # 計(jì)算費(fèi)用,單價(jià)0.05元

  calc_fee()

  {

  bc -q <

  0.05*$1

  EOF

  }

  將以上代碼粘貼到終端。

  [root@web ~]# # usage: calc_sum

  [root@web ~]# # 計(jì)算兩個(gè)數(shù)的和

  [root@web ~]# calc_sum()

  > {

  > bc -q <

  > $1+$2

  > EOF

  > }

  [root@web ~]#

  [root@web ~]# # usage: calc_free

  [root@web ~]# # 計(jì)算費(fèi)用,單價(jià)0.05元

  [root@web ~]# calc_fee()

  > {

  > bc -q <

  > 0.05*$1

  > EOF

  > }

  [root@web ~]#

  [root@web ~]#

  [root@web ~]# calc_sum 123 321

  444

  [root@web ~]# calc_fee 1000

  50.00

  [root@web ~]#

  示例七 使用數(shù)學(xué)庫

  有文章稱可以計(jì)算100位的圓周率pi值。

  [root@web ~]# echo "scale=100; a(1)*4" | bc

  Runtime error (func=(main), adr=11): Function a not defined.

  [root@web ~]# echo "scale=100; a(1)*4" | bc -l

  3.141592653589793238462643383279502884197169399375105820974944592307\

  8164062862089986280348253421170676

  [root@web ~]#

  EOF

  }

  # usage: calc_free

  # 計(jì)算費(fèi)用,單價(jià)0.05元

  calc_fee()

  {

  bc -q <

  0.05* class="main">

linux計(jì)算命令

時(shí)間: 佳洲1085 分享

  EOF

  }

  將以上代碼粘貼到終端。

  [root@web ~]# # usage: calc_sum

  [root@web ~]# # 計(jì)算兩個(gè)數(shù)的和

  [root@web ~]# calc_sum()

  > {

  > bc -q <

  > class="main">

linux計(jì)算命令

時(shí)間: 佳洲1085 分享
  linux下當(dāng)我們要計(jì)算大數(shù)據(jù)時(shí)可以通過bc命令來執(zhí)行。那么它的具體語法是什么呢?下面由學(xué)習(xí)啦小編為大家整理了linux計(jì)算命令的相關(guān)知識(shí),希望大家喜歡!

  一、linux計(jì)算命令bc命令詳解

  用途說明

  Bash內(nèi)置了對(duì)整數(shù)四則運(yùn)算的支持,但是并不支持浮點(diǎn)運(yùn)算,而bc命令可以很方便的進(jìn)行浮點(diǎn)運(yùn)算,當(dāng)然整數(shù)運(yùn)算也不再話下。手冊(cè)頁上說bc是An arbitrary precision calculator language,即一個(gè)任意精度的計(jì)算語言,注意是一種語言,它提供了一些語法結(jié)構(gòu),比如條件判斷、循環(huán)等,可以說是很強(qiáng)大的,但是我在實(shí)際中還沒有找到需要這個(gè)用途的場(chǎng)合 。另外一個(gè)用途就是用來進(jìn)行進(jìn)制轉(zhuǎn)換。

  常用參數(shù)

  一般情況下,我們使用不帶任何參數(shù)的bc命令。

  bc

  如果需要bc不輸出提示信息,可以加上-q參數(shù):

  bc -q

  如果要使用強(qiáng)大的數(shù)學(xué)庫,比如計(jì)算三角函數(shù),需要加上-l參數(shù):

  bc -l

  因?yàn)閎c本身是一個(gè)命令解釋器,要退出它只要直接輸入quit回車或者按Ctrl+D終止。

  二、linux計(jì)算命令使用示例

  示例一 命令行方式使用bc

  [root@localhost centos39]# bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  3+4

  7

  3-4

  -1

  3*4

  12

  3/4

  0

  scale=2;3/4 # 保留小數(shù)點(diǎn)精度只對(duì)除法、取余、乘冪有效

  .75

  3/4

  .75

  3%4

  0

  scale=0

  3%4

  3

  3^4

  81

  Ctrl+D

  [root@localhost centos39]#

  示例二 通過管道使用bc來計(jì)算

  [root@localhost centos39]# echo 3 * 4 | bc

  (standard_in) 1: parse error

  [root@localhost centos39]# echo "3 * 4" | bc

  12

  [root@localhost centos39]# echo "scale=7; 355/113" | bc

  3.1415929

  [root@localhost centos39]#

  示例三 進(jìn)制轉(zhuǎn)換

  [root@rhel55 ~]# echo "ibase=16; FFFF" | bc

  65535

  [root@rhel55 ~]# echo "obase=16; 1000" | bc

  3E8

  [root@rhel55 ~]#

  示例四 將多個(gè)表達(dá)式寫在一個(gè)文件中一起計(jì)算

  [root@rhel55 ~]# cat test.bc

  123*321

  123/321

  scale=4;123/321

  [root@rhel55 ~]# bc test.bc

  bc 1.06

  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

  This is free software with ABSOLUTELY NO WARRANTY.

  For details type `warranty'.

  39483

  0

  .3831

  Ctrl+D

  [root@rhel55 ~]#

  [root@rhel55 ~]# cat test.bc | bc

  39483

  0

  .3831

  [root@rhel55 ~]#

  示例五 一個(gè)計(jì)算三角形面積的Bash腳本

  先復(fù)習(xí)一下初中的知識(shí):b表示三角形的底,h表示三角形的高,那么三角形的面積計(jì)算公式是b*h/2 。

  文件 :area_of_triangle.sh

  Bash代碼

  #!/bin/bash

  # Shell program/script to read the base and height of a traingle and find its area

  # -------------------------------------------------------------------------

  # Copyright (c) 2005 nixCraft project

  # This script is licensed under GNU GPL version 2.0 or above

  # -------------------------------------------------------------------------

  # This script is part of nixCraft shell script collection (NSSC)

  # Visit http://bash.cyberciti.biz/ for more information.

  # -------------------------------------------------------------------------

  # Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html

  # Area=(1/2) x Base x Height

  echo -n "Enter base of a triangle : "

  read b

  echo -n "Enter height of a triangle : "

  read h

  # calculate it and display back

  area=$(echo "scale=2;(1/2) * $b * $h"|bc)

  echo "Area of a triangle is $area"

  [root@smsgw academic]# ./area_of_triangle.sh

  Enter base of a triangle : 123

  Enter height of a triangle : 321

  Area of a triangle is 19741.50

  [root@smsgw academic]#

  示例六 使用bc命令的腳本片段

  Bash代碼

  # usage: calc_sum

  # 計(jì)算兩個(gè)數(shù)的和

  calc_sum()

  {

  bc -q <

  $1+$2

  EOF

  }

  # usage: calc_free

  # 計(jì)算費(fèi)用,單價(jià)0.05元

  calc_fee()

  {

  bc -q <

  0.05*$1

  EOF

  }

  將以上代碼粘貼到終端。

  [root@web ~]# # usage: calc_sum

  [root@web ~]# # 計(jì)算兩個(gè)數(shù)的和

  [root@web ~]# calc_sum()

  > {

  > bc -q <

  > $1+$2

  > EOF

  > }

  [root@web ~]#

  [root@web ~]# # usage: calc_free

  [root@web ~]# # 計(jì)算費(fèi)用,單價(jià)0.05元

  [root@web ~]# calc_fee()

  > {

  > bc -q <

  > 0.05*$1

  > EOF

  > }

  [root@web ~]#

  [root@web ~]#

  [root@web ~]# calc_sum 123 321

  444

  [root@web ~]# calc_fee 1000

  50.00

  [root@web ~]#

  示例七 使用數(shù)學(xué)庫

  有文章稱可以計(jì)算100位的圓周率pi值。

  [root@web ~]# echo "scale=100; a(1)*4" | bc

  Runtime error (func=(main), adr=11): Function a not defined.

  [root@web ~]# echo "scale=100; a(1)*4" | bc -l

  3.141592653589793238462643383279502884197169399375105820974944592307\

  8164062862089986280348253421170676

  [root@web ~]#

  > EOF

  > }

  [root@web ~]#

  [root@web ~]# # usage: calc_free

  [root@web ~]# # 計(jì)算費(fèi)用,單價(jià)0.05元

  [root@web ~]# calc_fee()

  > {

  > bc -q <

  > 0.05* class="main">

linux計(jì)算命令

時(shí)間: 佳洲1085 分享

  > EOF

  > }

  [root@web ~]#

  [root@web ~]#

  [root@web ~]# calc_sum 123 321

  444

  [root@web ~]# calc_fee 1000

  50.00

  [root@web ~]#

  示例七 使用數(shù)學(xué)庫

  有文章稱可以計(jì)算100位的圓周率pi值。

  [root@web ~]# echo "scale=100; a(1)*4" | bc

  Runtime error (func=(main), adr=11): Function a not defined.

  [root@web ~]# echo "scale=100; a(1)*4" | bc -l

  3.141592653589793238462643383279502884197169399375105820974944592307\

  8164062862089986280348253421170676

  [root@web ~]#

3599454