Linux Shell 数学运算

在Linux中直接使用数学运算符进行数学运算往往得不到我们想要的计算结果。要在Shell中进行数学运算,我们需要借助点小手段。目前,Linux Shell中进行数学运算的方法主要有三种:bc、expr、let。

1 bc

1.1 命令行方式

在bash界面,直接输入bc或者bc -q,就可以进去bc的命令行,通过使用数学运算符能够得到我们想要的结果:

 [scott@centos1 ~]$ bc -q

 +

 -

 -

 *

 /

 %

 ^

 scale=;/

 .

 %

 scale=;/

 %

输入运算数和运算符号,回车即可得到运算结果。通过设置scale,可以定义当前的小数点精度,对除法、取余和幂运算有效。

这种方式只能在bc的命令行中进行,在代码中当然不能这样干了。

1.2 管道方式

 [scott@centos1 ~]$ echo +|bc

 [scott@centos1 ~]$ echo -|bc

 -

 [scott@centos1 ~]$ echo *|bc

 [scott@centos1 ~]$ echo /|bc

 [scott@centos1 ~]$ echo %|bc

 [scott@centos1 ~]$ echo "scale=2;2/3"|bc

 .

 [scott@centos1 ~]$ echo "scale=2;2%3"|bc

 .

 [scott@centos1 ~]$ echo "scale=2;3/2"|bc

 1.50

 [scott@centos1 ~]$ echo "scale=2;3%2"|bc

 [scott@centos1 ~]$ echo ^|bc

这种管道方式在shell中应用的更多一些,同样可以在运算的时候加上精度的限制。

1.3 进制转换

 [scott@centos1 ~]$ echo "ibase=10;15"|bc

 [scott@centos1 ~]$ echo "ibase=8;15"|bc

 [scott@centos1 ~]$ echo "ibase=16;F"|bc

上文的例子,是把几种进制都转化为10进制。

1.4 表达式运算

 [scott@centos1 ~]$ vim bc-test.bc

 [scott@centos1 ~]$ bc -q bc-test.bc

 -

 .

其中,bc-test.bc的内容为:

+

-

*

/

scale=;/

scale=;/

就是表达式的集合。

2 expr

expr是个很强大的命令,可以进行数学运算,也可以进行字符串的操作等。先看下数学运算的功能。

 [scott@centos1 ~]$ expr +

 +

 [scott@centos1 ~]$ expr  +

 expr: syntax error

 [scott@centos1 ~]$ expr  + 

 [scott@centos1 ~]$ expr  * 

 expr: syntax error

 [scott@centos1 ~]$ expr  \* 

 [scott@centos1 ~]$ expr  / 

 [scott@centos1 ~]$ expr  % 

expr不支持浮点运算,不支持幂乘运算,在运算的时候可要注意运算符和运算数的分离,写在一起可是不识别的,另外,乘法有点特殊,需要转义。

下面看看expr的字符串操作。

 [scott@centos1 ~]$ string=123456789asdfg

 [scott@centos1 ~]$ expr length $string

 [scott@centos1 ~]$ expr index $string ''

 [scott@centos1 ~]$ expr substr $string  

 789a

 [scott@centos1 ~]$ expr substr $string  

 789asdfg

上例分别利用expr命令进行了计算字符串长度、获取字串或者字符的首次出现位置、取指定位置开始的限定长度的字符字串,需要注意的是expr中的下标是从1开始的。

3 let

 [scott@centos1 ~]$ let a=+

 [scott@centos1 ~]$ echo $a

 [scott@centos1 ~]$ let a=*

 [scott@centos1 ~]$ echo $a

 [scott@centos1 ~]$ let a=/

 [scott@centos1 ~]$ echo $a

 [scott@centos1 ~]$ let a=%

 [scott@centos1 ~]$ echo $a

 [scott@centos1 ~]$ let a=^

 [scott@centos1 ~]$ echo $a

 [scott@centos1 ~]$ let a=**

 [scott@centos1 ~]$ echo $a

需要注意的是,let命令里的幂乘运算不是^,而是**。

4 其他方式

 [scott@centos1 ~]$ echo $((+))

 [scott@centos1 ~]$ echo $((*))

 [scott@centos1 ~]$ echo $((**))

 [scott@centos1 ~]$ echo $((((+))*))

 [scott@centos1 ~]$ echo `date`

 Fri Aug  :: PDT 

 [scott@centos1 ~]$ echo `date +%Y%m%d`

