Ex01
以冒号分隔, 取出/etc/passwd
文件的第14至第17行, 并将这些信息按第3个字段的数值大小进行排序, 最后仅显示的各自的第1个字段
<Method.1>
[root@Study ~]# cat -n /etc/passwd | head -n 17 | tail -n +14 | sort -t: -k3 -n | cut -d: -f1
14 ftp
17 vcsa
16 dbus
15 nobody
<Method.2>
[root@Study ~]# cat -n /etc/passwd | sed -n '14,17p' | sort -t: -k3 -n | cut -d: -f1
14 ftp
17 vcsa
16 dbus
15 nobody
Ex02
创建用户gentoo
, 附加组为distro
和linux
, 默认shell为/bin/csh
, 注释信息为"Gentoo Distribution"
[root@Study ~]# useradd -G distro,linux -c "Gentoo Distribution" -s /bin/csh gentoo
[root@Study ~]# id gentoo
uid=500(gentoo) gid=502(gentoo) groups=502(gentoo),500(distro),501(linux)
[root@Study ~]# tail -n 1 /etc/passwd
gentoo:x:500:502:Gentoo Distribution:/home/gentoo:/bin/csh
[root@Study ~]# tail -n 3 /etc/group
distro:x:500:gentoo
linux:x:501:gentoo
gentoo:x:502:
Ex03
将/etc/passwd
文件中的前5行内容转换为大写后保存至/tmp/passwd.out
文件中
[root@Study ~]# cat -n /etc/passwd | head -n 5 | tr 'a-z' 'A-Z' | tee /tmp/passwd.out
1 ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
2 BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
3 DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN
4 ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
5 LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN
[root@Study ~]# cat /tmp/passwd.out
1 ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
2 BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
3 DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN
4 ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
5 LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN
Ex04
将登录至将前系统上用户信息中的后3行的信息转换为大写后保存至/tmp/who.out
文件中
[root@Study ~]# who | tail -n 3 | tr 'a-z' 'A-Z' | tee /tmp/who.out
ROOT TTY1 2017-11-04 18:03
ROOT PTS/0 2017-11-04 15:23 (192.168.1.114)
ROOT PTS/1 2017-11-04 18:02 (192.168.1.114)
[root@Study ~]# cat /tmp/who.out
ROOT TTY1 2017-11-04 18:03
ROOT PTS/0 2017-11-04 15:23 (192.168.1.114)
ROOT PTS/1 2017-11-04 18:02 (192.168.1.114)