[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 ~]# 示例五 一个计算三角形面积的Bash脚本 先复习一下初中的知识:b表示三角形的底,h表示三角形的高,那么三角形的面积计算公式是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 <http://cyberciti.biz/fb/> # 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 <num1> <num2> # 计算两个数的和 calc_sum() { bc -q <<EOF $1+$2 EOF }