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 ...
随机推荐
- PAT-乙级-1010. 一元多项式求导 (25)
1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一 ...
- 团体程序设计天梯赛-练习集L2-001. 紧急救援(dijkstra)
L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...
- select框的text与value值的获取(实用版)
function def(){ var key = document.getElementById ('selectarea'); //select list var value = docum ...
- 面试常考的数据结构Java实现
1.线性表 2.线性链表 3.栈 4.队列 5.串 6.数组 7.广义表 8.树和二叉树 的结点),并且,二叉树的子树有左右之分,其次序不能任意颠倒. 二叉树的性质: :在二叉树的第 i 层上至多有2 ...
- js setTimeout深度递归后完成回调
setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage]) setTimeout有两种形式 se ...
- nagios&pnp4nagios--yum 安装
一. 环境: 1. centos 6.4 2. 设置hostname 并且安装好apache 3. 关闭selinux及iptables 二. 安装nagios服务器端: 1. rpm -Uvh ht ...
- Android开发框架之xUtils学习
1.一个非作者弄的xUtils API文档: http://xutilsapi.oschina.mopaas.com/overview-summary.html 2.使用xUtils用户的一些博客文档 ...
- 1890. Money out of Thin Air(线段树 dfs转换区间)
1890 将树的每个节点都转换为区间的形式 然后再利用线段树对结点更新 这题用了延迟标记 相对普通线段树 多了dfs的转换 把所要求的转换为某段区间 RE了N次 最后没办法了 记得有个加栈的语句 拿来 ...
- poj 2506 Tiling(递推 大数)
题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...
- mvn命令
打包:mvn package 编译:mvn compile 编译测试程序:mvn test-compile 清空:mvn clean 运行测试:mvn test 生成站点目录: mvn site 生成 ...