赋值运算

使用=进行赋值运算

算数运算

四则运算

+ - * / % ** 加、减、乘、除、取余、开方

  • 整形运算
    • expr
    • let
    • $(())
    • bc
  • 浮点运算
    • bc ```shell

      expr 命令:只能做整数运算

      expr 1 + 1 2

let命令:只能做整数运算,且运算元素必须是变量,无法直接对整数做运算

let a=100+3;echo $a 103

双小圆括号运算,在shell中(( ))也可以用来做数学运算

echo $((6 / 3)) 2

浮点运算是采用的命令组合的方式来实现的 echo “scale=N;表达式”|bc

echo “scale=2;100/3”|bc

  1. <a name="y46Yy"></a>
  2. #### 练习1
  3. 简单的计算器
  4. ```shell
  5. #!usr/bin/bash
  6. #
  7. # Filename: calculator.sh
  8. # Author: HelloChen
  9. # CreatTime: 2021-1-13 16:42:26
  10. # Description: calculator
  11. echo "$1 $2 $3"|bc
  12. ------------------------------------
  13. # root @ Tenyun in /home/Shell [16:47:46]
  14. $ zsh ./calculator.sh 2 + 2
  15. 4
  16. # root @ Tenyun in /home/Shell [16:48:15]
  17. $ zsh ./calculator.sh 2 - 2
  18. 0
  19. # root @ Tenyun in /home/Shell [16:48:19]
  20. $ zsh ./calculator.sh 2 \* 2
  21. 4
  22. # root @ Tenyun in /home/Shell [16:48:22]
  23. $ zsh ./calculator.sh 2 / 2
  24. 1

练习2

打印内存使用率

  1. #!usr/bin/bash
  2. #
  3. # Filename: memory_use.sh
  4. # Author: HelloChen
  5. # CreatTime: 2021-1-13 16:42:26
  6. # Description: Print memory usage
  7. # 获取内存总量
  8. memory_total=`free -m|grep -i "mem"|tr -s " "|cut -d " " -f2`
  9. # 获取内存使用量
  10. memory_use=`free -m|grep -i "mem"|tr -s " "|cut -d " " -f3`
  11. # 计算输出
  12. echo "内存使用率: `echo "scale=2;$memory_use*100/$memory_total"|bc`%"

比较运算

整型比较符

  1. 精确比较
  2. -eq 等于 equal
  3. -gt 大于
  4. -lt 小于
  5. 模糊比较
  6. -ge 大于或等于
  7. -le 小于或等于
  8. -ne 不等于

使用test命令比较两个整数的关系

  1. # root @ Tenyun in /home/Shell [17:03:37]
  2. $ test 100 -eq 100;echo $?
  3. 0
  4. # root @ Tenyun in /home/Shell [17:03:46]
  5. $ test 100 -eq 200;echo $?
  6. 1
  7. 备注:linux命令test只能比较两个整数的关系,不会返回结果,需要通过$?才能看到结果

练习3

两个整数比较大小

  1. #!usr/bin/bash
  2. #
  3. # Filename: comparison.sh
  4. # Author: HelloChen
  5. # CreatTime: 2021-1-13 16:42:26
  6. # Description: Comparison of size
  7. #1、输入两个数,$1 $2
  8. #2、判断两个数关系
  9. if [ $1 -gt $2 ];then
  10. #3、输出结果
  11. echo "$1 > $2 "
  12. elif [ $1 -eq $2 ];then
  13. echo "$1 = $2"
  14. else
  15. echo "$1 < $2"
  16. fi
  17. -----------------------------------------
  18. # root @ Tenyun in /home/Shell [17:30:05]
  19. $ zsh comparison.sh 1 2
  20. 1 < 2
  21. # root @ Tenyun in /home/Shell [17:30:19]
  22. $ zsh comparison.sh 5 2
  23. 5 > 2
  24. # root @ Tenyun in /home/Shell [17:30:23]
  25. $ zsh comparison.sh 5 5
  26. 5 = 5

1465.gif

浮点型判断

