/
operator.sh
executable file
·253 lines (208 loc) · 3.25 KB
/
operator.sh
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash
# 运算符计算必须要用 expre
# 运算符与变量之间必须加空格
#[ $a == $b ] []与变量之间也需要空格
a=9688
b=8388
#算术运算符
# +
c=`expr $a + $b`
echo $c
# -
d=`expr $a - $b`
echo $d
# * 需要转义
e=`expr $a \* $b`
echo $e
# /
f=`expr $a / $b`
echo $f
# %
g=`expr $a % $b`
echo $g
#赋值
# =
h=$a
echo $h
#比较运算符
# ==
if [ $a == $b ]
then
echo "a==b"
else
echo "a!=b"
fi
# -eq 相等
if [ $a -eq $b ]
then
echo "a==b"
else
echo "a!=b"
fi
# -ne 不等
if [ $a -ne $b ]
then
echo "a!=b"
else
echo "a==b"
fi
# -gt 大于
if [ $a -gt $b ]
then
echo "a>b"
else
echo "a<=b"
fi
# -lt 小于
if [ $a -lt $b ]
then
echo "a<b"
else
echo "a>=b"
fi
# -ge 大于等于
if [ $a -ge $b ]
then
echo "a>=b"
else
echo "a<b"
fi
# -le 小于等于
if [ $a -le $b ]
then
echo "a<=b"
else
echo "a>b"
fi
# 布尔值运算符
# ! 非
if [ $a != $b ]
then
echo "a!=b"
else
echo "a==b"
fi
# -o 或
if [ $a -gt 0 -o $a -lt $b ]
then
echo "a>0 -o a<b"
else
echo "!(a>0 -o a<b)"
fi
# -a 与
if [ $a -gt 0 -a $a -gt $b ]
then
echo "a>0 -a a>b"
else
echo "!(a>0 -a a>b)"
fi
# 逻辑运算符
# &&
if [ $a -gt 0 ] && [ $a -gt $b ]
then
echo "a>0 && a>b"
else
echo "!(a>0 && a>b)"
fi
# !!
if [ $a -gt 0 ] || [ $a -lt $b ]
then
echo "a>0 || a<b"
else
echo "!(a>0 || a<b"
fi
# 字符串运算
# = 字符串是否相等
str1="bash"
str2="cshell"
if [ $str1 = $str2 ]
then
echo "str1 = str2"
else
echo "str1 != str2"
fi
# != 不相等
if [ $str1 != $str2 ]
then
echo "str1 != str2"
else
echo "str1 = str2"
fi
# -z 字符串长度是否为0 为0 返回true
if [ -z $str1 ]
then
echo "str1.length == 0"
else
echo "str1.length != 0"
fi
# -n 字符串长度不为0 返回true -n后面必须写 "" 如果不写会一直为真
if [ -n "${str2}" ]
then
echo "str2.length != 0"
else
echo "str2.length == 0"
fi
# "字符串" 检测字符串是否为空 不为空返回true
if [ $str1 ]
then
echo "str1 != NULL"
else
echo "str1 == NULL"
fi
# 文件测试运算符
#当前文件完整路径
path=$(cd $(dirname ${0}); pwd)
file=${0}
fileName=${file#.}
filePath=${path}${fileName}
echo $filePath
fileNil=${path}"/test.sh"
# -d file 检测file是不是目录 是返回true
if [ -d ${path} ]
then
echo "${path} is a dir"
else
echo "${path} is not a dir"
fi
# -f file 检测file是不是一个普通文件(既不是目录,也不是设备文件),是返回true
if [ -f ${filePath} ]
then
echo "${filePath} is a file"
else
echo "${filePath} is not a file"
fi
# -s file 检测file是否为空(文件有无内容) 是返回true
if [ -s ${filePath} ]
then
echo "${filePath} is not null"
else
echo "${filePath} is null"
fi
# -e file 检测file是否存在(包含文件和目录) 存在返回true
if [ -e ${fileNil} ]
then
echo "${fileNil} is exist"
else
echo "${fileNil} is not exist"
fi
# -r file 检测file是否有读权限 有返回true
if [ -r ${filePath} ]
then
echo "${filePath} can be readed"
else
echo "${filePath} can not be readed"
fi
# -w file 检测file是否有写权限 有返回true
if [ -r ${filePath} ]
then
echo "${filePath} can be writed"
else
echo "${filePath} can not be writed"
fi
# -x file 检测file是否有执行权限 有返回true
if [ -r ${filePath} ]
then
echo "${filePath} can be executed"
else
echo "${filePath} can not be executed"
fi