控制流结构: if then else语句,case语句,for循环,until循环,while循环,break控制,continue控制。
1、  if语句格式:
if 条件1   如果条件1为真
then       那么
  命令1   执行命令1
elif 条件2  如果条件1不成立
then       那么
  命令2   执行命令2
else        如果条件1,2都不成立
  命令3   那么执行命令3
fi   //if语句必须以单词fi终止
也就是:if条件
        then 命令
        fi
例子1:
#!/bin/bash
#if test
if [ "15" -lt "12" ]
then
echo "Yes,10 is less than 12"
else
echo "No,15 is greater than 12"
fi
#man test   //查看test的相关帮助信息
例子2:
#!/bin/bash
#if test2
echo -n "Enter your name:"
read NAME
if [ "$NAME" == "" ];then
        echo "You did not enter any information!"
else
        echo "Your Name is $NAME"
fi
例子3:
#!/bin/bash
#ifcp
if cp myfile.bak myfile2;then
        echo "Good copy"
else
        echo "`basename $0`:error could not copy the files">&2
fi
例子4:
#!/bin/bash
#ifelif
echo -n "Enter your name:"
read NAME
if [ -z $NAME ] || [ "$NAME" = "" ];then
        echo "You did not enter a name."
elif [ "$NAME" = "root" ];then
        echo "Hello root"
elif [ "$NAME" = "mxh" ];then
        echo "Hello mxh"
else
        echo "You are not root and mxh,but hi,$NAME"
fi
2、  case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。
Case语句格式:
case 值 in
模式1)
     命令1
;;
模式2)
     命令2
;;
esac
case取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,期间所有命令开始执行直至;;。模式匹配符*表示任意字符,?表示任意单字符,[..]表示类或范围中任意字符。
例子:
#!/bin/bash
#case select
echo -n "Enter a number from 1 to 3:"
read ANS
case $ANS in
1)
        echo "You select 1"
        ;;
2)
        echo "You select 2"
        ;;
3)
        echo "You select 3"
        ;;
y|Y)
        echo "You select a $ANS"
        ;;
*)
        echo "`basename $0`:This number is not between 1 and 3" >&2
        exit;
        ;;
esac
3、  for循环格式:
for 变量名 in 列表
do
        命令1
        命令2
done
-----当变量值在列表里,for循环即执行一次所有命令,使用变量名访问列表中取值。命令可为任何有效地shell命令和语句。变量名为任何单词。in列表用法是可选的,如果不用它,for循环使用命令行的位置参数。in列表可以包含替换、字符串和文件名。
例子1:
#!/bin/bash
#forlist1
for loop in 1 2 3 4 5
do
        echo $loop
done
例子2:
#!/bin/bash
#forlist2
for loop in "orange red blue grey"
#for loop in `cat myfile`    //反引号里面的说明是以一个命令执行了
do
        echo $loop
done     //可以再用for loop in `cat myfile`试一下,然后改变myfile的内容,会发现for循环再读取的时候如果没有” ”号,则以空格作为分隔符
4、  until 循环的格式为:
until 条件
do
命令1
命令2
……
done
------条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次。
例子:
#!/bin/bash
#until_df
Part="/boot"
LOOK_OUT=`df |grep "$Part" |awk '{print $5}' |sed 's/%//g'`
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do
        echo "Filesystem /boot is nearly full" |mail root    //达到90%给root用户发邮件
        LOOK_OUT=`df |grep $Part |awk '{print $5}' |sed 's/%//g'`
        sleep 3600    //每一个小时执行一次
done
----这个脚本是检查磁盘/boot使用百分比的,所以比较适合那些数据量产生很大的服务器,可以作为cron任务,也可以使用nohup ./dfuntil &来放在后台执行。
5、  while 循环的格式为:
while 命令
do
命令1
命令2
……
done
-----在while和do之间虽然通常只使用一个命令,但可以放几个命令,命令通常做测试条件。
例子1:
#!/bin/bash
#whileread
echo "Press ctrl+D to exit enter."
while echo -n "Enter your faverate film:"
read FILM
do
        echo "Yeah,$FILM is a good film!"
done
例子2:
#!/bin/bash
#whileread2
while read LINE
do
        echo $LINE
done<term.txt   //从term.txt读出内容作为LINE的变量,而这个文件的输出必须放在这个位置,如果放在while语句的后面,将一直不停的读取term.txt文件的第一行。
6、  break和continue控制
bread [n]
----退出循环
----如果是在一个嵌入循环 里,可以指定n来跳出的循环个数。
continue
----跳过循环步
注:continue命令类似于break命令,只有一点重要差别,它不会跳出循环,只是跳过这个循环步。
例子1:
#!/bin/bash
#breakout
while :
do
        echo -n "Enter any number [1...5]:"
        read ANS
        case $ANS in
        1|2|3|4|5)
                echo "You enter a number between 1 and 5."
                ;;
        *)
                echo "Wrong number,Bye."
                break
                ;;
        esac
done
例子2:
#!/bin/bash
#breakout2
while :
do
        echo -n "Enter any number [1...5]:"
        read ANS
        case $ANS in
        1|2|3|4|5)
                echo "You enter a number between 1 and 5."
                ;;
        *)
                echo -n "Wrong number.continue(y/n)?:"
                read IS_CONTINUE
                case $IS_CONTINUE in
                y|yes|Y|YES)
                        continue
                        ;;
                *)
                        break
                        ;;
                esac
        esac
done