shell编程系列5--数学运算

方法1 expr $num1 operator $num2
方法2 $(($num1 operator $num2)) expr操作符对照表1

操作符 含义
num1 | num2 num1不为空且非0,返回num1;否则返回num2 num1 & num2 num1不为空且非0,返回num1;否则返回0 num1 < num2 num1小于num2,返回1;否则返回0 num1 <= num2 num1小于等于num2,返回1;否则返回0 num1 = num2 num1等于num2,返回1;否则返回0 num1 != num2 num1不等于num2,返回1;否则返回0 num1 > num2 num1大于num2,返回1;否则返回0 num1 >= num2 num1大于等于num2,返回1;否则返回0 expr操作符对照表2

操作符 含义
num1 + num2 求和
num1 - num2 求差
num1 * num2 求积
num1 / num2 求商
num1 % num2 求余数 bash数学运算之expr: # 比较大小,只能对整数进行比较
[root@es01 ~]# num1=
[root@es01 ~]# num2= # 错误:没有加空格
[root@es01 ~]# expr $num1>$num2
[root@es01 ~]# echo $? [root@es01 ~]# echo $num2 # 错误:没有转义
[root@es01 ~]# expr $num1 > $num2 # 正确写法
[root@es01 ~]# expr $num1 \> $num2 [root@es01 ~]# num1=
[root@es01 ~]# echo $num1 [root@es01 ~]# expr $num1 \> $num2 # 小于、小于等于、大于等于
[root@es01 ~]# expr $num1 \< $num2 [root@es01 ~]# expr $num1 \<= $num2 [root@es01 ~]# expr $num1 \>= $num2 # 运算 加、减、乘、除
[root@es01 ~]# num1=
[root@es01 ~]# num2=
[root@es01 ~]# expr $num1 + $num2 [root@es01 ~]# num3=`expr $num1 + $num2`
[root@es01 ~]# echo $num3 [root@es01 ~]# expr $num1 - $num2 [root@es01 ~]# expr $num1 \* $num2 [root@es01 ~]# expr $num1 / $num2 # 取余数
[root@es01 ~]# expr $num1 % $num2 # 两个小括号的计算方法,要赋值,否则会报错
[root@es01 ~]# $(($num1+$num2))
-bash: : command not found
[root@es01 ~]# num3=$(($num1+$num2))
[root@es01 ~]# echo $num3 [root@es01 ~]# num3=$(($num1*$num2))
[root@es01 ~]# echo $num3 [root@es01 ~]# num3=$(($num1-$num2))
[root@es01 ~]# echo $num3 [root@es01 ~]# num3=$(($num1/$num2))
[root@es01 ~]# echo $num3 # 部分支持,= 不支持
[root@es01 ~]# num3=$(($num1>$num2))
[root@es01 ~]# echo $num3 [root@es01 ~]# num3=$(($num1<$num2))
[root@es01 ~]# echo $num3 [root@es01 ~]# num3=$(($num1=$num2))
-bash: =: attempted assignment to non-variable (error token is "=5") 在比较运算的时候最好使用expr 练习例子: 提示用户输入一个正整数num,然后计算1+++...+num的值;必须对num是否为正整数做判断,不符合应当运行再次输入 # 判断是否大于0
[root@es01 ~]# num1=
[root@es01 ~]# expr $num1 \> # expr只能对整数进行计算,直接用expr 和一个整数计算获取 $? 的值来判断是否为整数
[root@es01 ~]# num1=56.58
[root@es01 ~]# expr $num1 \> [root@es01 ~]# expr $num1 +
expr: non-integer argument
[root@es01 ~]# echo $? [root@es01 ~]# num1= [root@es01 ~]# expr $num1 + [root@es01 ~]# echo $? # 具体脚本
# vim sum.sh
#!/bin/bash
#
while true
do
read -p "please input a positive number: " num
# 判断数是否是整数
expr $num + &> /dev/null
if [ $? -eq ];then
# 判断这个整数是否大于0,大于0返回1
if [ `expr $num \> ` -eq ];then
#echo "yes,positive number"
# $sum没有赋值,默认为0
for((i=;i<=$num;i++))
do
sum=`expr $sum + $i`
done
echo "1+2+3+...+$num = $sum"
# 执行计算需要退出
exit
fi
fi
echo "error,input enlegal"
continue
done # bc介绍
bc是bash内建的运算器,支持浮点数运算 内建变量scale可以设置,默认为0 bc操作符对照表
操作符 含义 num1 + num2 求和
num1 - num2 求差
num1 * num2 求积
num1 / num2 求商
num1 % num2 求余
num1 ^ num2 指数运算 centos7默认没有安装bc命令
yum install -y bc # 交互模式
[root@es01 shell]# bc
bc 1.06.
Copyright -, , , , , Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
+ - * / % # 小数点保留2位
scale=
/
4.60
# 小数点保留6位
scale=
/
3.285714 # 脚本中使用管道符进行计算
[root@es01 shell]# echo "23+33" | bc [root@es01 shell]# echo "23.3+66" | bc
89.3
# 保留精度 scale=; 用分号隔开
[root@es01 shell]# echo "scale=4;23.3/3.5" | bc
6.6571 # bc示例脚本 [root@es01 shell]# cat bc.sh
#!/bin/bash
# read -p "num1: " num1
read -p "num2: " num2 #echo "scale=4;$num1/$num2" | bc num3=`echo "scale=4;$num1/$num2" | bc` echo "$num1 / $num2 = $num3" [root@es01 shell]# sh bc.sh
num1: 5.6
num2:
5.6 / = 1.8666

