700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 结构化命令case和for while循环

结构化命令case和for while循环

时间:2020-12-24 10:34:17

相关推荐

结构化命令case和for while循环

一、 流程控制语句:case

控制语句即用来实现对程序流程的选择、循环、转向和返回等进行控制。case是开关语句的一个组成部分;它是根据变量的不同进行取值比较,然后针对不同的取值分别执行不同的命令操作适用于多分支,是一个多选择语句case变量值in模式1)命令序列1命令序列2;;模式2)命令序列2;;……*)默认命令序列esaccase“找对象的条件”in有钱);;家里有房|还是两套);;*)算了esac执行流程:首先使用“变量值”与模式1进行比较,若取值相同则执行模式1后的命令序列,直到遇见双分号“;; ”后跳转至esac,表示分支结束;若与模式1不相匹配,则继续与模式2进行比较,若取值相同则执行模式2后的命令序列,直到遇见双分号“;; ”后跳转至esac,表示结束分支,……依次类推,若找不到任何匹配的值,则执行默认模式“*) ”后的命令序列,直到遇见esac后结束分支注意事项:u取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;u匹配中的值可以是多个值,通过“|”来分隔u取值将检测匹配每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号*捕获该值,再执行后面的命令u它需要一个esac(就是case反过来)作为结束标记,每个case分支用右圆括号,用两个分号表示break范例1:用户输入0-9任意一个数字,通过case来判断用户输入的是哪一个数字?[root@xuegod1603 days3]# cat case-1.sh#!/bin/bashread -p "Type a num ==> " NUMcase $NUM in1)echo "Input a num is 1 ";;2)echo "Input a num is 2 ";;[3-8])echo "Input a num is $NUM";;9|0)echo "Input a num is $NUM";;*)echo "Please input [0-9]"esac[root@xuegod1603 days3]# sh case-1.shType a num ==> 2Input a num is 2[root@xuegod1603 days3]# sh case-1.shType a num ==> 6Input a num is 6[root@xuegod1603 days3]# sh case-1.shType a num ==> 0Input a num is 0[root@xuegod1603 days3]# sh case-1.shType a num ==> aaaPlease input [0-9]我们也可以用If语句来实现[root@xuegod1603 days3]# cat if-1.sh#!/bin/bashread -p "Please input a num " NUMif [ $NUM -eq 1 ];thenecho "THE NUM IS $NUM"elif [ $NUM -eq 2 ];thenecho "THE NUM IS $NUM"elif [ $NUM -ge 3 -a $NUM -le 8 ];thenecho "THE NUM IS $NUM"elif [ $NUM -eq 0 -o $NUM -eq 9 ];thenecho "THE NUM IS $NUM"elseecho "INPUT A NUM [0-9]"fi[root@xuegod1603 days3]# sh if-1.shPlease input a num 3THE NUM IS 3[root@xuegod1603 days3]# sh if-1.shPlease input a num 0THE NUM IS 0[root@xuegod1603 days3]# sh if-1.shPlease input a num 6THE NUM IS 6[root@xuegod1603 days3]# sh if-1.shPlease input a num fwerif-1.sh: line 3: [: fwer: integer expression expectedif-1.sh: line 5: [: fwer: integer expression expectedif-1.sh: line 7: [: fwer: integer expression expectedif-1.sh: line 9: [: fwer: integer expression expectedINPUT A NUM [0-9][root@xuegod1603 days3]# sh if-1.shPlease input a num 56INPUT A NUM [0-9]范例2:启动HTTPD服务[root@xuegod1603 days3]# cat case-http.sh#!/bin/bashcase $1 instart)systemctl $1 httpdps -ef|grep -v grep |grep httpd &> /dev/nullif [ $? -eq 0 ];thenecho "HTTPD is start...."fi;;stop)systemctl $1 httpdecho "httpd stop....";;status)systemctl $1 httpd;;restart)systemctl $1 httpd;;*)echo "USAGE:`basename $0` start|stop|status|restart"esac扩展:shell中色彩的处理shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e(可以用来识别我们的转义字符)格式:echo -e\033[背景颜色;文字颜色xxxx\033[0mecho -e\e[背景颜色;文字颜色xxxx\e[0m例子:其中42的位置代表底色,34的位置代表的是字的颜色,0m是清除所有格式注:1、字背景颜色和文字颜色之间是英文的分号";"2、文字颜色后面有个m3、字符串前后可以没有空格,如果有的话,输出也是同样有空格4、echo显示带颜色,需要使用参数-e下面是文字颜色:echo -e "\033[30m 黑色字 \033[0m"echo -e "\033[31m 红色字 \033[0m"echo -e "\033[32m 绿色字 \033[0m"echo -e "\033[33m 黄色字 \033[0m"echo -e "\033[34m 蓝色字 \033[0m"echo -e "\033[35m 紫色字 \033[0m"echo -e "\033[36m 天蓝字 \033[0m"echo -e "\033[37m 白色字 \033[0m"下面是背景颜色:echo -e "\033[40;37m 黑底白字 \033[0m"echo -e "\033[41;37m 红底白字 \033[0m"echo -e "\033[42;37m 绿底白字 \033[0m"echo -e "\033[43;37m 黄底白字 \033[0m"echo -e "\033[44;37m 蓝底白字 \033[0m"echo -e "\033[45;37m 紫底白字 \033[0m"echo -e "\033[46;37m 天蓝底白字 \033[0m"echo -e "\033[47;30m 白底黑字 \033[0m"控制选项:\033[0m 关闭所有属性\033[1m 设置高亮度,加粗\033[5m 闪烁范例3:判断不同的物品用不同的颜色来表示[root@xuegod1603 days3]# cat case-3.sh#!/bin/bash#cat <<eof#===============# 1. book# 2. pear# 3. cat# =============#eofecho '==============1.book2.pear3.cat============='read -p "Please a num : " NUMcase “$NUM” in1|book)echo -e "\e[1;31mbook\e[0m";;2|pear)echo -e "\e[1;32mpear\e[0m";;3)echo -e "\e[1;33mcat\e[0m";;*)echo "Pleas is a num"esac

