一、变量的数值计算

1.算术运算符

常用的运算符号

常用的运算命令

(1)双小括号
基本语法

1)利用“(())”进行简单运算

[root@codis-178 ~]# echo $((1+1))
2
[root@codis-178 ~]# echo $((6-3))
3
[root@codis-178 ~]# ((i=5))
[root@codis-178 ~]# ((i=i*2))
[root@codis-178 ~]# echo $i
10

2)利用“(())”进行复杂运算

[root@codis-178 ~]# ((a=1+2**3-4%3))
[root@codis-178 ~]# echo $a
8
[root@codis-178 ~]# b=$((1+2**3-4%3))
[root@codis-178 ~]# echo $b
8
[root@codis-178 ~]# echo $((1+2**3-4%3))
8
[root@codis-178 ~]# a=$((100*(100+1)/2))
[root@codis-178 ~]# echo $a
5050
[root@codis-178 ~]# echo $((100*(100+1)/2))
5050

3)特殊运算符的运算

[root@codis-178 ~]# a=8
[root@codis-178 ~]# echo $((a=a+1))
9
[root@codis-178 ~]# echo $((a+=1))
10
[root@codis-178 ~]# echo $((a**2))
100

4)比较和判断

[root@codis-178 ~]# echo $((3<8))
1
[root@codis-178 ~]# echo $((8<3))
0
[root@codis-178 ~]# echo $((8==8))
1
[root@codis-178 ~]# if ((8>7&&5==5))
> then
> echo yes
> fi
yes

5)在变量前后使用--和++特殊运算符的表达式

[root@codis-178 ~]# a=10
[root@codis-178 ~]# echo $((a++))
10
[root@codis-178 ~]# echo $a
11
[root@codis-178 ~]# a=11
[root@codis-178 ~]# echo $((a--))
11
[root@codis-178 ~]# echo $a
10
[root@codis-178 ~]# a=10
[root@codis-178 ~]# echo $a
10
[root@codis-178 ~]# echo $((--a))
9
[root@codis-178 ~]# echo $((++a))
10
[root@codis-178 ~]# echo $a
10

6)通过“(())”运算后赋值给变量

[root@codis-178 ~]# myvar=99
[root@codis-178 ~]# echo $((myvar+1))
100
[root@codis-178 ~]# echo $((   myvar+1   ))
100
[root@codis-178 ~]# myvar=$((myvar+1))
[root@codis-178 ~]# echo $myvar
100

7)包含“(())”的各种常见运算

[root@codis-178 ~]# echo $((6+2))
8
[root@codis-178 ~]# echo $((6-2))
4
[root@codis-178 ~]# echo $((6*2))
12
[root@codis-178 ~]# echo $((6/2))
3
[root@codis-178 ~]# echo $((6%2))
0
[root@codis-178 ~]# echo $((6**2))
36

8)在shell脚本中的示例

[root@codis-178 ~]# cat test.sh
#!/bin/basha=6
b=2echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"[root@codis-178 ~]# sh test.sh
a-b=4
a+b=8
a*b=12
a/b=3
a**b=36
a%b=0

9)将上面的脚本中a、b两个变量通过命令行脚本传参,以实现混合运算

[root@codis-178 ~]# cat test.sh
#!/bin/basha=$1
b=$2echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"[root@codis-178 ~]# sh test.sh 8 3
a-b=5
a+b=11
a*b=24
a/b=2
a**b=512
a%b=2[root@codis-178 ~]# sh test.sh 3 6
a-b=-3
a+b=9
a*b=18
a/b=0
a**b=729
a%b=3

10)实现输入2个数进行运算的计算器

