-- 菜单和多重选择 case ( echo dialog select ) 从/etc/init.d/httpd里拷的一段 case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac case "choice" in "var1" ) command ;; "var2" ) command ;; "var3" ) command ;; * ) command esac 例一:输入一个字符,判断是大写字母还是小写字母,还是数字,还是其它 例二:改写第一天的判断脚本 使用read让用户输入它的名字,性别(对性别进行判断),年龄(判断是否有18岁成年);在case里面再嵌套case菜单,使之选项更丰富 ============================================ dialog yum install dialog -y # dialog --msgbox 大家好 10 30 # dialog --menu 请选择你的性别 9 20 9 1 男 2 女 # dialog --menu 请选择你的手机品牌 10 30 10 1 苹果 2 三星 3 小米 2>/dev/pts/1 --弹出的对话框,你的选择默认会保存到错误输出,所以用2>去重定向 =============================================== select 建立菜单选项的另一种工具,它是从ksh引进的 PS3=" " select var in choice1 choice2 choice3 ... do echo "................." done 例五:用select做一个选择菜单 PS3="please choose what operation system you are using:" --PS3是select命令用来加入提示字符串的符号,(默认会使用#?) echo select os in xp vista windows7 linux unix do echo echo "your operation system is $os" echo break --这里不加break的话,就会一直循环让你选择 done -------------------------------------------- 函数 function_name () { command command } function function_name () { command command } 例:模拟用函数写一个服务启动,关闭,重启的脚本 (要求有start,stop,restart,reload,status,支持chkconfig等功能) --以sshd服务为例写的服务脚本 ==================================================================== 正则表达式 ( Regular Expression,在代码中常简写为regex、regexp或RE) . 一个字符 .. 两个字符 ^# 以#号开头 #$ 以#号结束 [abc] 包含a或b或c的都匹配 [^abc] 只要出现了abc这三个字母以外的字符就都匹配 --重点 ^[abc] 以a开头或以b开头或以c开头 ^[^abc] 不以a开头或不以b开头或不以c开头 a+ 匹配至少一个或多个a a* 匹配0或多个a 大写 [[:upper:]] [A-Z] 小写 [[:lower:]] [a-z] 字母 [[:alpha:]] [a-Z] 字母数字 [[:alnum:]] 空格或者制表符 [[:blank:]] 纯数字 [[:digit:]] [0-9] 标点符号 [[:punct:]] ========================================================================== 正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。 许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。 显示查出的行在原文件的行号加n参数 grep -n root /etc/passwd 反向查找加v参数 grep -v bin /etc/passwd 大小写不敏感加i参数 grep -ni root grep.txt 扩展grep # egrep "root|ftp|adm" /etc/passwd # grep -E "root|ftp|adm" /etc/passwd 统计root在/etc/passwd里出现了几次 grep -o root /etc/passwd |wc -l [root@li shell03]# cat 1.txt 111 222 333 444 555 666 [root@li shell03]# cat 2.txt aaa bbb 333 ccc ddd 555 [root@li shell03]# grep -f 1.txt 2.txt --找出两个文件里重复的行 333 555 # ifconfig |grep -A 1 vmnet vmnet1 Link encap:Ethernet HWaddr 00:50:56:C0:00:01 inet addr:1.1.1.1 Bcast:1.1.1.255 Mask:255.255.255.0 -- vmnet8 Link encap:Ethernet HWaddr 00:50:56:C0:00:08 inet addr:192.168.56.1 Bcast:192.168.56.255 Mask:255.255.255.0 查找出有rot或者是rat的行 grep -n r[oa]t grep.txt --注意的是,[]括号内不论几个字符,都是选一个 查找一个非r字符加oot连在一起的行 grep '[^r]oot' grep.txt 查找非小写字母连着一个oo的行 grep '[^a-z]oo' grep.txt --记住: [] 里的^为取反 ,外面的为以它开头 # grep ^[br][oa]t grep.txt grep [^a-z] grep.txt 查找不包含小写字母的行 --错误 查找非全部为小写字母的字符串行 --正确 查找至少包含一个非小写字母的字符行 --正确 [root@dns shell03]# grep '^[a-z]oot' grep.txt root boot [root@dns shell03]# grep '[^a-z]oot' grep.txt Root Boot [root@li test]# cat test root Root rot boot Rot [root@li test]# grep '[^A-Z]o' test root Root rot boot [root@li test]# grep '[^A-Z]oo' test root boot [root@li test]# grep '^[^A-Z]' test root rot boot 查找不以大写字母开头 grep '^[^[:upper:]]' grep.txt grep '^[^A-Z]' grep.txt grep -v '^[A-Z]' grep.txt 查找有数字的行 grep '[0-9]' grep.txt 或者 grep [[:digit:]] grep.txt 查找一个数字和一个字母连起来的行 grep -E '[0-9][a-Z]|[a-Z][0-9]' grep.txt 查找不以r开头的 grep -v ^r grep.txt grep ^[^r] grep.txt 查找以数字开头的 grep ^[0-9] grep.txt grep ^[[:digit:]] grep.txt 查找以大写字母开头的 grep ^[A-Z] grep.txt 或者 grep ^[[:upper:]] grep.txt 查找以小写字母开头的 grep ^[a-z] grep.txt 或者 grep ^[[:lower:]] grep.txt 查找以点结束的 grep "\."$ grep.txt --注意要引起来,而且要转义 去掉空格行 cat grep.txt |grep -v ^$ --以^$表示空格行 grep -v ^# /etc/vsftpd/vsftpd.conf --默认会留有一个空行 grep ^[^#] /etc/vsftpd/vsftpd.conf --没有空行,因为这里中括号代表会有一个字符,而空行是没有任何字符的,所以不匹配 查找完全匹配abc的行 grep ^abc$ grep.txt 查找到A后有三个数字的行 egrep A[0-9]{3} [root@li shell03]# egrep "A[0-9]*" grep.txt --查找A后有0到多个数字 abA2342sdfa A23dfgdfg sdfA1df dfgsdfA54353 3235Asfsfsdafas [root@li shell03]# egrep "A[0-9][0-9]*" grep.txt --查找A后有1到多个数字,所以相对于上面的显示,最后一行没有了(等同于egrep -n "A[0-9]+" grep.txt) abA2342sdfa A23dfgdfg sdfA1df dfgsdfA54353 --------------------------------------- . 点号代表一个任意字符 * 代表零个或者多个前字符 + 代表一个或者多个前字符 .* 代表0个或多个任意字符 ..* 代表非空的多个任意字符 [root@li test]# cat grep.txt ggle gogle google gooogle gagle gaagle gaaagle abcgef abcdef goagle aagoog wrqsg grep g. grep.txt grep g* grep.txt --结果比较怪 grep g.g grep.txt grep g*g grep.txt grep go.g grep.txt grep go.*g grep.txt grep go*g grep.txt grep go..*g grep.txt grep go.*.g grep.txt grep g*.*g grep.txt ==================================================================== 数组 数组的定义: [root@li shell03]# abc=( 1 2 a b "sfsda sf sfdsa" ) --定义一个数组,可以是数字,字母,或者字符串 [root@li shell03]# echo ${abc[0]} --第一个变量是0不是1 1 [root@li shell03]# echo ${abc[1]} 2 [root@li shell03]# echo ${abc[2]} a [root@li shell03]# echo ${abc[3]} b [root@li shell03]# echo ${abc[4]} sfsda sf sfdsa # echo ${abc[$[$RANDOM%5]]} --在这个数组里取随机数 --查看数组的全部值 [root@li shell03]# echo ${abc[*]} 1 2 a b sfsda sf sfdsa [root@li shell03]# echo ${abc[@]} 1 2 a b sfsda sf sfdsa --查看数组的个数 [root@li shell03]# echo ${#abc[*]} 5 [root@li shell03]# echo ${#a[*]} 3 [root@li shell03]# array3=(`ifconfig eth0|grep Mask`) [root@li shell03]# echo ${array3[0]} inet [root@li shell03]# echo ${array3[1]} addr:10.1.1.35 [root@li shell03]# echo ${array3[2]} Bcast:10.1.1.255 [root@li shell03]# echo ${array3[3]} Mask:255.255.255.0 产生1000个随机的手机号到一个文本,假设手机号前三位有130-139,158,155,186,189这十四种 array=( 130 131 132 133 134 135 136 137 138 139 155 158 186 189 ) for i in `seq 1000` do ran1=$[$RANDOM%14] head=${array[$ran1]} n1=$[$RANDOM%10] n2=$[$RANDOM%10] n3=$[$RANDOM%10] n4=$[$RANDOM%10] n5=$[$RANDOM%10] n6=$[$RANDOM%10] n7=$[$RANDOM%10] n8=$[$RANDOM%10] echo "$head$n1$n2$n3$n4$n5$n6$n7$n8" >> phonenum.txt done --用下面的命令验证一下产生的前三位数 [root@li shell05]# cat phonenum.txt | cut -c1-3|sort |uniq -c 72 130 81 131 91 132 65 133 73 134 48 135 70 136 79 137 80 138 61 139 75 155 64 158 70 186 71 189 ======================================================================= 1,使用read输入一个整数,把数字中的所有数字转换成英文单词 如:2345 转换为 two three four five 提示:需要计算输入的整数的长度,然后进行循环,截取整数中的单一字符(cut -c),对截取的单一字符 echo 整数 |sed 's/0/zero /g;......;s/9/night /g;' --这里使用sed替换十次 2:read输入一个目录,找出目录下包括子目录下为死链接的文件。(要求不使用find,使用函数进行递归查找子目录里的文件) 3:结合函数和case菜单功能,写一下关于usb挂载的脚本,(要实现的功能有挂载,卸载,列出挂载后内容,usb拷贝文件到系统,系统拷贝文件到usb,退出等六个功能) --或者你自己自定义写一个其它的菜单式的脚本 以/etc/passwd为例,使用正则表达式表示下面的题目 查找每行里第四个字符为t的行 # grep ^...t /etc/passwd 查找每行里倒数第六个字符为o的行 # grep o.....$ /etc/passwd 查找每行里有s和n字符,并且中间有两个字符的行 # grep -E "s..n|n..s" /etc/passwd 有一个文件类似/etc/passwd有7列,每列由:分隔,查找第三列为4的行(只使用grep和正则表达式,不使用cut或awk的截取) grep x:4: /etc/passwd --如果指定为/etc/passwd文件,则可以使用这句 --但如果是和题目要求一致的类似/etc/passwd的文件,则需要使用下面的方法 grep .*:.*:4:.*:.*:.*:.* /etc/passwd grep ^[^:]*:[^:]*:4: /etc/passwd 在上题基础上把第三列为4改成字符包括4的行 grep .*:.*:.*4.*:.*:.*:.*:.* /etc/passwd grep ^[^:]*:[^:]*:[^:]*4.*: /etc/passwd grep -v ^[^:]*:[^:]*:[^4:]*: /etc/passwd --如果一个文件有70列(以:分隔),我要找出第30列为空的行(用awk就好做了) awk -F: '$30=="" {print $0}' /etc/passwd 查找/etc/passwd里注释列不为空的行 grep .*:.*:.*:.*:..*:.*:.* /etc/passwd