二、循环语句

1、for-do-done语法格式:for变量名in变量取值列表docommands ifdone======================for var in list;docommandsdone取值列表有多种取值方式,比如(1)可以直接读取IN后面的值,默认以空格做分隔[root@xuegod1603 days3]#[root@xuegod1603 days3]# cat for-1.sh#!/bin/bashfor var in a b cdoecho the text is $vardone[root@xuegod1603 days3]# sh for-1.shthe text is athe text is bthe text is c(2)列表中的复杂值,注意双引号和转义字符的使用[root@xuegod1603 days3]# cat for-2.sh#!/bin/bashfor i in a b "xue god" c "d e"doecho the text is $idone[root@xuegod1603 days3]# sh for-2.shthe text is athe text is bthe text is xue godthe text is cthe text is d e[root@xuegod1603 days3]# cat for-3.sh#!/bin/bashfor i in a b "xue god" c "It's man"doecho the test is $idone[root@xuegod1603 days3]# sh for-3.shthe test is athe test is bthe test is xue godthe test is cthe test is It's man[root@xuegod1603 days3]# vim for-3.sh[root@xuegod1603 days3]# cat for-3.sh#!/bin/bashfor i in a b "xue god" c It\'s mandoecho the test is $idone[root@xuegod1603 days3]# sh for-3.shthe test is athe test is bthe test is xue godthe test is cthe test is It'sthe test is man(3)从变量中取值[root@xuegod1603 days3]# cat for-4.sh#!/bin/bashlist="a1 b1 c1 d1"for i in $listdoecho $idone[root@xuegod1603 days3]# sh for-4.sha1b1c1d1(4)从命令中取值[root@xuegod1603 days3]# cat for-5.sh#!/bin/bashfor i in `cat /etc/hosts`doecho $idonefor i in $(ls /mnt)doecho$idone[root@xuegod1603 days3]# sh for-5.sh127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6CentOS_BuildTagEFIEULAGPLimagesisolinuxLiveOSPackagesrepodataRPM-GPG-KEY-CentOS-7RPM-GPG-KEY-CentOS-Testing-7TRANS.TBL(5)字段分隔符默认情况下,base shell会把空格,制表符,换行符,我们也可以通过IFS来指定其中的一种做为分隔符[root@xuegod1603 days3]# cat for-6.sh#!/bin/bashIFS=$\nIFS='#IFS=里面直接回车'for i in `cat /etc/hosts`doecho $idone[root@xuegod1603 days3]# sh for-6.sh127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6[root@xuegod1603 days3]# cat for-7.sh#!/bin/bashIFS=:list=$(head -1 /etc/passwd)for i in $listdoecho $idone[root@xuegod1603 days3]# sh for-7.shrootx00root/root/bin/bash[root@xuegod1603 days3]# head -1 /etc/passwdroot:x:0:0:root:/root:/bin/bash注意:IFS的使用可以单独指定IFS=:IFS=$\n可以指定多个IFS=$\n:;(6)C语言风格的for例1:单个变量[root@xuegod1603 days3]# cat for-8.sh#!/bin/bash#for-cfor ((i=0;i<10;i++))doecho Num is $idone[root@xuegod1603 days3]# sh for-8.shNum is 0Num is 1Num is 2Num is 3Num is 4Num is 5Num is 6Num is 7Num is 8Num is 9例2:多个变量[root@xuegod1603 days3]# cat for-9.sh#!/bin/bashfor ((a=2,b=4;a<8;a++,b--))doecho num is "$a - $b"done[root@xuegod1603 days3]# sh for-9.shnum is 2 - 4num is 3 - 3num is 4 - 2num is 5 - 1num is 6 - 0num is 7 - -1扩展:数字列表的获取方式[root@xuegod1603 days3]# cat for-10.sh#!/bin/bashfor i in {5..10}doecho $idoneecho =============for i in `seq 2 8`doecho $idone命令:seq-w补全[root@xuegod1603 days3]# seq -w 6 10意思是如果最大数是两位,不足两位的用0补起0607080910步长,跨度[root@xuegod1603 days3]# seq 2 2 7从2开始计算,每次加2,最大到7246

