1 测试运算符test [ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
DESCRIPTION
The test utility evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). If there
is no expression, test also returns 1 (false).

All operators and flags are separate arguments to the test utility.

The following primaries are used to construct expression:

-b file True if file exists and is a block special file.

-c file True if file exists and is a character special file.

-d file True if file exists and is a directory.

-e file True if file exists (regardless of type).

-f file True if file exists and is a regular file.

-g file True if file exists and its set group ID flag is set.

-h file True if file exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do
not rely on its existence; use -L instead.

-k file True if file exists and its sticky bit is set.

-n string True if the length of string is nonzero.

-p file True if file is a named pipe (FIFO).

-r file True if file exists and is readable.

-s file True if file exists and has a size greater than zero.

-t file_descriptor
True if the file whose file descriptor number is file_descriptor is open and is associated with a terminal.

-u file True if file exists and its set user ID flag is set.

-w file True if file exists and is writable. True indicates only that the write flag is on. The file is not writable on a read-only file
system even if this test indicates true.

-x file True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that
file can be searched.

-z string True if the length of string is zero.

-L file True if file exists and is a symbolic link.

-O file True if file exists and its owner matches the effective user id of this process.

-G file True if file exists and its group matches the effective group id of this process.

-S file True if file exists and is a socket.

file1 -nt file2
True if file1 exists and is newer than file2.

file1 -ot file2
True if file1 exists and is older than file2.

file1 -ef file2
True if file1 and file2 exist and refer to the same file.

string True if string is not the null string.
s1 = s2 True if the strings s1 and s2 are identical.

s1 != s2 True if the strings s1 and s2 are not identical.

s1 < s2 True if string s1 comes before s2 based on the binary value of their characters.

s1 > s2 True if string s1 comes after s2 based on the binary value of their characters.

n1 -eq n2 True if the integers n1 and n2 are algebraically equal.

n1 -ne n2 True if the integers n1 and n2 are not algebraically equal.

n1 -gt n2 True if the integer n1 is algebraically greater than the integer n2.

n1 -ge n2 True if the integer n1 is algebraically greater than or equal to the integer n2.

n1 -lt n2 True if the integer n1 is algebraically less than the integer n2.

n1 -le n2 True if the integer n1 is algebraically less than or equal to the integer n2.

These primaries can be combined with the following operators:

! expression True if expression is false.

expression1 -a expression2
True if both expression1 and expression2 are true.

expression1 -o expression2
True if either expression1 or expression2 are true.

( expression )
True if expression is true.
The -a operator has higher precedence than the -o operator.

EXIT STATUS
The test utility exits with one of the following values:

0 expression evaluated to true.

1 expression evaluated to false or expression was missing.

>1 An error occurred.

数字测试运算符

关系运算符只支持数字,不支持字符串,除非字符串的值是数字。
下表列出了常用的关系运算符,假定变量 a 为 10,变量 b 为 20

运算符 单词 说明 举例
-eq equal 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
-ne not equal 检测两个数是否相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt great than 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
-lt less than 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
-ge great than or equal 检测左边的数是否大于等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
-le less than or equal 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。
  • 案例一:测试结果
1
2
3
4
5
6
7
8
# 0表示正常执行
#非0 表示错误类型
[root@localhost ~]$ [ 10 -gt 10 ]
[root@localhost ~]$ echo $?
1
[root@localhost ~]$ [ 10 -eq 10 ]
[root@localhost ~]$ echo $?
0
  • 案例二:判断用户是否存在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]$ vim demo.sh 
#!/bin/bash
#接受用户的输入
read -p '请输入需要查询的用户名:' username

#获取指定用户名在passwd文件中出现的次数
count=$(cat /etc/passwd | grep $username | wc -l)
#count=`cat /etc/passwd | grep $username | wc -l`

#判断出现的次数,如果次数=0则用户不存在,反之存在
if [ $count == 0 ]
then
echo '用户不存在'
else
echo '用户存在'
fi

逻辑测试运算符

下表列出了常用的布尔运算符,假定变量 a 为 10,变量 b 为 20:

运算符 说明 举例
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
-o 或(或者)运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 与(并且)运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。

或运算:一个为真即为真,全部为假才是假
与运算:一个为假即为假,全部为真才是真

字符串测试运算符

下表列出了常用的字符串运算符,假定变量 a 为 “abc”,变量 b 为 “efg”:

运算符 说明 举例
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否为0,不为0返回 true。 [ -n $a ] 返回 true。
str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。

文件测试运算符

文件测试运算符用于检测 Unix/Linux 文件的各种属性。

操作符 说明 举例
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。

2 算术运算符-数字

原生bash不支持数学运算。所以

1
2
3
4
5
➜  shell git:(master) ✗ a=1+1  
➜ shell git:(master) ✗ echo a
a
➜ shell git:(master) ✗ echo $a
1+1

expr 是一款表达式计算工具,使用它能完成表达式的求值操作。它相当于命令行工具,其后是参数

  • 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
  • 两个数相加(注意使用的是反引号 ` 而不是单引号 ‘):
1
2
3
4
[root@localhost ~]$ vi computer.sh
#!/bin/bash
val=`expr 2 + 2`
echo "两数之和为 : $val"

其他算术运算方法

(1) 使用let命令进行算术运算,只支持整数运算。

(2) 使用expr命令进行算术运算,只支持整数运算。

(3) 使用bc命令进行算术运算,支持小数运算。

(4) 使用运算语法:$[算术表达式],只支持整数运算。

(5) 使用运算语法:$((算术表达式)),只支持整数运算。

(6) 在初始化变量时,将变量定义为”整数”类型,则可直接进行整数运算。

3 控制流运算符

4 关键字

readonly

相当于C中的const,readonly将变量设为只读模式,任何针对他们的赋值都是错误的

1
2
3
$readonly var=123
$var=345
bash: var: readonly variable

unset

删除变量

1
2
3
4
5
6
7
8
unset var_name 删除变量var_name
unset -v first middle last 删除其他变量
unset -f fcn 删除函数
$tunset=123
$echo $tunset
123
$unset tunset
$echo $tunset

shift

用来截去来自列表的位置参数,从左侧开始,一旦执行shift,$1的初始值就会永远消失,取而代之的是$2的旧值,依次类推,$#的值也会依次减1
shift也可以带一个参数,表示一次截取的个数

1
2
3
4
5
6
7
8
9
10
$cat shell.sh 
#!/bin/bash
echo "${1}"

shift

echo "${1}"
$./*.sh 123 456
123
456

set

设置新的位置参数

1
2
3
4
5
6
7
8
9
$set qq ww ee
$echo "${1}"
qq
$echo "${0}"
bash
$echo "${2}"
ww
$echo "$#"
3

export

添加变量到变量导出列表,即如果在shell中执行子shell,该变量将作为环境变量从而在子shell中变得可以使用。

1
2
3
4
5
6
7
8
9
10
$cat shell.sh 
#!/bin/bash
echo "$NEWENV"

$NEWENV=123
$./shell.sh

$export NEWENV
$./shell.sh
123

exit

提前结束标本

  • 0 正常退出
  • 0 错误类型返回

test

test 和[ ]既是一个linux缺省安装的软件命令,又是一个shell关键字。两者的功能都是一模一样的