一:if 判断

1.1 基本语法

(1)单分支

1
2
3
if [ 条件判断式 ];then
程序
fi
1
2
3
4
if [ 条件判断式 ]
then
程序
fi

(2)多分支

1
2
3
4
5
6
7
8
9
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi

1.2 注意事项

  • ① [ 条件判断式 ],中括号和条件判断式之间必须有空格 ;
  • ② if 后要有空格;

1.3 案例实操

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@hadoop100 shell]# cat if.sh 
#!/bin/bash
# 使用字符串的拼接阻止程序报错
if [ "$1"x = "1"x ]
then
echo "num is one"
elif [ "$1"x = "2"x ]
then
echo "num is two"
else
echo "others"
fi
[root@hadoop100 shell]# ./if.sh 1
num is one
[root@hadoop100 shell]# ./if.sh 2
num is two
[root@hadoop100 shell]# ./if.sh 3
others
[root@hadoop100 shell]# ./if.sh
others

1.4 经验汇总

  • 当需要合并分支进行判断时,可以使用 -a,同时对2个条件进行判断
1
2
3
[root@hadoop100 shell]# a=19
[root@hadoop100 shell]# if [ $a -gt 18 -a $a -lt 30 ];then echo "young" ; fi
young
  • 使用 (( )) 可以直接在内部直接用数学运算符做判断
1
2
3
4
5
6
[root@hadoop100 shell]# a=1
[root@hadoop100 shell]# if (( $a > 2 ));then echo OK; else echo OMG;fi
OMG
[root@hadoop100 shell]# a=3
[root@hadoop100 shell]# if (( $a > 2 ));then echo OK; else echo OMG;fi
OK
  • 使用 { .. } 表示省略中间,直至某个阈值;
1
2
[root@hadoop100 shell]# for i in {1..100};do sum=$[$sum+$i];done;echo $sum
5050

二:case 语句

2.1 基本语法

1
2
3
4
5
6
7
8
9
10
11
12
case $变量名 in
"值 1")
如果变量的值等于值 1,则执行程序 1
;;
"值 2")
如果变量的值等于值 2,则执行程序 2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac

2.2 注意事项

  • case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束;
  • 双分号“;;”表示命令序列结束,相当于 java 中的 break;
  • 最后的“*)”表示默认模式,相当于 java 中的 default;

2.3 案例实操

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@hadoop100 shell]# cat case.sh 
#!/bin/bash
case $1 in
1)
echo "one"
;;
2)
echo "two"
;;
*)
echo "others"
;;
esac

[root@hadoop100 shell]# ./case.sh 1
one
[root@hadoop100 shell]# ./case.sh 2
two
[root@hadoop100 shell]# ./case.sh 3
others

三:for 循环

3.1 基本语法

1
2
3
4
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
1
2
3
4
for 变量 in 值 1 值 2 值 3…
do
程序
done

3.2 案例实操

1
2
3
4
5
6
7
8
9
[root@hadoop100 shell]# cat sum.sh 
#!/bin/bash
for (( i=0;i<=100;i++ ))
do
sum=$[$sum+$i]
done
echo $sum
[root@hadoop100 shell]# ./sum.sh
5050
1
2
3
4
5
6
7
8
9
10
[root@hadoop100 shell]# cat foreach.sh 
#!/bin/bash
for i in 12 18 24
do
echo "num is $i"
done
[root@hadoop100 shell]# ./foreach.sh
num is 12
num is 18
num is 24

比较$*和$@区别:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@hadoop100 shell]# cat diff.sh 
#!/bin/bash
echo '=== $* ==='
for i in $*
do
echo "letter is $i"
done

echo '=== $@ ==='

for i in $@
do
echo "letter is $i"
done
[root@hadoop100 shell]# ./diff.sh 123 qwe 456
=== $* ===
letter is 123
letter is qwe
letter is 456
=== $@ ===
letter is 123
letter is qwe
letter is 456

当它们被双引号“”包含时,$*会将所有的参数作为一个整体,以“$1 $2 …$n”的形式输 出所有参数;$@会将各个参数分开,以“$1” “$2”…“$n”的形式输出所有参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@hadoop100 shell]# cat diff.sh 
#!/bin/bash
echo '=== $* ==='
for i in "$*"
do
echo "letter is $i"
done

echo '=== $@ ==='

for i in "$@"
do
echo "letter is $i"
done
[root@hadoop100 shell]# ./diff.sh 123 qwe 456
=== $* ===
letter is 123 qwe 456
=== $@ ===
letter is 123
letter is qwe
letter is 456

四:while 循环

4.1 基本语法

1
2
3
4
while [ 条件判断式 ]
do
程序
done

4.2 案例实操

1
2
3
4
5
6
7
8
9
10
11
12
[root@hadoop100 shell]# cat while.sh 
#!/bin/bash
i=0
sum=0
while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[root@hadoop100 shell]# ./while.sh
5050
  • 使用 let 直接用数学运算符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@hadoop100 shell]# cat while.sh 
#!/bin/bash
i=0
sum=0
while [ $i -le 100 ]
do
#sum=$[$sum+$i]
#i=$[$i+1]
let sum+=i
let i++
done
echo $sum
[root@hadoop100 shell]# ./while.sh
5050