ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

shell学习训练&&总结_7

2022-01-23 14:00:59  阅读:201  来源: 互联网

标签:总结 字符 Lane shell egrep 23 passwd etc &&


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

标签:总结,字符,Lane,shell,egrep,23,passwd,etc,&&
来源: https://blog.csdn.net/weixin_45052781/article/details/122629448

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有