一、数值运算与运算符:
思考引入:
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
[root@localhost sh]# cc=$aa+$bb
[root@localhost sh]# echo $cc
11+22
如上:Linux中,shell中的变量类型,都是字符串类型,在此表示加号连接了两个字符串,然后把该字符串赋给了cc,于是如上
1、declare 声明变量类型
[root@localhost ~]# declare 【+/-】【选项】变量名
选项:
-: 给变量设定类型属性
+:取消变量的类型属性
-i :将变量声明为整数型(integer)
-x :将变量声明为环境变量
-p :显示指定变量的被声明的类型
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
[root@localhost sh]# cc=$aa+$bb
[root@localhost sh]# echo $cc
11+22
[root@localhost sh]# declare -p aa
declare -- aa="11"
[root@localhost sh]# export aa #习惯于用export 将某变量设置为环境变量,或者说是为其添加-x属性
[root@localhost sh]# declare -p aa
declare -x aa="11"
[root@localhost sh]#
2、数值运算——
方法1:
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
#如上为给变量赋值方法
[root@localhost sh]# declare-i cc=$aa+$bb
-bash: declare-i: command not found
[root@localhost sh]# declare -i cc=$aa+$bb
[root@localhost sh]# echo $cc
33
方法2:expr 或let 数值运算工具
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
[root@localhost sh]# dd=$(expr $aa+$bb)
[root@localhost sh]# echo $dd
11+22
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
[root@localhost sh]# dd=$(expr $aa + $bb)
[root@localhost sh]# echo $dd
33
特别注意:注意“ + ”号左右的空格!!!
方法3:“$((运算式))” 或 “$[运算式]”
[root@localhost sh]# aa=11
[root@localhost sh]# bb=22
[root@localhost sh]# ff=$(($aa+$bb))
[root@localhost sh]# echo $ff
33
[root@localhost sh]# gg=$[$aa+$bb]
[root@localhost sh]# echo gg
gg
[root@localhost sh]# echo $gg
33
[root@localhost sh]#
建议使用第三种,比较方便
3、运算符:
优先级 | 运算符 | 说明 |
13 | -,+ | 单目负、单目正 |
12 | !,~ | 逻辑非、按位取反或补码 |
11 | *,/,% | 乘、除、取模 |
10 | +,- | 加、减 |
9 | << ,>> | 按位左移、按位右移 |
8 | <= ,>= ,< ,> | 小于或等于、大于或等于、小于、大于 |
7 | == ,!= | 等于、不等于 |
6 | & | 按位与 |
5 | ^ | 按位异或 |
4 | | | 按位或 |
3 | && | 逻辑与 |
2 | || | 逻辑或 |
1 | =,+=,=,*=,/=,&=,^=,!=,<<=,>>= | 赋值、运算且赋值 |
[root@localhost sh]# aa=$(((11+3)*3/2))
#虽然乘和除的优先级高于加,但是通过小括号可以调整运算优先级
[root@localhost sh]# echo $aa
21
[root@localhost sh]# bb=$(( 14%3))
#14不能被3整除,余数是2
[root@localhost sh]# echo $bb
2
[root@localhost sh]# cc=$(( 1 && 0))
#逻辑与运算只有想与的两边都是1时结果才是1,否则与的结果是0
[root@localhost sh]# echo $cc
0
注意原则:先写$() 括起来,内部是什么和正常的数学运算都是一致的
二、变量测试与内容替换
变量置换方式 | 变量y没有设置 | 变量y为空值 | 变量y设置值 |
x=${y-新值} | x=新值 | x为空 | x=$y |
x=${y:-新值} | x=新值 | x=新值 | x=$y |
x=${y+新值} | x为空 | x=新值 | x=新值 |
x=${y:+新值} | x为空 | x为空 | x=新值 |
x=${y=新值} | x=新值 y=新值 |
x为空 y值不变 |
x=$y y值不变 |
x=${y:=新值} | x=新值 y=新值 |
x=新值 y=新值 |
x=$y y值不变 |
x=${y?新值} | 新值输出到标准错误输出(就是屏幕) | x为空 | x=$y |
x=${y:?新值} | 新值输出到标准错误输出(就是屏幕) | 新值输出到标准错误输出(就是屏幕) | x=$y |
注:上述内容不用背,要作为手册放到易查的位置,用到时查一下就好,但首先要能看懂表格含义。
示例一、测试x=${y-新值}
[root@localhost ~]# unset y
# 删除变量y
[root@localhost ~]# x=${ y-new}
#进行测试
[root@localhost ~]# echo $x
new
#因为变量y不存在,所以x=new
[root@localhost ~]# y=""
[root@localhost ~]# x=${ y-new}
[root@localhost ~]#echo $x
如上,测试第一行
[root@localhost sh]# unset y #表示不管是否有Y,用此句表示删除y,保证确定不存在y变量
[root@localhost sh]# x=${y-new} #new 可以是其他任意的变量
[root@localhost sh]# echo $x #如果打印出来为new,则表示y变量不存在
new
[root@localhost sh]# y=""
[root@localhost sh]# x=${y-new}
[root@localhost sh]# echo $x
[root@localhost sh]#
[root@localhost sh]# y=old
[root@localhost sh]# x=${y-new}
[root@localhost sh]# echo $x
old
变量测试,就是用来判断某个变量是否存在,是否为空,等,用于shell编程中,
用于计算机系统自己判断某个变量的状态
实例二:第二行第一个判断
[root@localhost sh]# y=""
[root@localhost sh]# x=${y:-new}
[root@localhost sh]# echo $x
new
[root@localhost sh]#