if [ ! -d "$1" ] 6w6Linux联盟 then 6w6Linux联盟 echo "$1 is not a directory" 6w6Linux联盟 exit 2 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 cd $1 6w6Linux联盟 6w6Linux联盟 ls -a | cpio -o > /dev/rmt/0h 6w6Linux联盟 6w6Linux联盟 if [ $? -eq 0 ] 6w6Linux联盟 then 6w6Linux联盟 rm -rf * 6w6Linux联盟 else 6w6Linux联盟 echo "A problem has occured in creating backup" 6w6Linux联盟 echo "The directory will not be ereased" 6w6Linux联盟 echo "Please check the backup device" 6w6Linux联盟 exit 3 6w6Linux联盟 fi 6w6Linux联盟 # end of unload 6w6Linux联盟 6w6Linux联盟 在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二 6w6Linux联盟 是 6w6Linux联盟 设置程序的退出状态 6w6Linux联盟 6w6Linux联盟 g. if嵌套及elif结构 6w6Linux联盟 if command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 else 6w6Linux联盟 if command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 else 6w6Linux联盟 if command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 fi 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 改进:使用elif结构 6w6Linux联盟 if command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 elif command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 elif command 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 elif结构同if结构类似,但结构更清淅,其执行结果完全相同. g6w6Linux联盟Bourne Shell及Shell编程(2) 6w6Linux联盟 h.交互式从键盘读入数据 6w6Linux联盟 使用read语句,其格式如下: 6w6Linux联盟 6w6Linux联盟 read var1 var2 ... varn 6w6Linux联盟 6w6Linux联盟 read将不作变量替换,但会删除多余的空格,直到遇到第一个换行符(回车), 6w6Linux联盟 并将输入值依次赋值给相应的变量。 6w6Linux联盟 6w6Linux联盟 例: 6w6Linux联盟 $ read var1 var2 var3 6w6Linux联盟 Hello my friends 6w6Linux联盟 $ echo $var1 $var2 $var3 6w6Linux联盟 Hello my friends 6w6Linux联盟 $ echo $var1 6w6Linux联盟 Hello 6w6Linux联盟 $ read var1 var2 var3 6w6Linux联盟 Hello my dear friends 6w6Linux联盟 $ echo $var3 6w6Linux联盟 dear friends <-输入多余变量时,输入值余下的内容赋给最后一个变量 6w6Linux联盟 $ read var1 var2 var3 6w6Linux联盟 Hello friends 6w6Linux联盟 $ echo $var3 6w6Linux联盟 <- var3为空 6w6Linux联盟 $ 6w6Linux联盟 6w6Linux联盟 在shell script中可使用read语句进行交互操作: 6w6Linux联盟 6w6Linux联盟 ... 6w6Linux联盟 #echo -n message 输出结果后不换行 6w6Linux联盟 echo -n "Do you want to continue: Y or N" 6w6Linux联盟 read ANSWER 6w6Linux联盟 6w6Linux联盟 if [ $ANSWER=N -o $ANSWER=n ] 6w6Linux联盟 then 6w6Linux联盟 exit 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 i. case结构:结构较elif-then结构更清楚 6w6Linux联盟 6w6Linux联盟 比较if-then语句: 6w6Linux联盟 6w6Linux联盟 if [ variable1 = value1 ] 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 command 6w6Linux联盟 elif [ variable1 = value2 ] 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 command 6w6Linux联盟 elif [ variable1 = value3 ] 6w6Linux联盟 then 6w6Linux联盟 command 6w6Linux联盟 command 6w6Linux联盟 fi 6w6Linux联盟 6w6Linux联盟 相应的case结构: 6w6Linux联盟 6w6Linux联盟 case value in 6w6Linux联盟 pattern1) 6w6Linux联盟 command 6w6Linux联盟 command;; 6w6Linux联盟 pattern2) 6w6Linux联盟 command 6w6Linux联盟 command;; 6w6Linux联盟 ... 6w6Linux联盟 patternn) 6w6Linux联盟 command; 6w6Linux联盟 esac 6w6Linux联盟 6w6Linux联盟 * case语句只执行第一个匹配模式 6w6Linux联盟 6w6Linux联盟 例:使用case语句建立一个菜单选择shell script 6w6Linux联盟 6w6Linux联盟 #Display a menu 6w6Linux联盟 echo _ 6w6Linux联盟 echo "1 Restore" 6w6Linux联盟 echo "2 Backup" 6w6Linux联盟 echo "3 Unload" 6w6Linux联盟 echo 6w6Linux联盟 6w6Linux联盟 #Read and excute the user's selection 6w6Linux联盟 echo -n "Enter Choice:" 6w6Linux联盟 read CHOICE 6w6Linux联盟 6w6Linux联盟 case "$CHOICE" in 6w6Linux联盟 1) echo "Restore";; 6w6Linux联盟 2)
|