第一部分 Shell基础编程——第四章 控制流结构

来源:互联网 发布:linux who am i 编辑:程序博客网 时间:2024/06/05 22:50

笔记

#流控制是什么#if语句#if语句必须以单词fi终止#if条件、then语句、fi#编辑iftestvi iftest#查看iftest内容cat iftest#!/bin/bash#if test#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#改变权限[root@localhost 0402]# chmod 755 iftest#执行[root@localhost 0402]# ./iftestYes, 10 is less than 12#查看test命令帮助信息man test#编辑iftest2vi iftest2#查看iftest2内容cat iftest2#!/bin/bash#iftest2echo -n "Enter your name:"read NAME#did the user just hit returnif [ "$NAME" == "" ];then        echo "You did not enter any infomation"else        echo "Your name is ${NAME}"#改变权限chmod 755 iftest2#执行[root@localhost 0410]# ./iftest2 Enter your name:ChinaItlabYour name is ChinaItlab[root@localhost 0410]# ./iftest2Enter your name:You did not enter any information#新建ifcpvi ifcp#查看ifcp内容[root@localhost 0410]# cat ifcp #!/bin/bash#ifcpif cp myfile.bak myfile;then        echo "good copy"else        echo "`basename $0`:error could not copy the files" >&2fi#改变权限chmod 755 ifcp#执行[root@localhost 0410]# ./ifcp cp: cannot stat `myfile.bak': No such file or directoryifcp:error could not copy the files#新建ifelifvi ifelif#改变权限chmod 755 ifelif#查看ifelif内容[root@localhost 0410]# cat ifelif #!/bin/bash#ifelifecho -n "Enter your name:"read NAMEif [ -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 wgb,but hi,$NAME"fi#执行[root@localhost 0410]# ./ifelif Enter your name:jikeYou are not root or wgb,but hi,jike[root@localhost 0410]# ./ifelif Enter your name:rootHello root[root@localhost 0410]# ./ifelif Enter your name:chinaitlabHello chinaitlab[root@localhost 0410]# ./ifelif Enter your name:You did not enter a name.#vi编辑时设置行号:set nu#case语句#新建caseselectvi caseselect#改变权限chmod 755 caseselect#查看caseselect内容[root@localhost 0410]# cat caseselect #!/bin/bash#case selectecho -n "Enter a number from 1 to 3:"read ANScase $ANS in1)        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#执行[root@localhost 0410]# ./caseselect Enter a number from 1 to 3:45caseselect:This is not between 1 and 3[root@localhost 0410]# ./caseselect Enter a number from 1 to 3:2You select 2[root@localhost 0410]# ./caseselect Enter a number from 1 to 3:3You select 3#重新修改,然后查看内容[root@localhost 0410]# cat caseselect #!/bin/bash#case selectecho -n "Enter a number from 1 to 3:"read ANScase $ANS in1)        echo "You select 1";;2)        echo "You select 2";;3)        echo "You select 3";;y|Y)        echo "You select $ANS";;*)        echo "`basename $0`:This is not between 1 and 3" >&2        exit;;;esac#执行[root@localhost 0410]# ./caseselect Enter a number from 1 to 3:YYou select Y[root@localhost 0410]# ./caseselect Enter a number from 1 to 3:yYou select y#for循环#新建forlist1vi forlist1#查看forlist1内容[root@localhost 0410]# cat forlist1#!/bin/bash#forlist1for loop in 1 2 3 4 5do        echo $loopdone#改变权限[root@localhost 0410]# chmod 755 forlist1#执行[root@localhost 0410]# ./forlist1 12345#将执行结果重定向到list.txt[root@localhost 0410]# ./forlist1 >list.txt#查看list.txt内容[root@localhost 0410]# cat list.txt 12345#新建forlist2vi forlist2#查看forlist2内容 [root@localhost 0410]# cat forlist2#!/bin/bash#forlist2#有双引号for loop in "orange red bue grey"do        echo $loopdone#改变权限[root@localhost 0410]# chmod 755 forlist2#执行[root@localhost 0410]# ./forlist2 orange red bue grey#拷贝文件cp forlist2 forlist3#编辑forlist3vi forlist3#查看forlist3内容[root@localhost 0410]# cat forlist3#!/bin/bash#forlist2#没有双引号for loop in orange red bue greydo        echo $loopdone#执行[root@localhost 0410]# ./forlist3orangeredbuegrey#拷贝文件cp forlist2 forlist4#编辑forlist4vi forlist4#新建myfilevi myfile#查看forlist4内容[root@localhost 0410]# cat forlist4#!/bin/bash#forlist2for loop in `cat myfile`do        echo $loopdone#执行forlist4[root@localhost 0410]# ./forlist4parmfindfile#查看myfile内容[root@localhost 0410]# cat myfile parmfindfile#编辑myfilevi myfile#重新查看[root@localhost 0410]# cat myfile parm 1 2findfile#执行[root@localhost 0410]# ./forlist4parm12findfile#说明是以空格或者换行作为划分的#until循环#新建until_monvi until_mon#查看until_mon内容[root@localhost 0410]# cat until_mon #!/bin/s#until_mon#监控分区Part="/backup"#得到磁盘使用的百分比LOOK_OUT=`df|grep "$Part"|awk '{print $5}'|sed 's/%//g'`echo $LOOK_OUTuntil [ "$LOOK_OUT"-gt"90" ]do        echo "Filesystem /backup is nearly full" |mail rootdone#改变权限chmod 755 until_mon#执行[root@localhost 0410]# ./until_mon#重新编辑until_monvi until_mon#后台执行until_monnohup ./until_mon#查看until_mon[root@localhost 0410]# cat until_mon #!/bin/sh#until_mon#监控分区Part="/backup"#得到磁盘使用的百分比LOOK_OUT=`df|grep "$Part"|awk '{print $5}'|sed 's/%//g'`echo $LOOK_OUTuntil [ "$LOOK_OUT"-gt"90" ]do        echo "Filesystem /backup is nearly full" |mail root        LOOK_OUT=`df|grep "$Part"|awk '{print $5}'|sed 's/%//g'`        sleep 3600done#while循环#新建whilereadvi whileread#改变权限chmod 755 whileread#改变权限[root@localhost 0410]# chmod 755 whileread#查看whileread[root@localhost 0410]# cat whileread#!/bin/sh#whilereadecho "按住<crtl>+D退出输入。"while echo -n "输入你最喜欢的电影:";read FILMdo        echo "Yeah,${FILM}是一部好电影!"done#执行[root@localhost 0410]# ./whileread 按住<crtl>+D退出输入。输入你最喜欢的电影:KAT Yeah,KAT是一部好电影!输入你最喜欢的电影:KATYeah,KAT是一部好电影!输入你最喜欢的电影:KATYeah,KAT是一部好电影!输入你最喜欢的电影:#新建whilereadlinevi whilereadline#查看whilereadlinecat whilereadline#修改权限chmod 755 whilereadline#查看names.txt,没有改文件[root@localhost 0410]# cat names.txtcat: names.txt: No such file or directory#重新查看,已输入内容[root@localhost 0410]# cat names.txtshenzhenshanghabeijing#查看whilereadline内容[root@localhost 0410]# cat whilereadline#!/bin/sh#whileraadwhile read LINEdo        echo $LINEdone < names.txt#执行[root@localhost 0410]# ./whilereadline shenzhenshanghabeijing#拷贝文件cp whilereadline whilereadline2#编辑whilereadline2vi whilereadline2#查看whilereadline2内容[root@localhost 0410]# cat whilereadline2#!/bin/sh#whileraadwhile read LINE <names.txtdo        echo $LINEdone#执行./whilereadline2shenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhenshenzhen#break和continue控制#新建breakoutvi breakout#改变权限[root@localhost 0410]# chmod 755 breakout#查看breakout内容[root@localhost 0410]# cat breakout #!/bin/bash#breakoutwhile :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;                ;;        esacdone#执行[root@localhost 0410]# ./breakout Enter any number[1...5]:4You enter a number between 1 and 5.Enter any number[1...5]:3You enter a number between 1 and 5.Enter any number[1...5]:54Wrong number,Bye.#拷贝文件cp breakout breakout2#新建breakout2vi breakout2#查看breakout2内容[root@localhost 0410]# cat breakout2#!/bin/bash#breakoutwhile :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:        esacdone#执行[root@localhost 0410]# ./breakout2Enter any number[1...5]:1You enter a number between 1 and 5.Enter any number[1...5]:3You enter a number between 1 and 5.Enter any number[1...5]:3You enter a number between 1 and 5.Enter any number[1...5]:56Wrong number,continue(y/n)?:yEnter any number[1...5]:432Wrong number,continue(y/n)?:n[root@localhost 0410]# ./breakout2Enter any number[1...5]:5You enter a number between 1 and 5.Enter any number[1...5]:45Wrong number,continue(y/n):yesEnter any number[1...5]:n


 

 

附图

 

 

 

katoonSina  CSDN@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]
原创粉丝点击