1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

2、显示/etc/passwd文件中不以/bin/bash结尾的行

3、显示/etc/passwd文件中ID号最大的用户的用户名

4、如果root用户存在,显示其默认的shell程序

5、显示/etc/passwd文件中两位或三位数

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行

9、 显示当前系统root,centos或user1用户的默认shell和uid

10、 找出/etc/rc.d/init.d/functions文件中(CentOS 6),某个单词后面跟一个小括号的行

11、 echo 输出一个路径,使用egrep取出其基名,

12、 找出ifcofnig命令结果中1-255中间的任意数值

13、 找出ifconfig命令结果中的ip地址。


1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

# egrep '^(s|S)' /proc/meminfo
# egrep -i '^s' /proc/meminfo

2、显示/etc/passwd文件中不以/bin/bash结尾的行

# egrep -v '/bin/bash$' /etc/passwd

3、显示/etc/passwd文件中ID号最大的用户的用户名

4、如果root用户存在,显示其默认的shell程序

# id root > /dev/null 2>&1 && egrep '^root\b' /etc/passwd | cut -d':' -f1,7 --output-delimiter='^_^'
root^_^/bin/bash

5、显示/etc/passwd文件中两位或三位数

# egrep -o '\<[[:digit:]]{2,3}\>' /etc/passwd

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

# egrep '^[[:space:]]+[^[:space:]]' rc.sysinit

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

# netstat -tan | egrep 'LISTEN[[:space:]]*'

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行

# egrep '(^[[:alnum:]]+\>).*\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:2018:2018::/home/nologin:/sbin/nologin

9、 显示当前系统root,centos或user1用户的默认shell和uid

# egrep '^(centos|root|user1)\>' /etc/passwd | cut -d':' -f1,7 --output-delimiter=' '
root /bin/bash
centos /bin/bash
user1 /bin/sh

10、 找出/etc/rc.d/init.d/functions文件中(CentOS 6),某个单词后面跟一个小括号的行

1)cat文件:有 字母 或 _ 符 和 () ,字符用 [ ]

# grep -E -o '[[:alpha:]_]+\(\)' /etc/rc.d/init.d/functions
# grep -o '[[:alpha:]_]\+()' /etc/rc.d/init.d/functions

11、 echo 输出一个路径,使用egrep取出其基名,

文件名的特性:<255, 非/ , . 开头为隐藏文件,区分大小写。

     [^/] :文件名,一定不能用/表示

1)取基名

# echo '/mnt/sdc' | egrep -o '[^/]+/?$'

2)取目录名

# echo '/mnt/sdc' | egrep -o '/[^/]+/'

12、 找出ifcofnig命令结果中1-255中间的任意数值

# ifconfig | egrep '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

13、 找出ifconfig命令结果中的ip地址。

# ifconfig | egrep '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
]# ifconfig | egrep '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

wKiom1mAFSjCGl5xAAAJgG6T_go924.png