第四章   控制流结构  

 
 
简介
 
控制结构
if then else语句
case语句
for循环
 
until循环
while循环
break循环
ocntinue控制
 
 
 
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if语句格式
 
if 如果条件一为真,那么执行命令1
then
 
elif 如果条件一不成立,条件二为真,那么执行命令2
then
 
else 如果条件一二都不成立,那么执行命令3
fi 完成(if语句必须以fi终止)
 
 
 
 
 
例句:
 
 
 
#!/bin/bash
#if test
#this is a comment line,all comment lines start with a #
if [ "10" -1t "12" ]
then
 #yes 10 is less than 12
 echo "Yes,10 is less than 12"
else
 echo "No"
fi
 
 
 
 
 
例句2:
 
 
#!/bin/bash
#if test2
echo -n "Enter your name:"
read NAME
#did the user just hit return
if [ "$NAME" == "" ];then
        echo "You did not enter any information"
else
        echo "Your Name is ${NAME}"
fi
 
 
 
 
 
 
 
if
 
elif 
 
elif
 
else
 
fi
 
 
例:(注意[]两边都有空格
chmod 755 ifelif   
./ifelif
vi中fset nu可以显示行号)
 
#!/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" = "chinaitlab" ];then
        echo "Hello chinaitlab"
else
        echo You are not root or chinaitlab,but hi,"$NAME"
fi
 
 
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case语句
 
 
case 值 in       ----------------取值后面必须跟in
模式1) ---------------每个模式必须以 ) 结束
  命令1
  ;; ---------------终止提示符
模式2)
  命令2
  ;;
esac
 
 
 
 
例子:
 
#!/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"
  ;;
*)
  echo "`basename $0`: This is not beteen 1 and 3" >&2
  exit;
  ;;
esac
 
 
例子解释:
例子中*)表示除了1) 2) 3)外的所有其他情况
例子中可以在  2)  命令结束后打入
y|Y)
echo "You select $ANS"
;;
 
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for 循环   ----------- 将 列表 依次带入 变量名中,执行命令
 
for 变量名 in 列表
do
   命令1
   命令2
done
 
 
 
 
例:
 
 
#!/bin/bash
#forlist1
for loop in 1 2 3 4 5
do
 echo $loop
done
 
 
例子延伸:将上例中1 2 3 4 5 更换成 `cat forlist1` (``内的内容可以表示linux命令),  则
 
./forlist1之后屏出forlist1文件内容,注意for循环一次读入空格之间的字符。
 
 
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
until 循环
 
 
until  条件
do
  命令
  命令
 
done
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while 循环
 
while 命令 
 
do命令1
do命令2
 
done
 
 
 
 
例子:
 
#!/bin/sh
#whileread
echo "press ctrl +d to quit"
while echo -n "type ur fav movie";read FILM
do
        echo "Yeah,${FILM} is ur fav movie"
done
 
 
 
例子2:
 
 
#!/bin/sh
#whilereadline
while read LINE
do
        echo $LINE
done < names.txt
 
 
 
 
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
break & continue
 
 
 
 
 
ex:
 
~~~~~~~~~~~~
#!/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
 
~~~~~~~~~~~~~~
 
ex2:
 
 
~~~~~~~~~~~
 
 
#!/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 -n "Wrong number,continue <y/n>?:"
                read IS_CONTINUE
                case $IS_CONTINUE in
                        y|yes|Y|Yes)
                        continue
                        ;;
                *)
                        break
                        ;;
                esac
        esac
done
 
 
~~~~~~~~