Linux Shell 数学运算
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 数学运算的更多相关文章
- Shell初学(六)Linux Shell 时间运算以及时间差计算方法
Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...
- Linux shell 整数运算 let [ ] (( )) expr以及 浮点数 bc用法(转)
Abstract : 1) Linux shell 中使用 let , [ ] ,(( )) 三种运算符操作 shell 变量进行简单的基本运算:2)Linux shell 中使用 expr 与 b ...
- shell 数学运算
数学运算之 expr expr操作符对照表 比较大小,只能对整数进行比较,需要加空格,linux 保留关键字要转义 num1=30 num2=50 expr $num1 \> $num2 查看上 ...
- 7 shell 数学运算
shell中数学运算的易错点: 1.在 Bash Shell 中,如果不特别指明,每一个变量的值都是字符串,无论你给变量赋值时有没有使用引号,值都会以字符串的形式存储.即使是将整数和小数赋值给变量,它 ...
- shell数学运算
shell的数学运算 branches@ubuntu:~$ var1=$[ * ] branches@ubuntu:~$ echo $var1 branches@ubuntu:~$ var2=$[$v ...
- 【Linux】shell数学运算
在Bash shell环境中,可以利用let.(())和[]执行基本的算术操作.而在进行高级操作时,expr和bc这两个工具就特别有用 let的使用 Script01.sh #!/bin/bash # ...
- shell 数学运算总结
# !/bin/bash ## 整数-算数运算 ### 1. expr r=`expr 4 + 5` ### Tips:1. '4''+''5'三者之间有空白 echo $r; r=`expr 4 \ ...
- shell编程之数学运算
shell数学运算支持整数运算的四种方法 1.let命令 no1=4; no2=5; let result=no1+no2 2.[]操作符 result=$[ no1 + no2] 3.(())操作符 ...
- shell执行数学运算
整数: expr let $(()) $[] 浮点数: bc 1.使用expr ♦参与运算的成员和运算符之间必须有一个空格: ♦对于那些容易被shell错解的,在它们传入expr命令之前,需要使用sh ...
随机推荐
- WPF 位置转化和动画
位置转化 private void DrawScale() { double majorTickUnitValue = this.ScaleSweepLenth / this.MajorDivisio ...
- IOS 录像软件
http://iphone.91.com/tutorial/cjjc/140430/21683219.html
- iOS开发的小技巧(断点打印)
iOS开发中我们会碰到这样的需求:打印沙盒目录,打印对象信息,对象信息可以通过断点查看,有时候对象属性繁多时看起来又比较麻烦. 今天学到一个比较实用的方法: 在运行时打一个断点,当程序停在这个断点后, ...
- 编程实现Windows关机、重启、注销
要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...
- JS复制对象
CSSCommonJS.DeepCopy = function (json) { if (typeof json == 'number' || typeof json == 'string' || t ...
- R语言学习笔记:取数据子集
上文介绍了,如何生成序列,本文介绍一下如何取出其数据子集 取出元素的逻辑值 > x<-c(0,-3,4,-1,45,90,5) > x>0 [1] FALSE FALSE T ...
- Git教程(1)官网及官方中文教程
1,Git官网 http://www.git-scm.com/ 2,官方中文教程 http://git-scm.com/book/zh/v2
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...
- Hadoop集群安装配置教程_Hadoop2.6.0_Ubuntu/CentOS
摘自:http://www.powerxing.com/install-hadoop-cluster/ 本教程讲述如何配置 Hadoop 集群,默认读者已经掌握了 Hadoop 的单机伪分布式配置,否 ...