[root@codis-178 ~]# cat yunsuan.sh
#!/bin/bashprint_usage(){printf "Please enter an integer\n"exit 1
}
read -p "Please input first number: " firstnum
if [ -n "`echo $firstnum|sed 's/[0-9]//g'`" ];thenprint_usage
fi
read -p "Please input the operators: " operators
if [ "${operators}" != "+" ] && [ "${operators}" != "-" ] && [ "${operators}" != "*" ] && [ "${operators}" != "/" ];thenecho "please use (+|-|*|/)"exit 2
fi
read -p "Please input second number: " secondnum
if [ -n "`echo $secondnum|sed 's/[0-9]//g'`" ];thenprintf_usage
fi
echo "${firstnum}${operators}${secondnum}=$((${firstnum}${operators}${secondnum}))"
[root@codis-178 ~]# sh yunsuan.sh
Please input first number: 5
Please input the operators: +
Please input second number: 16
5+16=21
[root@codis-178 ~]# sh yunsuan.sh
Please input first number: 9
Please input the operators: *
Please input second number: 24
9*24=216
[root@codis-178 ~]# sh yunsuan.sh
Please input first number: 76
Please input the operators: -
Please input second number: 24
76-24=52

改良版

[root@codis-178 ~]# cat yunsuan1.sh
#!/bin/bashprint_usage(){printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"exit 1
}
if [ $# -ne 3 ]thenprint_usage
fi
firstnum=$1
secondnum=$3
op=$2
if [ -n "`echo $firstnum|sed 's/[0-9]//g'`" ];thenprint_usage
fi
if [ "$op" != "+" ] && [ "$op" != "-" ] && [ "$op" != "*" ] && [ "$op" != "/" ]thenprint_usage
fi
if [ -n "`echo $secondnum|sed 's/[0-9]//g'`" ];thenprintf_usage
fi
echo "${firstnum}${op}${secondnum}=$((${firstnum}${op}${secondnum}))"
[root@codis-178 ~]# sh yunsuan1.sh 6 + 8
6+8=14
[root@codis-178 ~]# sh yunsuan1.sh 56 + 13
56+13=69
[root@codis-178 ~]# sh yunsuan1.sh 56 - 13
56-13=43[root@codis-178 ~]# sh yunsuan1.sh 2 * 2
USAGE:yunsuan1.sh NUM1 {+|-|*|/} NUM2
[root@codis-178 ~]# sh yunsuan1.sh 9 / 3
9/3=3
[root@codis-178 ~]# sh yunsuan1.sh 2 \* 2  # 乘号需要转义
2*2=4

(2)let运算命令
1)给自变量加8

[root@codis-178 ~]# i=2
[root@codis-178 ~]# i=i+8
[root@codis-178 ~]# echo $i
i+8
[root@codis-178 ~]# unset i
[root@codis-178 ~]# i=2
[root@codis-178 ~]# let i=i+8
[root@codis-178 ~]# echo $i
10

2)监控Web服务状态,如果访问两次均失败,则报警

[root@codis-178 ~]# cat web.sh
#!/bin/bash
CheckUrl(){timeout=5fails=0success=0while truedowget --timeout=$timeout --tries=1 http://oldboy.blog.51cto.com -q -O /dev/nullif [ $? -ne 0 ]thenlet fails=fails+1elselet success+=1fiif [ $success -ge 1 ]thenecho successexit 0fiif [ $fails -ge 2 ]thenCritical="sys is down."echo $Critical|tee|mail -s "$Critical" tongxiaoda@anzhi.comexit 2fidone
}
CheckUrl
[root@codis-178 ~]# sh web.sh
success

(3)expr命令
1)expr用于计算

[root@codis-178 ~]# expr 2 + 2
4
[root@codis-178 ~]# expr 2 - 2
0
[root@codis-178 ~]# expr 2 * 2  # 需要转义
expr: syntax error
[root@codis-178 ~]# expr 2 \* 2
4
[root@codis-178 ~]# expr 2 / 2
1

2)expr配合变量计算

[root@codis-178 ~]# i=5
[root@codis-178 ~]# i=`expr $i + 6`
[root@codis-178 ~]# echo $i
11

3)判断一个变量或字符串是否为整数

[root@codis-178 ~]# i=5
[root@codis-178 ~]# expr $i + 6 &>/dev/null
[root@codis-178 ~]# echo $?
0
[root@codis-178 ~]# i=oldboy
[root@codis-178 ~]# expr $i + 6 &>/dev/null
[root@codis-178 ~]# echo $?
2

