判断

if…fi

用法

1
2
3
4
5
6
7
if condition
then
command1 if condition is true or if exit status
of condition is 0 (zero)
...
...
fi

示例

  1. 示例1

1
2
3
4
5
6
7
8
#!/bin/sh
#
#Script to print file
#
if cat $1
then
echo -e "\n\nFile $1, found and successfully echoed"
fi

  1. 示例2

1
2
3
4
5
6
7
8
#!/bin/sh
#
# Script to see whether argument is positive
#
if test $1 -gt 0
then
echo "$1 number is positive"
fi

注意上面的代码用到 test 命令来帮助判断后面跟着的语句。也可以使用 [ condition ] 来进行判断,二者完全等效。用法:

1
test condiction OR [ condiction ]

test[ condiction ]可以用于判断:

  1. 整数(不带小数点的十进制数字);
  2. 文件类型;
  3. 字符串。

用于整数的比较:

Shell脚本中的数学操作符 含义 test 语句搭配 [ condiction ] 语句搭配
-eq 两值相等 if test 5 -eq 6 if [ 5 -eq 6 ]
-ne 两值不相等 if test 5 -ne 6 if [ 5 -ne 6 ]
-lt 小于 if test 5 -lt 6 if [ 5 -lt 6 ]
-le 小于或等于 if test 5 -le 6 if [ 5 -le 6 ]
-gt 大于 if test 5 -gt 6 if [ 5 -gt 6 ]
-ge 大于或等于 if test 5 -ge 6 if [ 5 -ge 6 ]

用于字符串的比较:

操作 含义
string1 = string2 string1和string2相等
string1 != string2 string1和string2不相等
string1 string1非空或者有定义
-n string1 string1非空,并且有定义
-z string1 string1为空,并且有定义

Shell也可以用来检测文件和目录的类型:

操作 含义
-s file 非空文件
-f file 存在该文件
-d dir 存在该目录
-w file 为可写的文件
-r file 为只读文件
-x file 为可执行文件

用于逻辑判断的操作:

操作 含义
! condiction 逻辑非
condiction1 -a condiction2 逻辑与
condiction1 -o condiction2 逻辑或

if…else…fi

用法

1
2
3
4
5
6
7
8
9
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
if test $1 -ge 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

多级 if-then-else

如果需要用到多级的 if-then-else 结构,除了用嵌套的方式写之外,还可以用下面的方式:

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
then
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
then
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are true (i.e.
all of the above nonzero or false)
execute all commands up to fi
fi

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
# Script to test if..elif...else
#
if [ $1 -gt 0 ]; then
echo "$1 is positive"
elif [ $1 -lt 0 ]
then
echo "$1 is negative"
elif [ $1 -eq 0 ]
then
echo "$1 is zero"
else
echo "Opps! $1 is not number, give number"
fi

&& 和 ||

还有一个和条件判断有关的是条件操作符 &&||

用法

1
2
command1 && command2 # 当且仅当 command1 返回0值时,command2 才被执行
command1 || command2 # 当且仅当 command1 返回非0值时,command2 才被执行

也可以连起来用

1
command1 && comamnd2 || command3 # 当且仅当 command1 执行成功并返回0值,command2才被执行。否则 command3 被执行

[ condiction ] 语句

单独使用 [ condiction ] 语句也可以用来检查进行判断。它的用途包括:

  1. 文件属性比较;
  2. 字符串比较;
  3. 算术运算。

用法

1
[ condition ]

1
[ ! condition ]

1
[ condition ] && true-command

1
[ condition ] || false-command

1
[ condition ] && true-command || false-command

`

示例

1
2
3
4
5
[ 5 == 5 ] && echo "Yes" || echo "No"
[ 5 == 15 ] && echo "Yes" || echo "No"
[ 5 != 10 ] && echo "Yes" || echo "No"
[ -f /etc/resolv.conf ] && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."
[ -f /etc/resolv1.conf ] && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."

循环

Bash 支持两种类型的循环:

  1. for 循环
  2. while 循环

for 循环

用法

  1. 用法1

1
2
3
4
5
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done

  1. 用法2

1
2
3
4
5
6
7
for (( condiction1; condiction2; condiction3 ))
do
.....
...
repeat all statements between do and
done until condiction2 is TRUE
Done

示例

  1. 示例1

1
2
3
4
for i in 1 2 3 4 5
do
echo "welcome $i times"
done

  1. 示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
#
#Script to test for loop
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i = `condiction $i \* $n`"
done

  1. 示例3

1
2
3
4
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

  1. 示例4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (( i = 1; i <= 9; i++ )) ### Outer for loop ###
do
for (( j = 1 ; j <= 9; j++ )) ### Inner for loop ###
do
tot=`condiction $i + $j`
tmp=`condiction $tot % 2`
if [ $tmp -eq 0 ]; then
echo -e -n "\033[47m "
else
echo -e -n "\033[40m "
fi
done
echo -e -n "\033[40m" #### set back background colour to black
echo "" #### print the new line ###
done

while循环

用法

1
2
3
4
5
6
7
8
while [ condition ]
do
command1
command2
command3
..
....
done

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
#
#Script to test while statement
#
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `condiction $i \* $n`"
i=`condiction $i + 1`
done

分支

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
#
# if no command line arg
if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac

Comments