-
Notifications
You must be signed in to change notification settings - Fork 4
/
Expr.sh
95 lines (79 loc) · 1.16 KB
/
Expr.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
#!/bin/bash
# shell运算符,算术运算符,关系运算符,布尔运算符,字符串运算符
var=`expr 2 + 2` # 空格要有,算术运算符
echo "算术=${var}"
#关系运算符
a=10
b=20
c=10
if [ $a == $c ]
then
echo "相等:a == c"
fi
if [ $a != $b ]
then
echo "不相等:a != b"
fi
if [ $a -eq $c ]
then
echo "相等:a -eq c"
fi
if [ $a -ne $b ]
then
echo "不相等:a -ne b"
fi
if [ $b -gt $a ]
then
echo "大于:b -gt a"
fi
if [ $a -lt $b ]
then
echo "小于:a -lt b"
fi
if [ $a -ge $c ]
then
echo "大于等于:a -ge c"
fi
if [ $a -le $c ]
then
echo "小于等于:a -le c"
fi
#布尔运算符
if [ $a -gt 0 -a $a -lt 100 ]
then
echo "并且:a大于0并且小于100"
fi
if [ $a -gt 0 -o $a -lt 5 ]
then
echo "并且:a大于0或者小于5"
fi
#字符串运算符
a="abc"
b="efg"
c="abc"
if [ $a = $c ] #注意是一个等于号
then
echo "相等:a = c"
fi
if [ $a ]
then
echo "字符串不为空"
fi
#文件测试运算符
file="/tsh/shell/Expr.sh"
if [ -e $file ]
then
echo "文件存在"
fi
if [ -r $file ]
then
echo "文件可读"
fi
if [ -w $file ]
then
echo "文件可写"
fi
if [ -x $file ]
then
echo "文件可执行"
fi