4)通过传参判断输出内容是否为整数

[root@codis-178 ~]# cat expr.sh
#!/bin/bash
expr $1 + 1 > /dev/null 2>&1
[ $? -eq 0 ] && echo int || echo chars
[root@codis-178 ~]# sh expr.sh oldboy
chars
[root@codis-178 ~]# sh expr.sh 119
int

5)通过read读入持续等待输入

[root@codis-178 ~]# cat judge_int.sh
#!/bin/bash
while true
doread -p "Pls input:" aexpr $a + 0 >/dev/null 2>&1[ $? -eq 0 ] && echo int || echo chars
done
[root@codis-178 ~]# sh judge_int.sh
Pls input:oldgirl
chars
Pls input:76
int
Pls input:76sd
chars

6)通过expr判断文件扩展名是否符合要求

[root@codis-178 ~]# cat expr1.sh
#!/bin/bash
if expr "$1" : ".*\.pub" &>/dev/nullthenecho "you are using $1"
elseecho "Pls use *.pub file"
fi
[root@codis-178 ~]# sh expr1.sh ttt.pub
you are using ttt.pub
[root@codis-178 ~]# sh expr1.sh ttt.py
Pls use *.pub file

7)使用expr命令实现ssh服务自带的ssh-copy-id公钥分发脚本

[root@codis-178 ~]# sed -n '10,20p' which /usr/bin/ssh-copy-id
sed: can't read which: No such file or directory
if [ "-i" = "$1" ]; thenshift# check if we have 2 parameters left, if so the first is the new ID fileif [ -n "$2" ]; thenif expr "$1" : ".*\.pub" > /dev/null ; thenID_FILE="$1"elseID_FILE="$1.pub"fishift         # and this should leave $1 as the target namefi

8)通过expr计算字符串长度

[root@codis-178 ~]# char="I am oldboy"
[root@codis-178 ~]# expr length "$char"
11
[root@codis-178 ~]# echo ${char}
I am oldboy
[root@codis-178 ~]# echo ${char}|wc -L
11
[root@codis-178 ~]# echo ${char}|awk '{print length($0)}'
11

9)请编写shell脚本,打印下面语句中字符数不大于6的单词
I am oldboy linux welcome to our training

[root@codis-178 ~]# cat word_length.sh
#!/bin/bash
for n in I am oldboy linux welcome to our training
doif [ `expr length $n` -le 6 ]thenecho $nfi
done
[root@codis-178 ~]# sh word_length.sh
I
am
oldboy
linux
to
our

(4)bc命令
Linux内部计算器

[root@codis-178 ~]# 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+1
2
3*3
9

1)将bc用在命令行实现运算

[root@codis-178 ~]# echo 3+5|bc
8
[root@codis-178 ~]# echo 3.3+5.6|bc
8.9
[root@codis-178 ~]# echo 7.28-5.62|bc
1.66
[root@codis-178 ~]# echo "scale=2;355/113"|bc  # scale保留几位小数
3.14
[root@codis-178 ~]# echo "scale=4;355/113"|bc
3.1415
[root@codis-178 ~]# i=5
[root@codis-178 ~]# i=`echo $i+6|bc`
[root@codis-178 ~]# echo $i
11

2)通过一条命令计算输出1+2+3+...+10的表达式,并计算结果

[root@codis-178 ~]# echo `seq -s '+' 10`=`seq -s "+" 10|bc`
1+2+3+4+5+6+7+8+9+10=55
[root@codis-178 ~]# echo `seq -s '+' 10`=$((`seq -s "+" 10`))
1+2+3+4+5+6+7+8+9+10=55
[root@codis-178 ~]# echo `seq -s "+" 10`=`seq -s " + " 10|xargs expr`
1+2+3+4+5+6+7+8+9+10=55

(5)awk实现计算

[root@codis-178 ~]# echo "7.73 3.854" |awk '{print ($1-$2)}'
3.876
[root@codis-178 ~]# echo "367 131" |awk '{print ($1-3)/$2}'
2.77863

(6)$[]符号的运算