2、while-do-done

重复测试指令的条件,只要条件成立就反复执行对应的命令操作,直到命令不成立或为假;语法格式如下:while测试命令do命令done注意:避免陷入死循环while true while false ,循环退出根据测试条件的退出码来定范例1:[root@xuegod1603 days3]# cat while-1.sh#!/bin/bashvar=10while [ $var -gt 0 ]doecho $varvar=$[$var-1]done[root@xuegod1603 days3]# sh while-1.sh10987654321范例2:自增操作:let var++自减操作:let var--[root@xuegod1603 days3]# cat while-2.sh#!/bin/bashnum=1while [ $num -lt 10 ]dosum=$(expr $num \* $num)echo "$num * $num = $sum"let num++done[root@xuegod1603 days3]# sh while-2.sh1 * 1 = 12 * 2 = 43 * 3 = 94 * 4 = 165 * 5 = 256 * 6 = 367 * 7 = 498 * 8 = 649 * 9 = 81[root@xuegod1603 days3]# cat while-3.sh#!/bin/bashnum=1while [ $num -lt 5 ]dosum=$(expr $num \* $num)echo "$num * $num = $sum"#let num++((num++))done[root@xuegod1603 days3]# sh while-2.sh1 * 1 = 12 * 2 = 43 * 3 = 94 * 4 = 16在学习过程中有遇到问题的同学,希望有一个可以答疑解惑的地方,可以点击加入我们,一起学习吧! Linux公开课

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。