Java教程

shell学习训练&&总结_7

本文主要是介绍shell学习训练&&总结_7,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

shell学习训练&&总结_7


1.统计班级学生各科成绩(shuxue,yuwen,yinyu)信息,并计算各科成绩的平均值和总和

#!/bin/bash
#本操作实现数组存储,学生和成绩。按q键退出
t=0
while true;do
read -p"input student name:" st
if [[ $st != q ]];then
	student[$t]=$st
else 
	break
fi
read -p"input math scores:" s1
if [[ $s1 != q ]];then
        shuxue[$t]=$s1
else	
	echo ${zongjie[@]}
        break
fi
read -p"input yuwen scores:" s2
if [[ $s2 != q ]];then
        yuwen[$t]=$2
else
	echo ${zongjie[@]}
        break
fi
read -p"input yinyu scores:" s3
if [[ $s3 != q ]];then
        yinyu[$t]=$s3
else
	echo ${zongjie[@]}
        break
fi
echo "student:${student[$t]} math scores:${shuxue[$t]} yuwen scores:${yuwen[$t]} yinyu scores:${yinyu[$t]}"

let t++ 
done
 

2.正则案例分析

datafile内容如下

Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

file

48   Dec    3BC1977    LPSX    68.00   LVX2A   138

483 Sept   5AP1996     USP    65.00   LVX2C   189

47    Oct      3ZL1998    LPSX   43.00   KVM9D 512

219  dec      2CC1999  CAD     23.00   PLV2C    68

484  nov      7PL1996   CAD    49.00     PLV2C  234

483   may     5PA1998  USP     37.00    KVM9D  644

216   sept      3ZL1998  USP     86.00    KVM9E   234

Question2:
1、显示/etc/passwd文件中以bash结尾的行;

egrep "bash$" /etc/passwd

2、找出/etc/passwd文件中的三位或四位数;

egrep "([0-9]){3,4}" /etc/passwd
#{}引用符
#[]引用字符,若引用123则格式:[1][2][3];若引用1或2或3则格式:[1]|[2]|[3]

3、找出/etc/grub2.cfg文件中,以至少一个空白字符开头,后面又跟了非空白字符的行

egrep "^ {1,}[^ ].*" /etc/grub2.cfg

4、找出"netstat -tan”命令的结果中,以‘LISTEN’后跟0或多个空白字符结尾的行;

netstat -tan|egrep "LISTEN[0]$|LISTEN +"
#或者
netstat -tan|egrep "LISTEN[0]$|LISTEN[[:space:]]+"
# +的意思为将前面相邻的字符至少匹配一次,相当于{1,}。
#[[:space:]]在正则中可以直接用空格代替。

5、找出"fdisk -l“命令的结果中,包含以/dev/后跟sd或hd及一个字母的行;

fdisk -l | egrep '/dev/(s|h)d[a-z]'
或者
fdisk -l | egrep '/dev/[hs]d[a-z]'
# []表示匹配中括号内的任一字符

6、找出”ldd /usr/bin/cat“命令的结果中文件路径;

ldd /usr/bin/cat|egrep -o "/.*"

7、找出/proc/meminfo文件中,所有以大写或小写s开头的行;至少用三种方式实现;

#1
egrep "^[sS]" /proc/meminfo

#2
egrep "^(s|S)" /proc/meminfo

#3
cat /proc/meminfo|egrep "^[sS]"

8、显示当前系统上root、centos或spark用户的相关信息;

egrep "(root|centos|spark)" /etc/passwd

9、echo输出一个绝对路径,使用egrep取出其基名;

echo /etc/passwd/world|egrep "[^/]+/?$"
#[^/]
#?:最多匹配一次,相当于{,1}
#/?$:意思是,匹配绝对路径最后1个或1个以下/的路径

10、找出ifconfig命令结果中的1-255之间的整数;

ifconfig |egrep -o "\<25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]\>"

11、找出系统中其用户名与shell名相同的用户。

egrep "^(.*):.*\1$" /etc/passwd

Question3:
1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;

egrep ^[hH] /etc/passwd

2、显示/etc/passwd中以sh结尾的行;

egrep "sh$" /etc/passwd

3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;

egrep "^# + {1,}.*" /etc/passwd
#或者
egrep "^#[[:space:]]+[[:space:]]{1,}.*" /etc/passwd

4、查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行;

egrep "^(to).*\1" /etc/rc.d/rc.local

5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;(ps:用“\<word\>”"\bworld\b"匹配字符)

egrep "\<s.*d$\>" /etc/inittab

6、查找ifconfig命令结果中的1-255之间的整数;

ip a |egrep -o "\<25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]\>"

7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行

egrep "Faild|FAILED" /var/log/secure

8、在/etc/passwd中取出默认shell为bash

egrep "^(.*).*\1$" /etc/passwd

9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息

egrep "^(ns).*\.(conf)$" /etc

10、高亮显示passwd文件中冒号,及其两侧的字符

egrep "[^:]" /etc/passwd

11、匹配/etc/services中开头结尾字母一样的单词

egrep  "\<(.)[a-zA-Z]*\1\>" /etc/services
这篇关于shell学习训练&&总结_7的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!