1）bc

bc在默认的情况下是个交互式的指令。在bc工作环境下，可以使用以下计算符号：
+ 加法
- 减法
* 乘法
/ 除法
^ 指数
% 余数

# 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+6 &lt;=加法 9 4+2*3 &lt;=加法、乘法 10 (4+2)*3 &lt;=加法、乘法（优先） 18 4*6/8 &lt;=乘法、除法 3 10^3 &lt;=指数 1000 18%5 &lt;=余数 3+4;5*2;5^2;18/4 &lt;=一行输入多个计算，用;相隔。 7 10 25 4 quit &lt;=退出
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } # 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'. scale=3 &lt;=设小数位 1/3 .333 quit .csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

A.用echo和|法，如：

# echo "(6+3)*2" |bc 18 # echo 15/4 |bc 3 # echo "scale=2;15/4" |bc 3.75 # echo "3+4;5*2;5^2;18/4" |bc 7 10 25 4
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

//将16进制的A7输出为10进制, 注意，英文只能大写
# echo "ibase=16;A7" |bc
167
//将2进制的11111111转成10进制
# echo "ibase=2;11111111" |bc
255
//输入为16进制，输出为2进制
# echo "ibase=16;obase=2;B5-A4" |bc
10001

# more calc.txt 3+2 4+5 8*2 10/4 # bc calc.txt 5 9 16 2
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
2)expr

expr命令可不光能计算加减乘除哦，还有很多表达式，都可以计算出结果，不过有一点需要注意，在计算加减乘除时，不要忘了使用空格和转义。下面直接用实例来介绍一下expr的运算，如：

# expr 6 + 3 （有空格） 9 # expr 2 \* 3 （有转义符号） 6 # expr 14 % 9 5 # a=3 # expr \$a+5 （无空格） 3+5 # expr \$a + 5 （变量，有空格） 8 # a=`expr 4 + 2` echo \$a 6 # expr \$a + 3 9
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

//字串长度
# expr length "yangzhigang.cublog.cn"
21
//从位置处抓取字串
# expr substr "yangzhigang.cublog.cn" 1 11
yangzhigang

//字串开始处
# expr index "yangzhigang.cublog.cn" cu
13

3)dc

# dc 3 2+ p 5 4* p 20 quit # echo 3 2+ 4* p |dc 20
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
4)echo

echo用来进行回显，是周知的事。上面也配合bc来进行计算。其实echo也可以单独进行简单的计算，如：

# echo \$((3+5)) 8 # echo \$(((3+5)*2)) 16 echo还可以进行变量的计算，如： # a=10 # b=5 # echo \$((\$a+\$b)) 15 # echo \$a+\$b 10+5 # echo \$a+\$b |bc 15 //计算前天的日期 # echo `date +%Y%m%d` 20090813 # echo `date +%Y%m%d`-2 20090813-2 # echo `date +%Y%m%d`-2 |bc 20090811
.csharpcode, .csharpcode pre { font-size: small; color: black;"Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
5)AWK

awk在处理文件的时，可以进行运算，那当然也可以单单用来计算了，如：
# awk 'BEGIN{a=3+2;print a}'
5
# awk 'BEGIN{a=(3+2)*2;print a}'
10