[root@codis-178 ~]# i=5
[root@codis-178 ~]# i=$[i+6]
[root@codis-178 ~]# echo $i
11
[root@codis-178 ~]# echo $[2*3]
6
[root@codis-178 ~]# echo $[3%5]
3

打印数学杨辉三角

[root@codis-178 ~]# cat shuxue.sh
#!/bin/bash
if (test -z $1);thenread -p "Input Max Lines:" MAX
elseMAX=$1
fi
i=1
while [ $i -le $MAX ]
doj=1while [ $j -le $i ]dof=$[i-1]g=$[j-1]if [ $j -eq $i ] || [ $j -eq 1 ];thendeclare SUM_${i}_$j=1elsedeclare A=$[SUM_${f}_$j]declare B=$[SUM_${f}_$g]declare SUM_${i}_$j=`expr $A + $B`fiecho -en $[SUM_${i}_$j]" "let j++doneecholet i++
done[root@codis-178 ~]# sh shuxue.sh
Input Max Lines:4
1
1 1
1 2 1
1 3 3 1

2.基于Shell变量输入read命令的运算

(1)read命令
语法格式:read [参数] [变量名]
参数:

  • -p prompt 设置提示信息
  • -t timeout 设置输入等待时间,单位秒
[root@codis-178 ~]# read -t 10 -p "Pls input one num:" num
Pls input one num:18
[root@codis-178 ~]# echo $num
18
[root@codis-178 ~]# read -t 10 -p "Pls input two num:" a1 a2
Pls input two num:5 6
[root@codis-178 ~]# echo $a1
5
[root@codis-178 ~]# echo $a2
6

(2)以read命令读取及传参的综合案例

