有各种不同的运算符shell都支持。本教程是基于默认shell(Bourne),所以我们要涵盖所有重要的Bourne Shell运算符。
有以下的运算符,我们将要讨论的:
-
算术运算符。
-
关系运算符。
-
布尔运算符。
-
字符串运算符。
-
文件测试操作。
Bourne shell的最初并没有任何机制来执行简单的算术,但它使用外部程序,无论是awk或必须简单的程序expr。
下面是简单的例子,把两个数相加:
#!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
这将产生以下结果:
Total value : 4
记下有以下几点:
算术运算符:
算术运算符有以下Bourne Shell支持。
假设变量a=10,变量b=20:
算术运算符例子
运算符
描述
例子
+ |
Addition - Adds values on either side of the operator |
`expr $a + $b` will give 30 |
- |
Subtraction - Subtracts right hand operand from left hand operand |
`expr $a - $b` will give -10 |
* |
Multiplication - Multiplies values on either side of the operator |
`expr $a \* $b` will give 200 |
/ |
Division - Divides left hand operand by right hand operand |
`expr $b / $a` will give 2 |
% |
Modulus - Divides left hand operand by right hand operand and returns remainder |
`expr $b % $a` will give 0 |
= |
Assignment - Assign right operand in left operand |
a=$b would assign value of b into a |
== |
Equality - Compares two numbers, if both are same then returns true. |
[ $a == $b ] would return false. |
!= |
Not Equality - Compares two numbers, if both are different then returns true. |
[ $a != $b ] would return true. |
这是非常重要的,这里要注意,所有的条件式将放在方括号内,他们身边有一个空格,例如 [ $a == $b ]是正确的,为[$a==$b]是不正确的。
所有的算术计算,使用长整数。
关系运算符:
Bourne Shell的支持,关系运算符的具体数值。这些运算符不能使用字符串值,除非它们的值是数字。
例如,运算符将努力检查10和20之间的关系,以及在“10”和“20”,但不是“10”和“21”之间。
假设变量a=10,变量b=20:
关系运算符
运算符
描述
示例
-eq |
Checks if the value of two operands are equal or not, if yes then condition becomes true. |
[ $a -eq $b ] is not true. |
-ne |
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
[ $a -ne $b ] is true. |
-gt |
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
[ $a -gt $b ] is not true. |
-lt |
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
[ $a -lt $b ] is true. |
-ge |
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
[ $a -ge $b ] is not true. |
-le |
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
[ $a -le $b ] is true. |
这里要注意,所有的条件式将放在方括号内,他们周围有一个空格,这是非常重要的,例如[ $a <= $b ]是正确的,[$a <= $b]是不正确的。
布尔运算:
布尔运算符有以下Bourne Shell的支持。
假设变量一个变量b=10,然后变量b=20:
布尔运算示例
运算符
描述
示例
! |
This is logical negation. This inverts a true condition into false and vice versa. |
[ ! false ] is true. |
-o |
This is logical OR. If one of the operands is true then condition would be true. |
[ $a -lt 20 -o $b -gt 100 ] is true. |
-a |
This is logical AND. If both the operands are true then condition would be true otherwise it would be false. |
[ $a -lt 20 -a $b -gt 100 ] is false. |
字符串运算符:
有下列字符串运算由Bourne Shell支持。
假设变量a=“abc”和变量b=“efg”:
关系运算例子
运算符
描述
例子
= |
Checks if the value of two operands are equal or not, if yes then condition becomes true. |
[ $a = $b ] is not true. |
!= |
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
[ $a != $b ] is true. |
-z |
Checks if the given string operand size is zero. If it is zero length then it returns true. |
[ -z $a ] is not true. |
-n |
Checks if the given string operand size is non-zero. If it is non-zero length then it returns true. |
[ -z $a ] is not false. |
str |
Check if str is not the empty string. If it is empty then it returns false. |
[ $a ] is not false. |
文件测试操作:
有以下是操作测试Unix文件相关联的各种属性。
假设一个的变量文件保存现有文件名“test”,其大小为100字节,有读,写和执行权限:
文件测试操作例子
操作符
描述
示例
-b file |
Checks if file is a block special file if yes then condition becomes true. |
[ -b $file ] is false. |
-c file |
Checks if file is a character special file if yes then condition becomes true. |
[ -b $file ] is false. |
-d file |
Check if file is a directory if yes then condition becomes true. |
[ -d $file ] is not true. |
-f file |
Check if file is an ordinary file as opposed to a directory or special file if yes then condition becomes true. |
[ -f $file ] is true. |
-g file |
Checks if file has its set group ID (SGID) bit set if yes then condition becomes true. |
[ -g $file ] is false. |
-k file |
Checks if file has its sticky bit set if yes then condition becomes true. |
[ -k $file ] is false. |
-p file |
Checks if file is a named pipe if yes then condition becomes true. |
[ -p $file ] is false. |
-t file |
Checks if file descriptor is open and associated with a terminal if yes then condition becomes true. |
[ -t $file ] is false. |
-u file |
Checks if file has its set user id (SUID) bit set if yes then condition becomes true. |
[ -u $file ] is false. |
-r file |
Checks if file is readable if yes then condition becomes true. |
[ -r $file ] is true. |
-w file |
Check if file is writable if yes then condition becomes true. |
[ -w $file ] is true. |
-x file |
Check if file is execute if yes then condition becomes true. |
[ -x $file ] is true. |
-s file |
Check if file has size greater than 0 if yes then condition becomes true. |
[ -s $file ] is true. |
-e file |
Check if file exists. Is true even if file is a directory but exists. |
[ -e $file ] is true. |
分享到:
相关推荐
算术运算符是shell运算符中最基本的一类,包括加、减、乘、除、模等运算符。这些运算符可以用来进行基本的数学运算,例如: $ echo $((10+40)) # 输出50 $ echo $((5*(3+3))) # 输出30 在shell脚本中,可以使用...
### Linux Shell 逻辑运算符详解 #### 一、引言 Linux Shell 是一种强有力的脚本语言,用于控制和管理各种操作系统任务。其中,逻辑运算符是进行条件判断的基础,广泛应用于自动化脚本、条件判断语句中。通过合理...
在深入探讨Linux Shell中的逻辑运算符之前,我们首先需要对逻辑运算符的基本概念以及它们在Linux Shell环境下的应用有一个清晰的认识。逻辑运算符是编程语言和脚本语言中非常重要的一部分,尤其是在条件判断和流程...
标题《shell学习笔记》和描述“主要介绍了shell变量、shell字符串、shell数组、shell传递参数、shell基本运算符”所表明的,本内容为学习Shell的基础知识,涵盖变量、字符串、数组、参数传递和基本运算符的使用方法...
#### 五、Shell基本运算符 Shell支持多种运算符,用于执行不同的操作。 - **关系运算符**:用于比较数值。 - `-eq`: 等于 - `-ne`: 不等于 - `-gt`: 大于 - `-lt`: 小于 - `-ge`: 大于等于 - `-le`: 小于等于...
以下是对Shell基本运算符的详细解释: 1. **Shell运算符**: - **算数运算符**:用于执行基本的数学运算,如加、减、乘、除和取余。在原生bash中,直接进行数学运算可能会有局限,但可以借助`awk`或`expr`命令。...
在 shell 脚本中,变量是用来存储数据的一种基本机制。这些变量可以用于存储字符串、数值或者其他类型的数据,并且可以在脚本的不同部分进行引用和修改。 ##### 声明与赋值 ```sh variable-name=value ``` 例如: `...
在Linux操作系统中,Shell脚本是一种强大的自动化工具...理解这些基本概念对于提升Linux Shell脚本的编写技能至关重要。在实际工作中,结合这些知识点,我们可以编写出更加灵活、可靠的自动化脚本来处理各种系统任务。
Shell算术运算符是Linux或Unix系统中Bash shell脚本编程的重要组成部分,它们用于进行基本的数学计算。本文将详细讲解如何使用这些运算符,以及如何在脚本中实现计算。 1. **算术运算符分类**: - 简单算术运算符...
在Shell中,`if`语句的基本结构如下: ```bash if condition then # 条件为真时执行的命令 else # 条件为假时执行的命令 fi ``` `condition`可以是各种表达式,包括运算符和`test`命令的组合。`test`是一个内置...
除了基本的变量和运算符,Shell还提供了数组、环境变量、位置参数和特殊变量等高级特性。数组允许你存储一组相关的值,而环境变量是全局的,对所有子进程可见。位置参数是脚本接收的命令行参数,通常用`$1`, `$2`等...
Shell 编程基础涵盖了变量、参数传递、数组、基本运算符、echo 命令、printf 命令、test 命令、流程控制、函数、输入/输出重定向、文件包含等多个方面。 变量是 Shell 编程的基础,变量可以是数字、字符串或其他...
Shell是Linux和Unix系统中的命令解释器,它提供了一...通过深入学习和实践这些Shell基本命令和脚本编写技巧,你可以有效地管理和自动化Linux系统的日常任务。记住,持续练习和查阅相关资料是成为Shell脚本专家的关键。
linux shell 变量和运算符 执行Shell脚本的方式 [小函数]:bash版rev&tac shell编程走马观花系列(KSH下set选项) 文章出处:http://www.diybl.com/chm/htm/3_program/shell/shelljs/2008821/2008821102938.html
* Shell 变量是 Shell 中的基本组成部分 * Shell 变量可以存储字符串、数字、布尔值等类型的数据 * Shell 变量可以被用来存储用户输入的数据、环境变量、命令的输出结果等 Shell 特殊变量: * $0:当前 Shell ...
以下面的格式提供运算表达式:$(( expression )) $ echo $((5*(3+3))) 30 $ result = $(($myvar-10)) shell提供方便的数之间的进制转换: $ echo $((013))#八进制 $ echo $((0xA4))#十六进制 还可以使用以下格式指定...
掌握 Shell 的基本概念和编程规范,是进行 Linux 系统管理和自动化运维的基础。理解变量的定义和使用,以及各种运算符和控制结构,将有助于编写出高效、灵活的 Shell 脚本,从而提高工作效率。对于更复杂的任务,...
本篇文章将深入探讨Shell的基础知识,包括其概念、重要性、基本语法以及如何进行Shell编程练习。 一、Shell简介 Shell是一个用户与操作系统之间的接口,它接受用户输入的命令并解析成操作系统可以理解的语言,然后...
5. **Shell运算符**:涵盖了Shell的各种运算符,包括算术、关系、布尔、逻辑和字符串运算符,以及文件测试运算符,并通过一个简单的计算器案例来实践这些运算符。 6. **Shell命令输出**:讲解了echo和printf命令...
在“Shell脚本学习教程PDF版”中,你将深入理解Shell脚本的基本概念、语法结构以及高级特性。 首先,Shell脚本的基础包括变量、条件语句、循环结构和函数。变量是存储数据的地方,可以是文本、数字或者其他类型。在...