收录查询

[转] 4. Bourne Shell及shell编程

if [ ! -d "$1" ]
         then
                echo "$1 is not a directory"
                exit 2
         fi
 
         cd $1
 
         ls -a | cpio -o > /dev/rmt/0h
 
         if [ $? -eq 0 ]
         then
                rm -rf *
         else
                echo "A problem has occured in creating backup"
                echo "The directory will not be ereased"
                echo "Please check the backup device"
                exit 3
         fi
         # end of unload
 
         在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二

         设置程序的退出状态
 
      g. if嵌套及elif结构
        if command
        then
                command
        else
                if command
                then
                        command
                else
                        if command
                        then
                                command
                        fi

                fi
        fi
 
        改进:使用elif结构
        if command
        then
                command
        elif    command
        then
                command
        elif    command
        then
                command
        fi
 
        elif结构同if结构类似,但结构更清淅,其执行结果完全相同. g

 

Bourne Shell及Shell编程(2)
      h.交互式从键盘读入数据
        使用read语句,其格式如下:
 
        read var1 var2 ... varn
 
        read将不作变量替换,但会删除多余的空格,直到遇到第一个换行符(回车),
        并将输入值依次赋值给相应的变量。
 
        例:
        $ read var1 var2 var3
        Hello   my  friends
        $ echo $var1 $var2 $var3
        Hello my friends
        $ echo $var1
        Hello
        $ read var1 var2 var3
        Hello my dear friends
        $ echo $var3
        dear friends   <-输入多余变量时,输入值余下的内容赋给最后一个变量
        $ read var1 var2 var3
        Hello friends
        $ echo $var3
                        <- var3为空
        $
 
        在shell script中可使用read语句进行交互操作:
 
        ...
        #echo -n message 输出结果后不换行
        echo -n "Do you want to continue: Y or N"
        read ANSWER
 
        if [ $ANSWER=N -o $ANSWER=n ]
        then
                exit
        fi
 
      i. case结构:结构较elif-then结构更清楚
 
        比较if-then语句:
 
        if [ variable1 = value1 ]
        then
            command
            command
        elif [ variable1 = value2 ]
        then
            command
            command
        elif [ variable1 = value3 ]
        then
            command
            command
        fi
 
        相应的case结构:
 
        case value in
            pattern1)
               command
               command;;
            pattern2)
               command
               command;;
            ...
            patternn)
               command;
         esac
 
       * case语句只执行第一个匹配模式
 
       例:使用case语句建立一个菜单选择shell script
 
       #Display a menu
       echo _
       echo "1 Restore"
       echo "2 Backup"
       echo "3 Unload"
       echo
 
       #Read and excute the user's selection
       echo -n "Enter Choice:"
       read CHOICE
 
       case "$CHOICE" in
        1)      echo "Restore";;
posted @ 2006-08-21 06:19  ->  阅读(182)  评论(0)    收藏  举报