Linux Shell 数学运算的更多相关文章

  1. Shell初学(六)Linux Shell 时间运算以及时间差计算方法

    Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...

  2. Linux shell 整数运算 let [ ] (( )) expr以及 浮点数 bc用法(转)

    Abstract : 1)  Linux shell 中使用 let , [ ] ,(( )) 三种运算符操作 shell 变量进行简单的基本运算:2)Linux shell 中使用 expr 与 b ...

  3. shell 数学运算

    数学运算之 expr expr操作符对照表 比较大小,只能对整数进行比较,需要加空格,linux 保留关键字要转义 num1=30 num2=50 expr $num1 \> $num2 查看上 ...

  4. 7 shell 数学运算

    shell中数学运算的易错点: 1.在 Bash Shell 中,如果不特别指明,每一个变量的值都是字符串,无论你给变量赋值时有没有使用引号,值都会以字符串的形式存储.即使是将整数和小数赋值给变量,它 ...

  5. shell数学运算

    shell的数学运算 branches@ubuntu:~$ var1=$[ * ] branches@ubuntu:~$ echo $var1 branches@ubuntu:~$ var2=$[$v ...

  6. 【Linux】shell数学运算

    在Bash shell环境中,可以利用let.(())和[]执行基本的算术操作.而在进行高级操作时,expr和bc这两个工具就特别有用 let的使用 Script01.sh #!/bin/bash # ...

  7. shell 数学运算总结

    # !/bin/bash ## 整数-算数运算 ### 1. expr r=`expr 4 + 5` ### Tips:1. '4''+''5'三者之间有空白 echo $r; r=`expr 4 \ ...

  8. shell编程之数学运算

    shell数学运算支持整数运算的四种方法 1.let命令 no1=4; no2=5; let result=no1+no2 2.[]操作符 result=$[ no1 + no2] 3.(())操作符 ...

  9. shell执行数学运算

    整数: expr let $(()) $[] 浮点数: bc 1.使用expr ♦参与运算的成员和运算符之间必须有一个空格: ♦对于那些容易被shell错解的,在它们传入expr命令之前,需要使用sh ...

随机推荐

  1. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

  2. Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. Linux---弹球游戏

    ReadMe: 弹球游戏按键说明(注意大小写): Q End Up Game 游戏停止 P Play Again  再玩一次 f 速度x轴方向减速 s 速度x轴方向加速 F 速度y轴方向减速 S 速度 ...

  4. SQLite入门与分析(四)---Page Cache之事务处理(1)

    写在前面:从本章开始,将对SQLite的每个模块进行讨论.讨论的顺序按照我阅读SQLite的顺序来进行,由于项目的需要,以及时间关系,不能给出一个完整的计划,但是我会先讨论我认为比较重要的内容.本节讨 ...

  5. Android:布局合集

    本文归纳Android布局中所用到的知识点,网络上的教程说得太细化,而对于前端来说,下面的归纳最适合不过了. Android五大布局: LinearLayout 线性布局 Relativelayout ...

  6. 213. House Robber II

    题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...

  7. IIS7 发布mvc3.0

    Windows7系统和我们见面已经有一段时间了,在我们经过一段时间熟悉了她的新鲜好玩儿的功能之后,也许我们该静下心来想一下怎么用她做一些与学习有 关的事情,从Windows7的第一个试用版到现在的零售 ...

  8. innodb master主线程

    http://wenku.baidu.com/link?url=MhY9yhHTgeOlyooWDvaVfPkW3cuVSX_rIZv2QtCu7GLeEuqSfYh_M7Yvl1N4IY08a3ws ...

  9. [原]Unity3D深入浅出 - 新版动画系统(Mecanim)

    Mecanim概述: Mecanim是Unity提供第一个丰富而复杂的动画系统,提供了: 针对人形角色的简易的工作流和动画创建能力 Retargeting(运动重定向)功能,即把动画从一个角色模型应用 ...

  10. Sencha touch Panel之间的跳转(如不使用TabPanel或者Carousel控件而产生跳转的动画效果)

    常规的Sencha touch 应用都是"header content footer"结构,这样的结构无疑将使用TabPanel来实现,而且TabPanel肯定是card布局,这样 ...