shell脚本编程之运算符

## 一、Bash的运算符和基本运算

  在shell中,所有的数据类型都是字符串型的,那如果需要进行运算的话,则需要手动修改一下变量数据类型,看例子

```sh
[root@localhost shells]# a=1
[root@localhost shells]# b=2
[root@localhost shells]# c=$a+$b
[root@localhost shells]# echo $c
1+2
##可以看到2个变量的值并没有相加,而是以“+”的方式组合在一起的,解决这个问题需要定义变量的类型
```

### 二、declare声明变量类型

declare [+/-] [选项] [变量名]

 -:给变量设定类型属性
 +:取消变量的类型属性
 
 选项:
 -a :将变量声明为数组型
 -i :将变量声明为整数型(integer)
 -x :将变量声明为环境变量    等同于: export a=100
 -r :将变量声明为只读变量
 -p :显示指定变量的被声明类型

示例1:把变量定义成数组型

```sh
[root@localhost shells]# declare -a name[0]=zhangsan   ##定义数组,name代表数组,[0]代表下标,默认0开始,“=”后代表数组的值
[root@localhost shells]# declare -a name[1]=lisi
[root@localhost shells]# declare -a name[2]=wangwu
[root@localhost shells]# echo ${name[*]}             ##查看数组的值
zhangsan lisi wangwu
```

示例2:把变量定义成环境变量

```sh
[root@localhost shells]# declare -x test=123   ##-x代表环境变量,作用和export一样,shell中的环境变量均是通过declare -x来定义的。
[root@localhost shells]# set |grep test
test=123
```

示例3:只读变量(如果设置只读变量,该变量不能赋值,不能取消变量的值,也不能取消变量)

```sh
[root@localhost shells]# declare -r test
[root@localhost shells]# test=456
-bash: test: 只读变量
[root@localhost shells]# declare +r test
-bash: declare: test: 只读变量
[root@localhost shells]# unset test
-bash: unset: test: 无法反设定: 只读 variable
```

示例4:查看变量属性

```sh
[root@localhost ~]# declare -p test  
declare -rx test="123"
[root@localhost ~]# 
```

## 三、 bash的运算符分类

```
1、算术运算符

2、关系运算符

3、布尔运算符

4、字符串运算符

5、文件测试运算符
```

### 算术运算符

```
格式1:$((算数运算符))

格式2:((表达式1,表达式2…))

格式3:[表达式]
```

特点:

```
1、在双括号结构中,所有表达式可以像c语言一样,如:a++,b--等。a++ 等价于 a=a+1 

2、在双括号结构中,所有变量可以不加入:“$”符号前缀。

3、双括号可以进行逻辑运算,四则运算

4、双括号结构 扩展了for,while,if条件测试运算((a=1,a<10,a++))

5、支持多个表达式运算,各个表达式之间用“,”分开
```

常用的算数运算符

| 运算符        | 意义                         |
| ------------- | ---------------------------- |
| ++  --        | 递增及递减,可前置也可以后置 |
| + - !  ~      | 一元运算的正负号 逻辑与取反  |
| + -   * /  %  | 加减乘除与余数               |
| <  <=   >  >= | 比较大小符号                 |
| ==  !=        | 相等 不相等                  |
| && \|\|       | 逻辑与 逻辑或                |
| ?            | 条件判断                     |


实例2: 比较大小

```sh
[root@localhost shell]# echo $((1>2))  

0

[root@localhost shell]# echo $((1<=2)) 

 \#真为1 假为0 

1
```

2)、expr

作用:Shell变量的算术运算

expr命令 可以对整数型变量进行算术运算,也可用于字符串

```
语法: expr 表达式 
#注意 运算符之间要有空格,特殊符号要转义
```

示例:

```sh
[root@localhost scripts]# expr 3 + 5

8

[root@localhost scripts]# var1=8

[root@localhost scripts]# var2=2

[root@localhost scripts]# expr $var1 - 5

3

[root@localhost scripts]# expr $var1 / $var2

4

[root@localhost scripts]# expr $var1 * $var2

expr: syntax error                                       ##报语法错误,需要进行转义

[root@localhost scripts]# expr $var1 \* $var2

16
```

3)  字符串的处理

实例1: 计算字符串的长度

```sh
[root@localhost ~]# expr length "ni hao a"

8
```

实例2:抓取字符串中特定的字串,后面的数字表示从第3个字符开始,截取5个字符出来

```sh
[root@localhost ~]# expr substr "this is a man" 3 5

is is
```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值