linux shell学习笔记(四) 控制流结构

1、if语句

   语句格式:

   if 条件1         如果条件1为真

   then             那么

            命令1   执行命令1

   elif 条件2       如果条件1不成立

   then             那么

            命令2   执行命令2

   else             如果条件1,2均不成立

            命令3   那么执行命令3

   fi               完成

   注:if语句必须以单词fi终止

   例子:

   #!/bin/bash

   #iftest

   #this is a comment line,all comment lines start with a#

   if [ "10" -lt "12" ]

   then

       #yes 10 is less than 12

   echo "Yes,10 is less than 12"

   fi

 man test

   例2:

   #!/bin/bash

   #iftest2

   echo -n "Enter your name:"

   read NAME

   #did the user just hit return

   if [ "$NAME" == "" ];then

               echo "You did not enter any information"

   fi

   例3:

   #/bin/bash

   #ifcp

   if cp myfile.bak myfile;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" = "chinaitlab" ];then

               echo "Hello chinaitlab"

   else

               echo "You are not root or chinaitlab,buthi,$NAME"

   fi

2、case语句:case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行    相匹配的命令。

   case 值 in

   模式1)

       命令1

       ;;

   模式2)

       命令2

       ;;

   esac

   case取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;。模式匹配符*表示任意字符,?表示任意单字符,[..]表示类或范围中任意字符。

   例1:

   #!/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 between 1 and 3" >&2

       exit;

       ;;

   esac

3、for循环

   for循环一般格式为:

   for 变量名 in 列表

   do

       命令1

       命令2

   done

   当变量值在列表里,for循环即执行一次所有命令,使用变量名访问列表中取值,命令可为任何有效的shell命令和语句。变量名为任何单词。in列表用法是可选的,如果不用它,for循环使用命令行的位置参数,in列表可以包含替换、字符串和文件名

4、until循环

   until循环一般格式为:

   until条件

   do

       命令1

       命令2

   done

   注:条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次。

   例1:

   #!/bin/sh

   #until mon

   #监控分区

   Part="/backup"

   #得到磁盘使用百分比


   echo $LOOK_OUT

   until [ "$LOOK_OUT" -gt "90" ]

   do

       echo "Filesystem /bachup is nearly full" |mail root

       LOOK_OUT=`df|grep "$Part" |awk '{print $5}' |sed 's/%//g'`

       #每隔3600秒执行一次

       sleep 3600

   done

5、while循环

   while循环一般格式为:

   while命令

   do

       命令1

       命令2

       ...

   done

   注:在while和do之间虽然通常只使用一个命令,但可以放几个命令,命令通常用作测试条件

   例1:

   #!/bin/sh

   #whileread

   echo "按住<ctrl>+D退出输入。"

   while echo -n "输入你最喜欢的电影:";read FILM

   do

       echo "Yeah,${FILM}是一部好电影!"

   done

   例2:

   #!/bin/sh

   #whileread

   while read LINE

   do

       echo $LINE

   done < names.txt

6、break和continue控制

   break [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

你可能感兴趣的:(linux,start,comment,控制流结构)