笔记
#流控制是什么 #if语句 #if语句必须以单词fi终止 #if条件、then语句、fi #编辑iftest vi 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]# ./iftest Yes, 10 is less than 12 #查看test命令帮助信息 man test #编辑iftest2 vi iftest2 #查看iftest2内容 cat iftest2 #!/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 infomation" else echo "Your name is ${NAME}" #改变权限 chmod 755 iftest2 #执行 [root@localhost 0410]# ./iftest2 Enter your name:ChinaItlab Your name is ChinaItlab [root@localhost 0410]# ./iftest2 Enter your name: You did not enter any information #新建ifcp vi ifcp #查看ifcp内容 [root@localhost 0410]# cat ifcp #!/bin/bash #ifcp if cp myfile.bak myfile; then echo "good copy" else echo "`basename $0`:error could not copy the files" >&2 fi #改变权限 chmod 755 ifcp #执行 [root@localhost 0410]# ./ifcp cp: cannot stat `myfile.bak': No such file or directory ifcp:error could not copy the files #新建ifelif vi ifelif #改变权限 chmod 755 ifelif #查看ifelif内容 [root@localhost 0410]# cat ifelif #!/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 wgb,but hi,$NAME" fi #执行 [root@localhost 0410]# ./ifelif Enter your name:jike You are not root or wgb,but hi,jike [root@localhost 0410]# ./ifelif Enter your name:root Hello root [root@localhost 0410]# ./ifelif Enter your name:chinaitlab Hello chinaitlab [root@localhost 0410]# ./ifelif Enter your name: You did not enter a name. #vi编辑时设置行号 :set nu #case语句 #新建caseselect vi caseselect #改变权限 chmod 755 caseselect #查看caseselect内容 [root@localhost 0410]# cat caseselect #!/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 #执行 [root@localhost 0410]# ./caseselect Enter a number from 1 to 3:45 caseselect:This is not between 1 and 3 [root@localhost 0410]# ./caseselect Enter a number from 1 to 3:2 You select 2 [root@localhost 0410]# ./caseselect Enter a number from 1 to 3:3 You select 3 #重新修改,然后查看内容 [root@localhost 0410]# cat caseselect #!/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" ;; 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:Y You select Y [root@localhost 0410]# ./caseselect Enter a number from 1 to 3:y You select y #for循环 #新建forlist1 vi forlist1 #查看forlist1内容 [root@localhost 0410]# cat forlist1 #!/bin/bash #forlist1 for loop in 1 2 3 4 5 do echo $loop done #改变权限 [root@localhost 0410]# chmod 755 forlist1 #执行 [root@localhost 0410]# ./forlist1 1 2 3 4 5 #将执行结果重定向到list.txt [root@localhost 0410]# ./forlist1 >list.txt #查看list.txt内容 [root@localhost 0410]# cat list.txt 1 2 3 4 5 #新建forlist2 vi forlist2 #查看forlist2内容 [root@localhost 0410]# cat forlist2 #!/bin/bash #forlist2 #有双引号 for loop in "orange red bue grey" do echo $loop done #改变权限 [root@localhost 0410]# chmod 755 forlist2 #执行 [root@localhost 0410]# ./forlist2 orange red bue grey #拷贝文件 cp forlist2 forlist3 #编辑forlist3 vi forlist3 #查看forlist3内容 [root@localhost 0410]# cat forlist3 #!/bin/bash #forlist2 #没有双引号 for loop in orange red bue grey do echo $loop done #执行 [root@localhost 0410]# ./forlist3 orange red bue grey #拷贝文件 cp forlist2 forlist4 #编辑forlist4 vi forlist4 #新建myfile vi myfile #查看forlist4内容 [root@localhost 0410]# cat forlist4 #!/bin/bash #forlist2 for loop in `cat myfile` do echo $loop done #执行forlist4 [root@localhost 0410]# ./forlist4 parm findfile #查看myfile内容 [root@localhost 0410]# cat myfile parm findfile #编辑myfile vi myfile #重新查看 [root@localhost 0410]# cat myfile parm 1 2 findfile #执行 [root@localhost 0410]# ./forlist4 parm 1 2 findfile #说明是以空格或者换行作为划分的 #until循环 #新建until_mon vi 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_OUT until [ "$LOOK_OUT"-gt"90" ] do echo "Filesystem /backup is nearly full" |mail root done #改变权限 chmod 755 until_mon #执行 [root@localhost 0410]# ./until_mon #重新编辑until_mon vi until_mon #后台执行until_mon nohup ./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_OUT until [ "$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 3600 done #while循环 #新建whileread vi whileread #改变权限 chmod 755 whileread #改变权限 [root@localhost 0410]# chmod 755 whileread #查看whileread [root@localhost 0410]# cat whileread #!/bin/sh #whileread echo "按住<crtl>+D退出输入。" while echo -n "输入你最喜欢的电影:"; read FILM do echo "Yeah,${FILM}是一部好电影!" done #执行 [root@localhost 0410]# ./whileread 按住<crtl>+D退出输入。 输入你最喜欢的电影:KAT Yeah,KAT是一部好电影! 输入你最喜欢的电影:KAT Yeah,KAT是一部好电影! 输入你最喜欢的电影:KAT Yeah,KAT是一部好电影! 输入你最喜欢的电影: #新建whilereadline vi whilereadline #查看whilereadline cat whilereadline #修改权限 chmod 755 whilereadline #查看names.txt,没有改文件 [root@localhost 0410]# cat names.txt cat: names.txt: No such file or directory #重新查看,已输入内容 [root@localhost 0410]# cat names.txt shenzhen shangha beijing #查看whilereadline内容 [root@localhost 0410]# cat whilereadline #!/bin/sh #whileraad while read LINE do echo $LINE done < names.txt #执行 [root@localhost 0410]# ./whilereadline shenzhen shangha beijing #拷贝文件 cp whilereadline whilereadline2 #编辑whilereadline2 vi whilereadline2 #查看whilereadline2内容 [root@localhost 0410]# cat whilereadline2 #!/bin/sh #whileraad while read LINE <names.txt do echo $LINE done #执行 ./whilereadline2 shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen shenzhen #break和continue控制 #新建breakout vi breakout #改变权限 [root@localhost 0410]# chmod 755 breakout #查看breakout内容 [root@localhost 0410]# cat breakout #!/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 #执行 [root@localhost 0410]# ./breakout Enter any number[1...5]:4 You enter a number between 1 and 5. Enter any number[1...5]:3 You enter a number between 1 and 5. Enter any number[1...5]:54 Wrong number,Bye. #拷贝文件 cp breakout breakout2 #新建breakout2 vi breakout2 #查看breakout2内容 [root@localhost 0410]# cat breakout2 #!/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 #执行 [root@localhost 0410]# ./breakout2 Enter any number[1...5]:1 You enter a number between 1 and 5. Enter any number[1...5]:3 You enter a number between 1 and 5. Enter any number[1...5]:3 You enter a number between 1 and 5. Enter any number[1...5]:56 Wrong number,continue(y/n)?:y Enter any number[1...5]:432 Wrong number,continue(y/n)?:n [root@localhost 0410]# ./breakout2 Enter any number[1...5]:5 You enter a number between 1 and 5. Enter any number[1...5]:45 Wrong number,continue(y/n):yes Enter any number[1...5]:n
附图
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客] |
作者:Wentasy 发表于2013-4-11 17:03:28 原文链接
阅读:28 评论:0 查看评论