forked from jaywcjlove/shell-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo26
More file actions
executable file
·40 lines (24 loc) · 955 Bytes
/
demo26
File metadata and controls
executable file
·40 lines (24 loc) · 955 Bytes
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
#!/bin/bash
# 用((...))结构来使用C风格操作符来处理变量.
echo
(( a = 23 )) # 以C风格来设置一个值,在"="两边可以有空格.
echo "a (initial value) = $a"
(( a++ )) # C风格的计算后自增.
echo "a (after a++) = $a"
(( a-- )) # C风格的计算后自减.
echo "a (after a--) = $a"
(( ++a )) # C风格的计算前自增.
echo "a (after ++a) = $a"
(( --a )) # C风格的计算前自减.
echo "a (after --a) = $a"
########################################################
# 注意,就像在C中一样,计算前自增自减和计算后自增自减有一点不同的的副作用
#
n=1; let --n && echo "True" || echo "False" # False
n=1; let n-- && echo "True" || echo "False" # True
# 多谢Jeroen Domburg.
########################################################
echo
(( t = a<45?7:11 )) # C风格的三元计算.
echo "If a < 45, then t = 7, else t = 11."
echo "t = $t " # Yes!