我的shell编程(四)-数学运算

Bash 数学运算之expr

此篇幅主要讲数学运算

  • 有2种数学运算方法:
语法
方法一 expr $num1 operator $num2
方法二 ( ( (( num1 operator $num2))
  • expr 操作符对照表
操作符 含义
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
num1+num2 求和
num1-num2 求差
num1*num2 求积
num%num2 求余
  • bash 运算之expr

num1=20

num2=100

[root@localhost ~]# num1=13
[root@localhost ~]# num2=14

[root@localhost ~]# expr $num1 > $num2  /不大于,返回0,但是这里没有返回值,没有转义
[root@localhost ~]# echo $?
0
[root@localhost ~]# expr $num1 \> $num2  //不大于,但是返回值0 ,此处转义了
0
[root@localhost ~]# 
[root@localhost ~]# num1=14
[root@localhost ~]# num2=13
[root@localhost ~]# expr $num1 \> $num2  大于,成立,所以返回值1
1
[root@localhost ~]# 

[root@localhost ~]# expr $num1 \>= $num2
1
[root@localhost ~]# 


加减乘除

[root@localhost ~]# num1=10
[root@localhost ~]# num2=14
[root@localhost ~]# num3=`expr $num1*$num2`
[root@localhost ~]# echo $num3
140
[root@localhost ~]# 
[root@localhost ~]# expr $num1 + $num2
24
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# expr $num1 - $num2
-4
[root@localhost ~]# expr $num1 \* $num2   相乘   //此处就需要转义
140
[root@localhost ~]# 
[root@localhost ~]# expr $num1 \% $num2  求余
10
[root@localhost ~]#
[root@localhost ~]# num3=$(($num1*num2))   //在括号里面不需要转义
[root@localhost ~]# echo $num3
140
[root@localhost ~]# 

练习例子:

  • 提示用户输入一个正整数num,然后计算1+2+3+4+……+num的值,必须对num是否为正整数做判断,不符合就让他再次输入

思路分析:正整数就要求 大于0 和整数,那么如何判断是整数了?

[root@localhost ~]# num1=50
[root@localhost ~]# expr $num1 \> 0
1
[root@localhost ~]# num1=51.78
[root@localhost ~]# expr $num1 \> 0  // 虽然大于0了,整数和小数返回的都是1 ,所以还是无法区分整数
1


[root@localhost ~]# expr $num1 + 1  //这里为了区分整数,我们采用 expr $num + 1,如果$num 是小数,就会报错,是整数,就不会报错
expr: non-integer argument
[root@localhost ~]# num1=10
[root@localhost ~]# expr $num1 + 1
11
[root@localhost ~]# 

#!/bin/bash
while true
do
	read -p "Please input positive nummber:" num
	expr $num + 1 &> /dev/null
	if [ $? -eq 0 ];then
		if [ `expr $num \> 0` -eq 1 ];then
			for ((i=1;i<=$num; i++))
			do
				sum=`expr $num + $i`
			done
			echo "1+2+3+4+5+6+……+$num = $sum"
			exit
		fi
	fi
	echo "error,input enleqal"
	continue
done

分析:首先我们输入一个数字,然后使用 expr $num + 1 去判断是不是整数,如果上一步操作正确,就说明是整数,饭后继续判断是不是大于0 ,如果大于0 则返回 数字1 是否是等于1 ,是怎继续循环
i=1;i<$num;i++,然后运算。输出。并且停止循环。如果输入的是错的,则提示非法输入。继续循环,

  • 如何进行浮点数进行运算

数学运算之bc

  • bc是bash内建的运算器,、支持浮点数运算
  • 内建变量scale可以设置,默认为0
操作符 含义
num1+num2 求和
num1-num2 求差
num1/num2 求商
num1*num2 求积
num1%num2 求余
num1^num2 指数运算

首先直接安装bc

[root@localhost ~]# yum -y install bc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package bc.x86_64 0:1.06.95-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================
 Package      Arch             Version                     Repository     Size
===============================================================================
Installing:
 bc           x86_64           1.06.95-13.el7              wyh           115 k

Transaction Summary
===============================================================================
Install  1 Package

Total download size: 115 k
Installed size: 215 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : bc-1.06.95-13.el7.x86_64                                    1/1 
  Verifying  : bc-1.06.95-13.el7.x86_64                                    1/1 

Installed:
  bc.x86_64 0:1.06.95-13.el7                                                   

Complete!
[root@localhost ~]# 

演示:

[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+2
3
23+5
28
24/6
4
24%3
0
24%4
0
24 % 4
0
24 % 3
0
23 % 5
3
3.23456 % 1
.23456

内建变量 scale 默认是0 精确程度

[root@localhost ~]# echo "23+25" | bc
48
[root@localhost ~]# echo "scale=4;23.3/3.5" |bc
6.6571
[root@localhost ~]# 
[root@localhost ~]# echo "scale=4;3.37891/2" |bc
1.6894


脚本部署:

[root@localhost ~]# cat example_4.sh 
#!/bin/bash
#
read -p "num1:" num1
read -p "num2:" num2

num3=`echo "scale=4;$num1/$num2" |bc`

echo "$num1 / $num2 = $num3"
[root@localhost ~]# sh example_4.sh 
num1:5.6
num2:3
5.6 / 3 = 1.8666
[root@localhost ~]#
发布了60 篇原创文章 · 获赞 3 · 访问量 2073

猜你喜欢

转载自blog.csdn.net/weixin_42313749/article/details/103110469