linux下计算命令bc的使用

http://codingstandards.iteye.com/blog/793734

用途说明

Bash内置了对整数四则运算的支持,但是并不支持浮点运算,而bc命令可以很方便的进行浮点运算,当然整数运算也不再话下。手册页上说bc是An arbitrary precision calculator language,即一个任意精度的计算语言,注意是一种语言,它提供了一些语法结构,比如条件判断、循环等,可以说是很强大的,但是我在实际中还没有找到需要这个用途的场合。另外一个用途就是用来进行进制转换。

常用参数

一般情况下,我们使用不带任何参数的bc命令。

bc

如果需要bc不输出提示信息,可以加上-q参数:

bc -q

如果要使用强大的数学库,比如计算三角函数,需要加上-l参数:

bc -l

因为bc本身是一个命令解释器,要退出它只要直接输入quit回车或者按Ctrl+D终止。

使用示例示例一 命令行方式使用bc

[root@localhost centos39]#bcbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty’.3+473-4-13*4123/40scale=2;3/4# 保留小数点精度只对除法、取余、乘幂有效.753/4.753%40scale=03%433^481

Ctrl+D[root@localhost centos39]#

示例二 通过管道使用bc来计算

[root@localhost centos39]#echo 3 * 4 | bc(standard_in) 1: parse error[root@localhost centos39]#echo "3 * 4" | bc12[root@localhost centos39]#echo "scale=7; 355/113" | bc3.1415929[root@localhost centos39]#

示例三 进制转换

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

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

示例四 将多个表达式写在一个文件中一起计算

[root@rhel55 ~]#cat test.bc123*321123/321scale=4;123/321[root@rhel55 ~]#bc test.bcbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty’.394830.3831

Ctrl+D[root@rhel55 ~]#[root@rhel55 ~]#cat test.bc | bc394830.3831[root@rhel55 ~]#

示例五 一个计算三角形面积的Bash脚本

先复习一下初中的知识:b表示三角形的底,h表示三角形的高,那么三角形的面积计算公式是b*h/2。

文件:area_of_triangle.sh

Bash代码

    #!/bin/bash#Shellprogram/scripttoreadthebaseandheightofatraingleandfinditsarea#————————————————————————-#Copyright(c)2005nixCraftproject<http://cyberciti.biz/fb/>#ThisscriptislicensedunderGNUGPLversion2.0orabove#————————————————————————-#ThisscriptispartofnixCraftshellscriptcollection(NSSC)#Visithttp://bash.cyberciti.biz/formoreinformation.#————————————————————————-#Formulainfo:http://www.mste.uiuc.edu/dildine/heron/triarea.html#Area=(1/2)xBasexHeightecho-n"Enterbaseofatriangle:"readbecho-n"Enterheightofatriangle:"readh#calculateitanddisplaybackarea=$(echo"scale=2;(1/2)*$b*$h"|bc)echo"Areaofatriangleis$area"

[root@smsgw academic]#./area_of_triangle.shEnter base of a triangle :123Enter height of a triangle :321Area of a triangle is 19741.50[root@smsgw academic]#

示例六 使用bc命令的脚本片段Bash代码

    #usage:calc_sum<num1><num2>#计算两个数的和calc_sum(){bc-q<<EOF$1+$2EOF}#usage:calc_free<count>#计算费用,单价0.05元calc_fee(){bc-q<<EOF0.05*$1EOF}

将以上代码粘贴到终端。

[root@web ~]## usage: calc_sum <num1> <num2>[root@web ~]## 计算两个数的和[root@web ~]#calc_sum()>{>bc -q <<EOF>$1+$2>EOF>}[root@web ~]#[root@web ~]## usage: calc_free <count>[root@web ~]## 计算费用,单价0.05元[root@web ~]#calc_fee()>{>bc -q <<EOF>0.05*$1>EOF>}[root@web ~]#[root@web ~]#[root@web ~]#calc_sum 123 321444[root@web ~]#calc_fee 100050.00[root@web ~]#

示例七 使用数学库

有文章称可以计算100位的圆周率pi值。

[root@web ~]#echo "scale=100; a(1)*4" | bcRuntime error (func=(main), adr=11): Function a not defined.[root@web ~]#echo "scale=100; a(1)*4" | bc -l3.141592653589793238462643383279502884197169399375105820974944592307\8164062862089986280348253421170676[root@web ~]#

问题思考相关资料

【1】乡下猫 LINUX技术博客linux中的bc命令(简单好用的计算器)

【2】想象力的专栏Linux下的计算器(bc、expr、dc、echo、awk)知多少?

【3】我是网管Liunx学习笔记19–bc计算器

【4】LAMP Chinalinux bc命令使用

人之所以能,是相信能。

linux下计算命令bc的使用

相关文章:

你感兴趣的文章:

标签云: