linux Shell 命令行-04-operator Shell 操作符

拓展阅读

linux Shell 命令行-00-intro 入门介绍

linux Shell 命令行-02-var 变量

linux Shell 命令行-03-array 数组

linux Shell 命令行-04-operator 操作符

linux Shell 命令行-05-test 验证是否符合条件

linux Shell 命令行-06-flow control 流程控制

linux Shell 命令行-07-func 函数

linux Shell 命令行-08-file include 文件包含

linux Shell 命令行-09-redirect 重定向

操作符

原始的 bash 不支持数学运算,我们可以使用 expr 来代替。

注意:

  1. expr中使用 ` ,而不是 '
  2. 数字之间的运算符应该用空格分隔。

数字运算

  • num_oper.sh
shell 复制代码
#!/bin/bash

# 数值操作

value1=10
value2=20
sum=`expr $value1 + $value2`
echo "数值 $value1 和 $value2 的和是: $sum"
  • 运行
shell 复制代码
houbinbindeMacBook-Pro:shell houbinbin$ chmod +x num_oper.sh
houbinbindeMacBook-Pro:shell houbinbin$ ./ num_oper.sh
数值 10 和 20 的和是: 30

数学运算符

假设: $a 是 10,$b 是 20

命令 描述 示例
+ 加法 expr $a + $b 结果为 30
- 减法 expr $a - $b 结果为 -10
* 乘法 expr $a \* $b 结果为 200
/ 除法 expr $b / $a 结果为 2
% 取余 expr $b % $a 结果为 0
= 赋值 a=$b 将把变量 b 的值赋给 a
== 用于比较两个数字,相同则返回 true [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a = = a == </math>a==b ] 返回 false
!= 用于比较两个数字,不相同则返回 true [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a ! = a != </math>a!=b ] 返回 true

关系运算符

仅支持数字或数字字符串。

假设: $a 是 10,$b 是 20

命令 描述 示例
-eq 相等 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − e q a -eq </math>a−eqb ] false
-ne 不相等 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − n e a -ne </math>a−neb ] true
-gt 大于 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − g t a -gt </math>a−gtb ] false
-lt 小于 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − l t a -lt </math>a−ltb ] true
-ge 大于等于 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − g e a -ge </math>a−geb ] false
-le 小于等于 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a − l e a -le </math>a−leb ] true

布尔运算符

| 命令 | 描述 | 示例 |
|:-----|:---|:---------------------------|---|----------------------------|
| ! | 非 | [ ! false ] true |
| -o | 或 | [ true -o false ] true |
| -a | 与 | [ true and false ] false |
| && | 与 | [ true and false ] false |
| ` | | ` | 或 | [ true and false ] false |

字符串运算符

假设: $a 是 "10",$b 是 "20"

命令 描述 示例
= 检测两个字符串是否相等,相等返回 true [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a = a = </math>a=b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a ! = a != </math>a!=b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true [ -z $a ] 返回 false。
-n 检测字符串长度是否为0,不为0返回 true [ -n $a ] 返回 true。
str 检测字符串是否为空,不为空返回 true [ $a ] 返回 true。

文件测试运算符

命令 描述
-b file 检测文件是否是块设备文件,如果是,则返回 true
-c file 检测文件是否是字符设备文件,如果是,则返回 true
-d file 检测文件是否是目录,如果是,则返回 true
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true
-p file 检测文件是否是具名管道,如果是,则返回 true
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true
-r file 检测文件是否可读,如果是,则返回 true
-w file 检测文件是否可写,如果是,则返回 true
-x file 检测文件是否可执行,如果是,则返回 true
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true
-e file 检测文件(包括目录)是否存在,如果是,则返回 true
  • file_test_oper.sh
shell 复制代码
#!/bash/sh

# 文件测试运算符

file="/Users/houbinbin/code/shell/file_test_oper.sh"

if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi
if [ -w $file ]
then
   echo "文件可写"
else
   echo "文件不可写"
fi
if [ -x $file ]
then
   echo "文件可执行"
else
   echo "文件不可执行"
fi
if [ -f $file ]
then
   echo "文件为普通文件"
else
   echo "文件为特殊文件"
fi
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi
if [ -s $file ]
then
   echo "文件不为空"
else
   echo "文件为空"
fi
if [ -e $file ]
then
   echo "文件存在"
else
   echo "文件不存在"
fi
  • 运行
shell 复制代码
houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh file_test_oper.sh
文件可读
文件可写
文件不可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在

参考资料

www.runoob.com/linux/linux...

本文由博客一文多发平台 OpenWrite 发布!

相关推荐
JANYI201816 分钟前
嵌入式设计模式基础--C语言的继承封装与多态
java·c语言·设计模式
xrkhy24 分钟前
反射, 注解, 动态代理
java
Ten peaches36 分钟前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
lyw2056191 小时前
RabbitMQ,Kafka八股(自用笔记)
java
邹诗钰-电子信息工程1 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
有梦想的攻城狮1 小时前
spring中的@MapperScan注解详解
java·后端·spring·mapperscan
寒小松1 小时前
Problem E: List练习
java·数据结构·list
zimoyin1 小时前
Kotlin 协程实战:实现异步值加载委托,对值进行异步懒初始化
java·前端·kotlin
柚个朵朵2 小时前
Spring的Validation,这是一套基于注解的权限校验框架
java·后端·spring
2301_803554522 小时前
c++和c的不同
java·c语言·c++