判断
简单if结构
1 2 3 4 5
| if test then command command ... fi
|
1 2 3 4 5
| if test; then command command ... fi
|
例子
1 2 3 4 5
| read int1
if [ "$int1" -lt 10 ] then echo "$int1" fi
|
exit命令
退出
1 2 3 4 5 6 7
| read str1
if [ -z "$str1" ] then echo "you input is null" exit 1 fi
|
if/else结构
1 2 3 4 5 6 7 8 9
| if test; then command ... command else command ... command fi
|
1 2 3 4 5 6
| if [ -e "$1" ]; then echo "file $1 exist." else echo "file $1 not exist." exit 1 fi
|
if/else语句嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13
| if test; then if test; then command else command fi else if test; then command else command fi fi
|
if/elif/else结构
1 2 3 4 5 6 7 8 9
| if test; then command elif test; then command elif test; then command else command fi
|
case结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| case variable in value1) command ... command;; value2) command ... command;; *) command ... command;; esac
|
1 2 3 4 5 6 7 8 9
| read month case "$month" in 1) echo "the month 1";; 2) echo "the month 2";; *) echo "谁说要写完了";; esac
|
运算符
算术运算符:+,-,,/,%,*
算术复合赋值运算符:+=,-=,*=,/=,%=
位运算符:<<(左移),>>(右移),&(按位与),|(按位或),~(按位非),^(按位异或)
自增自减运算符:++,–
数字常量
- 0作为前缀表示八进制
- 0x作为前缀表示十六进制
- 2#作为前缀表示二进制
- num#作为前缀表示num进制