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 ...
随机推荐
- 怎样快糙猛的开始搞Kaggle比赛
- The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971 题意:将输入的英文数字表达转化为阿拉伯数字. #include< ...
- 论MOBA类游戏五号位的重要性
观众朋友们,也许你对题目很好奇,才打开这篇文章.为什么技术圈中会出现游戏类的软文?如果时间充足,可以继续往下看. MOBA 类游戏的兴起,逐渐吞噬游戏市场,以病毒式的扩张方式肆意改变着游戏玩家内心对游 ...
- 程序自动生成Dump文件
前言:通过drwtsn32.NTSD.CDB等调试工具生成Dump文件, drwtsn32存在的缺点虽然NTSD.CDB可以完全解决,但并不是所有的操作系统中都安装了NTSD.CDB等调试工具.了解了 ...
- linux crontab任务调度的使用
(不推荐)可以直接将要调度的任务写入crontab任务表中 (推 荐)可以先将要完成的任务写入一个shell文件,如myTask.sh(还可能需要将该文件改为可执行的:chmod 744 myTa ...
- linux查看磁盘使用情况
# 查看磁盘使用情况 $ df -l # 查看某个目录在哪个分区,比如查看/root文件夹在哪个分区 $ df /root # 查看linux系统具体分区情况 $ fdisk -l
- POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...
- Android EditView 阻止软键盘自动弹出
最近再做一个查询内的小应用,界面最上面是一个EditText查询框,进行Activity后,总会弹起软键盘.这样就挡住了查询框下面的其他查询条件 控件,感觉很不友好.所以现在要做的就是在进入Activ ...
- C++11内存模型的粗略解释
基本解释 C++11引入了多线程,同时也引入了一套内存模型.从而提供了比较完善的一套多线程体系.在单线程时代,一切都很简单.没有共享数据,没有乱序执行,所有的指令的执行都是按照预定的时间线.但是也正是 ...
- Ubuntu12.04下arm交叉编译环境的建立
http://blog.csdn.net/heyangya2009/article/details/5424376 备注:ubuntu12.04+Android+Real6410 在主机上用来编译其他 ...