默认情况下shell是不能判断浮点的,那么在linux中又避免不了需要进行浮点运算,那怎么解决
解决思路如下:

  1. 两个数据同时放大到整数倍
  2. 处理掉小数点位,保留整数位
  3. 进行整形判断

    练习4

    两个浮点型数比较 ```shell

    !usr/bin/bash

    #

    Filename: comparison.sh

    Author: HelloChen

    CreatTime: 2021-1-13 17:42:26

    Description: Floating point comparison

1、交互或者外传参的方式获得两个整数

$1 $2

[ $# -lt 2 ]&&echo “need two args”&&exit 1

采用外传参的方式接收数据并放大100倍,并处理为整数

num1=echo "scale=2;$1*100"|bc|cut -d "." -f1 num2=echo "scale=2;$2*100"|bc|cut -d "." -f1

2、比较运算

if [ $num1 -gt $num2 ];then

3、输出结果

echo “$1 > $2” elif [ $num1 -lt $num2 ];then echo “$1 < $2” else echo “$1 = $2” fi

  1. ![897.gif](https://cdn.nlark.com/yuque/0/2021/gif/1404781/1610531088307-1f2731ce-5701-4532-ae35-bf46b1989533.gif#align=left&display=inline&height=1026&margin=%5Bobject%20Object%5D&name=897.gif&originHeight=1026&originWidth=1703&size=10844479&status=done&style=none&width=1703)
  2. <a name="lsFnp"></a>
  3. ### 字符串比较运算
  4. 运算符解释,注意字符串一定别忘了使用引号引起来<br /> == 等于 <br /> != 不等于<br /> -n 检查字符串的长度是否大于0 <br /> -z 检查字符串的长度是否为0
  5. ```shell
  6. # root @ Tenyun in /home/Shell [17:47:44] C:1
  7. $ test 'root' \=\= 'root';echo $?
  8. 0
  9. # root @ Tenyun in /home/Shell [17:47:48]
  10. $ test 'root' \!= 'root';echo $?
  11. 1
  12. # root @ Tenyun in /home/Shell [17:48:06]
  13. $ name=
  14. # root @ Tenyun in /home/Shell [17:48:24]
  15. $ test -n "$name";echo $?
  16. 1
  17. # root @ Tenyun in /home/Shell [17:48:46]
  18. $ test -z "$name";echo $?
  19. 0

练习5

模拟一个linux文本界面登陆程序,要求账号密码验证成功进入系统,账号密码验证失败退回登陆界面

  1. #!usr/bin/bash
  2. #
  3. # Filename: check_login.sh
  4. # Author: HelloChen
  5. # CreatTime: 2021-1-13 17:42:26
  6. # Description: check login
  7. ####
  8. default_account='root'
  9. default_pw='123456'
  10. ######main
  11. #1、清屏
  12. clear
  13. #2、输出提示信息
  14. echo "CentOS Linux 8 (Core)"
  15. echo -e "Kernel `uname -r` on an `uname -m`\n"
  16. #3、交互输入登陆名
  17. echo -n "$HOSTNAME login: "
  18. read account
  19. #4、交互输入密码
  20. read -s -t30 -p "Password: " pw
  21. #5、判断用户输入是否正确
  22. if [ "$default_account" == "$account" ] && [ "$default_pw" == "$pw" ];then
  23. clear
  24. echo -e "\nwelcome to root"
  25. else
  26. echo "用户名或密码错误..."
  27. #输入错误,再次调用本脚本
  28. sh $0
  29. fi

9879.gif

逻辑运算

  • 逻辑与运算 &&
  • 逻辑或运算 ||
  • 逻辑非运算 !

    练习6

    使用逻辑运算写一个仿真用户登录验证程序 ```shell

    !/bin/bash

    echo “CentOS linux 8 (Core)” echo -e “Kernel uname -r on an uname -m \n”

1、输入用户名

echo -n “$HOSTNAME login: “ read myuser

echo -n “password: “

read -s -t 5 -n 2 pw

2、输入密码

read -p “password: “ -s -t 5 -n 6 pw

3、与运算返回结果

[ $myuser == ‘root’ ] && [ $pw == ‘123456’ ] && echo yes || echo no ```

文件判断(文件类型、权限、新旧判断)