shell编程系列5--数学运算的更多相关文章

  1. shell编程系列16--文本处理三剑客之awk模式匹配的两种方法

    shell编程系列16--文本处理三剑客之awk模式匹配的两种方法 awk的工作模式 第一种模式匹配:RegExp 第二种模式匹配:关系运算匹配 用法格式对照表 语法格式 含义 RegExp 按正则表 ...

  2. shell编程系列15--文本处理三剑客之awk格式化输出printf

    shell编程系列15--文本处理三剑客之awk格式化输出printf printf的格式说明符 格式符 含义 %s 打印字符串 %d 打印十进制数 %f 打印一个浮点数 %x 打印十六进制数 %o ...

  3. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

  4. shell编程系列3--命令替换

    shell编程系列3--命令替换 命令替换 命令替换总结 方法1 `command` 方法2 $(command) 例子1: 获取系统的所有用户并输出 for循环能以空格.换行.tab键作为分隔符 [ ...

  5. C#)Windows Shell 编程系列5 - 获取图标

    原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...

  6. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...

  7. (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单

    原文 (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 接上一节:(C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开这 ...

  8. (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开

    原文 (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一篇:(C#)Windows Shell 编 ...

  9. (C#)Windows Shell 编程系列1 - 基础,浏览一个文件夹

    原文 (C#)Windows Shell 编程系列1 - 基础,浏览一个文件夹 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) Windows Shell 编程,即 Windows ...

随机推荐

  1. LeetCode - 86、分隔链表

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...

  2. P1311 选择客栈[模拟]

    题目描述 丽江河边有nn家很有特色的客栈,客栈按照其位置顺序从 11到nn编号.每家客栈都按照某一种色调进行装饰(总共 kk 种,用整数 00 ~k-1k−1 表示),且每家客栈都设有一家咖啡店,每家 ...

  3. 原创!ngxtop-监控nginx的利器!!!

    原创!ngxtop-监控nginx的利器!!! 无论名称还是界面,ngxtop的灵感均源自大名鼎鼎的top命令.ngxtop的功能就是,分析Nginx访问日志文件(以及其他日志文件,比如Apache2 ...

  4. eclipse更改jdk版本(1.6》1.7 以此类推)

    电脑装了两个版本的JDK,在开发项目的时候默认使用的是高版本的,但是公司又要求用低版本的JDK来编译,肿么办???么事,小编这就来给你支招! eclipse 安装两个版本的JDK 1 打开eclips ...

  5. Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明(转载)

    一.自适应游标共享(Adaptive Cursor Sharing) 说明 1.1 ACS概述绑定变量使Oracle DB 可以为多条SQL 语句共享单个游标,以减少分析SQL 语句所使用的共享内存量 ...

  6. luogu T96516 [DBOI2019]持盾 可持久化线段树+查分

    因为题中的操作是区间加法,所以满足前缀相减性. 而每一次查询的时候还是单点查询,所以直接用可持久化线段树维护差分数组,然后查一个前缀和就行了. code: #include <bits/stdc ...

  7. $spfa-dfs$优化板子

    \(spfa-dfs\)优化板子 快速判断是否存在负环(没负环时不要作死用) bool spfa(int u){ vis[u]=1; for(register int i=head[u];i;i=nx ...

  8. 机器学习---用python实现朴素贝叶斯算法(Machine Learning Naive Bayes Algorithm Application)

    在<机器学习---朴素贝叶斯分类器(Machine Learning Naive Bayes Classifier)>一文中,我们介绍了朴素贝叶斯分类器的原理.现在,让我们来实践一下. 在 ...

  9. 【一起来烧脑】一步React.JS学会体系

    [外链图片转存失败(img-cn4fbVDq-1563575047348)(https://upload-images.jianshu.io/upload_images/11158618-8c6f3d ...

  10. Flower(规律+逆向思维)

    Flower: 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6486 题解: 逆向思维+规律 因为每次剪n-1,所以逆向就是控制n-1朵不变,每次增高1 ...