|
1
2
3
4
|
if command
then
commands
fi
|
|
1
2
3
|
if command; then
commands
fi
|
|
1
2
3
4
5
6
|
if command
then
commands
else
commands
fi
|
|
1
2
3
4
5
6
7
|
if command1
then
commands
elif command2
then
more commands
fi
|
|
1
|
test condition
|
|
1
2
3
4
|
if test condition
then
commands
fi
|
|
1
2
3
4
|
if [ condition ]
then
commands
fi
|
| 比较 | 说明 |
|---|---|
| n1-eq n2 | 检查n1是否与n2相等 |
| n1-ge n2 | 检查n1是否大于或等于n2 |
| n1-gt n2 | 检查n1是否大于n2 |
| n1-le n2 | 检查n1是否小于或等于n2 |
| n1-lt n2 | 检查n1是否小于n2 |
| n1-ne n2 | 检查n1是否不等于n2 |
– 数值条件测试可以用在数值和变量上
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
...
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo ...
fi
if [ $val1 -eq $val2 ]
then
echo ...
else
echo ...
fi
...
|
|
1
2
3
4
5
6
7
8
|
...
val1=`echo "scale = 4; 10 / 3" | bc`
if[ $val1 -gt 3 ]//运行脚本时会报错
then
echo ...
fi
...
|
| 比较 | 描述 |
|---|---|
| str1 = str2 | 检查str1是否和str2相同 |
| str1 = str2 | 检查str1是否和str2不同 |
| str1 = str2 | 检查str1是否比str2小 |
| str1 = str2 | 检查str1是否比str2大 |
| -n str1 | 检查str1的长度是否非0 |
| -z str1 | 检查str1的长度是否为0 |
– 比较字符串相等性:会将所有的标点和大写也考虑在内
– 字符串顺序注意事项:
– 大于小于符号必须转义,否则shell会把他们当做重定向符号而把字符串当做文件名
– 大于小于顺序和sort命令所采用的不同
– 在test命令中大写字母会被当成小于小写字母
– test命令使用标准的ASCII顺序,根据每个字符的ASCII数值来决定排序顺序
– 当将同样的字符串放进文件中并用sort命令排序时,小写字母会先出现
– sort命令使用系统的本地化语言设置中定义的排序顺
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$cat test
#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
$
$./test
baseball is less than hockey
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$cat test
#!/bin/bash
val1=Testing
val2=testing
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
$
$./test
Testing is less than testing
$sort testfile
testing
Testing
$
|
|
1
2
3
4
5
6
7
8
9
10
11
|
val1=testing
val2=‘ ‘
if [ -n "$val1" ] //检查val1变量是否长度非零
then
...
if [ -z "$val2" ] //检查val2变量是否长度为零
then
...
if [ -z "$val3" ] //检查val1变量是否长度非零,这个变量并未在shell脚本中定义过,所以说明字符串长度仍然为零
then
|
| 比较 | 描述 |
|---|---|
| -d file | 检查file是否存在并是一个目录 |
| -e file | 检查file是否存在 |
| -f file | 检查file是否存在并是一个文件 |
| -r file | 检查file是否存在并可读 |
| -s file | 检查file是否存在并非空 |
| -w file | 检查file是否存在并可写 |
| -x file | 检查file是否存在并可执行 |
| -o file | 检查file是否存在并属当前用户所有 |
| -G file | 检查file是否存在并且默认组与当前用户相同 |
| file1 -nt file2 | 检查file是否比file2新 |
| file1 -ot file2 | 检查file是否比file2旧 |
| 符号 | 描述 |
|---|---|
| val++ | 后增 |
| val– | 后减 |
| ++val | 先增 |
| –val | 先减 |
| ** | 幂运算 |
| << | 左位移 |
| & | 位布尔和 |
| && | 逻辑和 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$cat test
#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 )) //大于号无需转义
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
$
$./test
The square of 10 is 100
$
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$cat test
#!/bin/bash
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I do not know you"
fi
$
$./test
Hello rich
$
|
|
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
|
//格式:
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
//例程:
$ cat test
#!/bin/bash
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to log off when you‘re done";;
*)
echo "Sorry, you are not allowed here";;
esac
$
$./test
Welcome, rich
Please enjoy your visit
$
|
原文:http://www.cnblogs.com/zhangmingcheng/p/5811448.html