[root@codis-178 ~]# cat read_size01.sh
#!/bin/bash
read -t 15 -p "Please input two number:" a b[ ${#a} -le 0 ] && {echo "the first num is null"exit 1
}
[ ${#b} -le 0 ] && {echo "the second num is null"exit 1
}expr $a + 1 &>/dev/null
RETVAL_A=$?
expr $b + 1 &>/dev/null
RETVAL_B=$?
if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ];thenecho "one of the num is not num,pls input again."exit 1
fiecho "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@codis-178 ~]# sh read_size01.sh
Please input two number:qq
the second num is null
[root@codis-178 ~]# sh read_size01.sh
Please input two number:12 6
a-b=6
a+b=18
a*b=72
a/b=2
a**b=2985984
a%b=0

通过传参方式

[root@codis-178 ~]# cat read_size02.sh
#!/bin/bash
a=$1
b=$2
Usage(){echo $"USAGE:sh $0 num1 num2"exit 1
}
if [ $# -ne 2 ];thenUsage
fi
expr $a + 1 >/dev/null 2>&1
[ $? -ne 0 ] && Usageecho "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a**b=$(($a**$b))"
if [ $b -eq 0 ]thenecho "your input does not allow to run"echo "a/b =error"echo "a%b =error"
elseecho "a/b =$(($a/$b))" echo "a%b =$(($a%$b))"
fi[root@codis-178 ~]# sh read_size02.sh
USAGE:sh read_size02.sh num1 num2
[root@codis-178 ~]# sh read_size02.sh 7 old
USAGE:sh read_size02.sh num1 num2
[root@codis-178 ~]# sh read_size02.sh 7 3
a-b=4
a+b=10
a*b=21
a**b=343
a/b =2
a%b =1

转载于:https://www.cnblogs.com/tongxiaoda/p/7454566.html

Shell编程之运算相关推荐

  1. shell编程入门、shell编程的基础知识(变量、命令、运算)、shell编程的语句

    shell编程 示例1 ex1 文件内容如下: #!/bin/sh #This is to show what a example looks like. echo "My First Sh ...

  2. shell编程之数值运算

    shell编程是Linux学习中的难点,很多人学了几个月也是不明就里,那么今天我带着大家看一看,shell编程里面的数值运算 Shell 编程中的基本数值运算,这类运算包括: 数值(包括整数和浮点数) ...

  3. 如让自己想学不好shell编程都困难?

    众所周知,shell是linux运维必备的技术,必须要掌握,但是shell语法复杂,灵活,网友掌握了语法也不知道如何应用到实际运维中,老男孩培训shell编程给所有linux运维人员带来了学好shel ...

  4. Linux学习(十四)---大数据定制篇Shell编程

    文章目录 一.为什么要学习shell编程 二.Shell 是什么 三.shell 编程快速入门-Shell 脚本的执行方式 3.1 脚本格式要求 3.2 编写第一个 Shell 脚本 3.3 脚本的常 ...

  5. Bourne Shell及shell编程

    Bourne Shell及shell编程<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office ...

  6. linux shell let命令,shell编程中的let与(())

    let与(()) 在shell编程中是可以互换的:它们在循环语句中控制变量变化非常有用: 使用let语句或者(())我们可以像C语言那样写程序~ 对于变量赋值,判断什么的不用繁琐的$VAR, -eq等 ...

  7. Linux Shell编程基础

    linux系统下给命令指定别名alias命令用法: 在linux系统中如果命令太长又不符合用户的习惯,那么我们可以为它指定一个别名.虽然可以为命令建立"链接"解决长文件名的问题,但 ...

  8. Linux Shell 编程学习总结

    Shell 教程 Shell简介:什么是Shell,Shell命令的两种执行方式 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成 ...

  9. linux shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select 学习笔记

    SHELL编程一UNIX和Shell工具简介 什么是shell? shell只是一个程序,它在系统中没有特权.因此,有多个不同风格shell共同存在原因--Bourne Shell,Korn Shel ...

最新文章

  1. android 蓝牙数据分包_无线组网技术谁能问鼎云巅-蓝牙Mesh, ZIGBEE, THREAD
  2. 学 Win32 汇编[12]: PTR、OFFSET、ADDR、THIS
  3. 《OpenGL ES 2.0游戏开发(上卷):基础技术和典型案例》一6.6 本章小结
  4. router3 BGP2 属性及选路
  5. Selenium对于对话框alert,confirm,prompt的处理
  6. 欢迎使用CSDN-markdown编辑器333333
  7. python3颜色代码_python3中布局背景颜色代码分析
  8. 禁止选择文字和文本超出显示省略号
  9. html css 自动滚动代码,使用CSS自动滚动
  10. HDU2090 算菜价【水题】
  11. 怎么判断网络回路_地暖管漏水怎么办?一打、二看、三确定,及时查出地暖管漏水点!...
  12. 如何在Mac电脑中设置投屏?Mac投屏,Mac电脑无线投屏教程
  13. Latex中PDF文档目录乱码解决方案
  14. springboot 删除路径下面所有文件_[原创]springboot 中 resources 资源目录里面的文件夹压缩下载...
  15. 2021秋软工实践第一次个人编程作业
  16. 基于 PCA 的人脸识别系统及人脸姿态分析
  17. 如何轻松学习C语言编程!
  18. 【问题记录】js 更改数组中某字段名
  19. 计算机如何用蓝牙实现文件传输,Win10系统电脑通过蓝牙进行传输文件操作步骤...
  20. java基础面试题及答案

热门文章

  1. python 两点曲线_python机器学习分类模型评估
  2. 河北工程大学c语言期末考试及答案,河北工程大学之数据结构c语言版期末考试复习试题...
  3. 两个形状不同的长方形周长_人教版数学六年级上册 5.2:圆的周长 微课视频|知识点|课件解析|同步练习...
  4. 使用pagehelper踩的坑PageHelper cannot be cast
  5. vnx 服务器映射,EMC VNX5200/5400存储 新增LUN与Hosts映射操作(示例代码)
  6. Execution default of goal org.springframework.boot:spring-boot-maven-plugin
  7. php简单验证码实例,php结合GD库简单实现验证码的示例代码
  8. 计算机网关,如何查看计算机的IP地址和网关
  9. excel两个表格数据对比_Excel表格技巧—如何统计数据个数
  10. oracle表段是什么,【DB笔试面试274】在Oracle中,什么是延迟段创建(Deferred Segment Crea......