條件判斷式
1.if then
if [ 條件判斷式 ];then
表示式1
else
表示式2
fi
if [ 條件判斷式1 ];then
表示式1
elif [ 條件判斷式2 ];then
表示式2
else
表示式3
fi
迴圈:loop
while 當---時
while [ 條件1 ]
do 程式碼段1
done
#!/bin/bash
PATH=$path
export PATH
s=0
i=0
while [ "$i" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+....+100'is ==> $s"
結果演示:
[hadoop@hadoop scripts]$ sh sh07.sh
The result of '1+2+3+....+100'is ==> 5050
until 當條件不成立時
until [ 條件1 ]
do 程式碼段1
done
for do done 固定迴圈:求和
#!/bin/bash
PATH=$path
export PATH
read -p "請輸入要累加的範圍起始位置: " startnum
read -p "輸入累加範圍的終止位置: " endnum
s=0
i=0
if [ "$startnum" -lt "$endnum" ];then
for ((i=$startnum; i<=$endnum; i=i+1))
do
s=$(($s+$i))
done
echo "The result of '$startnum+($startnum+1)+...+$endnum' ==> $s"
else
echo "開始"
for((i=$endnum; i<=$startnum; i=i+1))
do
echo "$i"
s=$(($s+$i))
done
echo "The result of '$startnum +($startnum-1)+...+$endnum' ==> $s"
fi
結果:
[hadoop@hadoop scripts]$ sh sh08.sh
請輸入要累加的範圍起始位置: 6
輸入累加範圍的終止位置: 3
開始
3
4
5
6
The result of '6 +(6-1)+...+3' ==> 18
網址的遞增: for的兩種形式
#!/bin/bash
PATH=$path
export PATH
network1="192.168.136"
#先定義一個域的前面部分
#for ((i=1; i<=10; i=i+1))
for i in {1..10}
do
echo "$network1.$i"
done
結果演示
[hadoop@hadoop scripts]$ sh sh09.sh
192.168.136.1
192.168.136.2
192.168.136.3
192.168.136.4
192.168.136.5
192.168.136.6
192.168.136.7
192.168.136.8
192.168.136.9
192.168.136.10