Shell教程(三):数组/Arrays、基本运算符
定义数组值:
一个数组变量和标量变量之间的差异可以解释如下。
说,你正试图表示各种学生为变量集的名字。每一个单个变量是一个标量变量,如下所示:
NAME01="Zara"
NAME02="Qadir"
NAME03="Mahnaz"
NAME04="Ayan"
NAME05="Daisy"
我们可以用一个单一的阵列来存储所有上述提及的名称。以下是最简单的方法创建一个数组变量分配一个值,其索引之一。这是表示,如下所示:
array_name[index]=value
array_name 是数组名,索引是在阵列中,你要设置的项目索引,值是你想要的值设置该项目。
作为一个例子,下面的命令:
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz www.yiibai.com"
NAME[3]="Ayan"
NAME[4]="Daisy"
如果您使用的是ksh shell在这里初始化数组的语法:
set -A array_name value1 value2 ... valuen
如果您使用的是bash shell中,这里是初始化数组的语法:
array_name=(value1 ... valuen)
访问数组值:
当您设置任何数组变量,并可访问它,如下所示:
${array_name[index]}
在这里,array_name是数组的名称,index是索引进行访问的值。下面是一个简单的例子:
#!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
This would producefollowing result:
$./test.sh
First Index: Zara
Second Index: Qadir
您可以访问数组中的所有项目通过以下方式之一:
${array_name[*]}
${array_name[@]}
array_name 是数组的名字你所感兴趣的 以下是个最简单的例子:
#!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
这将产生以下结果:
$./test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy
有各种不同的运算符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. |
C Shell 操作符:
以下链接将在C Shell运算符给出简单的用法。
C Shell 运算符
Korn Shell 运算符:
以下链接将在Korn Shell运算符给出简单的用法
Korn Shell 运算符
from: http://www.yiibai.com/shell/what_is_shell.html#
Shell教程(三):数组/Arrays、基本运算符相关推荐
- Java 结构体之 JavaStruct 使用教程三 JavaStruct 数组进阶
经过前面两篇博客的介绍,相信对于 JavaStruct 的认识以及编程使用,读者已经有一定的基础了.只要理解和实践结合起来,掌握还是很容易的.下面进行一些数组使用方面的实例说明及演示. 在结构体类中使 ...
- java 三点_[Java教程]三点运算符使用方法
[Java教程]三点运算符使用方法 0 2017-10-13 03:01:05 先看一个es6规范下三点运算符的使用实例:let fun=function(a,...list){ console.lo ...
- ES6 入门教程 9 数组的扩展 9.1 扩展运算符
ES6 入门教程 ECMAScript 6 入门 作者:阮一峰 本文仅用于学习记录,不存在任何商业用途,如侵删 文章目录 ES6 入门教程 9 数组的扩展 9.1 扩展运算符 9.1.1 含义 9.1 ...
- Hadoop教程(三)HDFS文件系统Shell命令
Hadoop教程(三)HDFS文件系统Shell命令 本文链接:https://blog.csdn.net/yuan_xw/article/details/50202381 Hadoop教程(三)HD ...
- shell脚本中数组的使用_Shell脚本中的数组
shell脚本中数组的使用 Knowing how to work with arrays in shell scripts will help you work with larger datase ...
- Linux学习笔记二Shell教程
Shell 教程 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个 ...
- linux shell教程(一)
本教程摘自 C语言教程网 Shell教程目录 1.Shell简介 2.几种常见的Shell 3.Shell与编译型语言的差异 4.什么时候使用Shell 5.第 ...
- linux shell 创建序列数组(list,array)方法
from: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851517.html 关于linux数组定义,以及生成方法,请看:linux she ...
- Linux Shell高级技巧(三)
十三.格式化输出指定用户的当前运行进程: 在这个例子中,我们通过脚本参数的形式,将用户列表传递给该脚本,脚本在读取参数后,以树的形式将用户列表中用户的所属进程打印出来. /> cat ...
- 区块链教程(三):Solidity编程基础
注:本教程为技术教程,不谈论且不涉及炒作任何数字货币 区块连教程(一):前置知识-linux补充 区块链教程(二):基础概念介绍 区块链教程(三):Solidity编程基础 区块链教程(四):搭建私链 ...
最新文章
- JBoss5.1.0部署SSH2
- Unity设置AppIcon方法
- 人工智能会终结就业吗?
- Ionic创建页面以及页面之间跳转、页面添加返回按钮、新增底部页面
- 统计字符串中单词个数的算法优化
- POJ 1741tree-点分治入门
- 一个拆分使用的存储过程例子
- 新媒体增长方法从哪里找?
- 背景虚化_背景虚化的效果用手机怎么拍?原来这样简单
- Grasshopper不显示gha插件的解决方法
- 转换动态磁盘到普通磁盘
- Windows XP 64位注册表删除问题
- H5页面设计器,仿有赞商城页面在线设计器,比富文本框更友好的内容编辑器...
- Flutter入门综合练习
- 软件设计模式--软件设计演变过程
- 快慢指针(java)
- vdbench 配置案例及参数说明
- Day07-课程分类管理-课程分类显示前端-p104
- chdir改变当前目录
- IOS版aplayer使用教程_Y Y的使用教程视频版/图片版(手机版。电脑版。楚恒上课必用软件)...
热门文章
- 亚马逊外包平台的50万劳工:人工智能的背后,无尽数据集的建造
- ICML论文|这违反直觉的“升噪”方法,反而能很好的解决激活函数梯度弥散的问题
- 揭秘高效协作工具背后的技术架构
- 阿里巴巴集团CTO王坚:互联网、数据和计算
- 不进化,则消亡——互联网时代企业管理的十项实践
- Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式
- Spring-AOP 引介切面
- 递归回溯解决八皇后问题
- php回调函数如何执行顺序,PHP回调函数调用方式
- mapgis明码文件转为点线面文件_手机上word文档可以转为pdf文件吗?