`
dengwenwei121
  • 浏览: 35894 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
文章分类
社区版块
存档分类
最新评论

Shell 基本运算符

 
阅读更多

有各种不同的运算符shell都支持。本教程是基于默认shell(Bourne),所以我们要涵盖所有重要的Bourne Shell运算符。

有以下的运算符,我们将要讨论的:

  • 算术运算符。

  • 关系运算符。

  • 布尔运算符。

  • 字符串运算符。

  • 文件测试操作。

Bourne shell的最初并没有任何机制来执行简单的算术,但它使用外部程序,无论是awk或必须简单的程序expr。

下面是简单的例子,把两个数相加:

#!/bin/sh

val=`expr 2 + 2`
echo "Total value : $val"

这将产生以下结果:

Total value : 4

记下有以下几点:

  • 运算符和表达式之间必须有空格,例如2+2是不正确的,因为它应该写成2 + 2。

  • ``,称为倒逗号之间应包含完整的表达。

算术运算符:

算术运算符有以下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.

分享到:
评论

相关推荐

    新版Linux Shell编程实训(全)20170518.docx

    任务5.1 Shell基本运算符 97 5.1.1 运算符 97 5.1.2 算术运算符 97 5.1.3 关系运算符 99 5.1.4 布尔运算符 101 5.1.5 逻辑运算符 103 5.1.6 字符串运算符 104 5.1.7 文件测试运算符 106 任务5.2 Shell案例:计算器 ...

    shell.docx

    shell的基本知识:shell 变量 shell 字符串 单引号和双引号的区别 shell 基本运算符

    shell学习笔记

    shell学习笔记,主要介绍了shell变量、shell字符串、shell数组、shell传递参数、shell基本运算符

    UNIX 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函数

    shell编程 创建shell脚本 ①创建第一个shell脚本②运行shell脚本shell 基础语法 ①变量②变量分类与数组③shell传递参数④基本运算符shell流程控制语法 ①if else条件判断②for循环③while循环④until循环⑤case语句...

    shell 基本计算、逻辑运算、位运算详解

    以下面的格式提供运算表达式:$(( expression )) $ echo $((5*(3+3))) 30 $ result = $(($myvar-10)) shell提供方便的数之间的进制转换: $ echo $((013))#八进制 $ echo $((0xA4))#十六进制 还可以使用以下格式指定...

    linux-Shell基础视频教程

    10.5.1 Shell基础-Bash变量-数值运算与运算符.mp4 10.5.2 Shell基础-Bash变量-变量测试与内容替换.mp4 10.6.1 Shell基础-环境变量配置文件-简介.mp4 10.6.2 Shell基础-环境变量配置文件-作用.mp4 10.6.3 Shell...

    Shell脚本编程基础篇思维导图

    SHELL是UNIX系统的用户与操作系统之间的一种接口。...在Shell编程基础篇详细介绍了环境和变量、字符串处理、数组、注释、参数传递、基本运算符、选择循环的流程控制、函数、输入/输出重定向、文件格式等。

    Linux学习笔记09 — 超详细shell脚本编程快速入门

    文章目录1.shell简介1)什么是shell2)shell脚本3)运行shell脚本4)shell注释5)shell编写的基本步骤2.shell变量1)命名变量2)使用变量3)变量类型3)变量操作3.shell字符串1)字符串类型2)字符串操作4.shell数组...

    Shell脚本学习笔记

    自己整理的Shell脚本学习笔记,脚本实例可直接运行:1. 变量和字符串;2. 传递参数;3. 基本运算符;4. echo&printf;&test;;5. 流程控制;6. 函数;7. 输入输出重定向&文件嵌入

    Shell编程基础

    Shell编程的入门,包括Bash变量命名,运算符,环境变量,正则表达式等等;在学习linux基本命令之后,再学习shell编程。该文档含有shell编程需要的技术和例子,欢迎下载。

    跟老男孩学Linux运维:Shell编程实战

    / 554.4 Shell特殊扩展变量的知识与实践 / 60第5章 变量的数值计算实践 / 655.1 算术运算符 / 655.2 双小括号“(())”数值运算命令 / 665.3 let运算命令的用法 / 735.4 expr命令的用法 / 755.5 bc命令的用法 ...

    19. Linux开发-Shell脚本编程语言.pdf

    介绍Linux下Shell脚本语法基本编程: 涉及的知识点有变量使用、数组定义与使用、for循环、while循环、基本运算符、if语句、break语句、函数定义与调用等等。

    Linux中,关于Shell脚本的基本语法(一)

    在写Shell脚本的过程中,掌握最基本的脚本语法,是完成Shell脚本的关键基础所在。但是,掌握Shell脚本的基础内容,也不是一蹴而就的!需要长期来练习,付出大量的时间与努力。(我也正在学习的路上…一起呗) 每天...

    online2311#Linux_Shell#python基本语法1

    1.Python标识符 2.Python有五个标准的数据类型 3.Python数据类型转换 4.Python 运算符

    shelly:具有实时IRC聊天功能的python shell!

    所有基本的Shell命令。 Python代码解释。 彩色的提示和错误消息。 制表符完成。 自定义文本编辑器-kedit。 IRC聊天。 命令特定的帮助消息 没有外部依赖性(一路手写python)。 去做 管道支撑。 重定向运算符...

    cqshell:CQ Shell 是 AEM6 的基于 Web 的管理控制台

    壳牌 它是一个基于 Web 的 CQ 命令行解释器,因为每个像样的软件都需要一个 shell! 支持的 shell 命令 以下命令当前可用。 所有命令都按组组织,以提高手册页信息的传递效率。 组:基本文件操作 ...基本运算符:

    FlxShell:HaxeFlixel 的抽象 Linux 环境——一个与 shell 结合的文件系统

    所以 FlxShell 就像一个模拟的类似 Linux 的迷你操作系统,其中唯一的接口是一个 shell,其中两种脚本语言是 Haxe 的解释版本和 Linux shell 语言的一个非常基本的实现。 尝试使用“help”命令开始。 shell 具有 |...

    UNIX Handbook

    二.shell基本知识 9 三.grep 命令 11 四.sed 11 五.awk命令 11 六.find命令 12 七.test命令 12 八.expr命令 13 九.流程控制语法 13 1 if then elif else fi语句 13 4 for语句 15 6 while语句 16 7 until ...

    linux C编程实战

     1.4.2 Linux Shell   1.4.3 Linux的常用命令   1.5 Linux下程序的开发环境和开发过程   1.6 习题   第2章 C编程基础、Vi和Emacs编辑器   2.1 C程序的结构   2.2 C语言的基本数据类型   2.2.1...

Global site tag (gtag.js) - Google Analytics