一、重定向、管道的用法练习
1、将/etc/issue文件的内容转换为大写保存到/tmp/issue.out文件中
[[email protected] ~]# tr [a-z] [A-Z] < /etc/issue > /tmp/issue.out [[email protected] ~]# cat /tmp/issue.out \S KERNEL \R ON AN \M
或者
[[email protected] ~]# tr ‘[:lower:]‘ ‘[:upper:]‘ < /etc/issue > /tmp/issue.out.bak [[email protected] ~]# cat /tmp/issue.out.bak \S KERNEL \R ON AN \M
2、将当前系统登录用户信息转换为大写后保存至/tmp/who.out文件中
[[email protected] ~]# w |tr ‘[:lower:]‘ ‘[:upper:]‘ > /tmp/who.out [[email protected] ~]# cat /tmp/who.out 10:54:38 UP 1:23, 2 USERS, LOAD AVERAGE: 0.00, 0.01, 0.05 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT ROOT PTS/0 10.1.250.91 09:31 6.00S 0.49S 0.02S W ROOT PTS/1 10.1.250.91 09:36 21:10 0.29S 0.26S INFO TR
3、一个Linux用户给root发邮件,邮件标题为help,正文是:Hello,I am 用户名,the system version is here,please help me to check it ,thanks!
操作系统信息
[[email protected] ~]$ mail -s "help" root <<eof > Hello,I am `whoami`,the system version is here,please help me to check it,thanks! > `uname -or` > eof [[email protected] ~]$
[[email protected] ~]# mail Heirloom Mail version 12.5 7/5/10. Type ? for help >N 8 nieda Sun Jul 31 11:01 19/716 "help" & 8 Message 8: From [email protected] Sun Jul 31 11:01:36 2016 Return-Path: <[email protected]> X-Original-To: root Delivered-To: [email protected] Date: Sun, 31 Jul 2016 11:01:36 +0800 To: [email protected] Subject: help User-Agent: Heirloom mailx 12.5 7/5/10 Content-Type: text/plain; charset=us-ascii From: [email protected] (nieda) Status: R Hello,I am nieda,the system version is here,please help me to check it,thanks! 3.10.0-327.el7.x86_64 GNU/Linux
其中`whoami`可以用$USER替换,uname -ar可能信息不全,可以用uname -a或者cat /etc/centos-release
也可以将要发送的信息先写在一个文件里,然后在发送,但是要发送的文件里只能是纯文本,不能有变量和命令。
4、将/root/下文件列表,显示成一行,文件名之间用空格隔开
[[email protected] ~]# ls /root/ |tr ‘\n‘ ‘ ‘ anaconda-ks.cfg Desktop Documents Downloads f1 file1 initial-setup-ks.cfg Music Pictures Public Templates Videos [[email protected] ~]#
5、file1文件的内容为“1 2 3 4 5 6 7 8 9 10”计算出所有数字的总和
[[email protected] testdir]# cat file 1 2 3 4 5 6 7 8 9 10 [[email protected] testdir]# cat file |tr ‘ ‘ ‘+‘|bc 55
6、删除wndows文本文件中的‘^M’字符
操作系统中^是ctrl的显示方式,在Windows中ctrl+M是回车换行的意思
[[email protected] /testdir]# ll Windows.txt -rw-r--r--. 1 root root 122 Aug 1 16:21 Windows.txt [[email protected] /testdir]# file Windows.txt Windows.txt: ASCII text, with CRLF line terminators [[email protected] /testdir]# hexdump -C Windows.txt 00000000 0d 0a 0d 0a 48 65 6c 6c 6f 2c 6e 69 63 65 20 74 |....Hello,nice t| 00000010 6f 20 6d 65 65 74 20 79 6f 75 21 0d 0a 0d 0a 0d |o meet you!.....| 00000020 0a 4c 65 74 27 73 20 73 74 75 64 79 20 68 61 72 |.Let‘s study har| 00000030 64 20 61 73 20 62 65 73 74 20 61 73 20 77 65 20 |d as best as we | 00000040 63 61 6e 20 64 6f 20 69 6e 20 74 68 65 20 6e 65 |can do in the ne| 00000050 78 74 20 6d 6f 6e 74 68 73 2c 0d 0a 0d 0a 61 6e |xt months,....an| 00000060 64 20 69 20 62 65 6c 65 76 65 20 77 65 20 63 61 |d i beleve we ca| 00000070 6e 20 64 6f 20 77 65 6c 6c 2e |n do well.| 0000007a [[email protected] /testdir]# cat Windows.txt Hello,nice to meet you! Let‘s study hard as best as we can do in the next months, and i beleve we can do well.[[email protected] /testdir]#
[[email protected] /testdir]# hexdump -c Windows.txt 0000000 \r \n \r \n H e l l o , n i c e t 0000010 o m e e t y o u ! \r \n \r \n \r 0000020 \n L e t ‘ s s t u d y h a r 0000030 d a s b e s t a s w e 0000040 c a n d o i n t h e n e 0000050 x t m o n t h s , \r \n \r \n a n 0000060 d i b e l e v e w e c a 0000070 n d o w e l l . 000007a
其中Windows.txt中的换行都是^M,可以看到它的编码是\r\n和回车换行一样
[[email protected] /testdir]# tr -d ‘[:cntrl:]M‘ < Windows.txt Hello,nice to meet you!Let‘s study hard as best as we can do in the next months,and i beleve we can do well.[[email protected] /testdir]#
可以看出换行已经被删除了
7、处理字符串“xt.,| 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格
[[email protected] testdir]# cat a xt.,| 1 jr#!$mn 2 c*/fe 3 uz 4 [[email protected] testdir]# tr -c -d ‘[:digit:][:space:]‘ < a 1 2 3 4
8、将PATH变量每个目录显示在独立的一行
[[email protected] testdir]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [[email protected] testdir]# echo $PATH|tr ‘:‘ ‘\n‘ /usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /root/bin
9、删除指定文件的空行
[[email protected] testdir]# cat a hello,wang I am root I have some questions about the linux command need your help if you have time,call me please! eof [[email protected] testdir]# tr -s ‘\n‘ < a hello,wang I am root I have some questions about the linux command need your help if you have time,call me please! eof [[email protected] testdir]#
10、将文件中每个单词(字母)显示在独立的一行,并无空行
[[email protected] testdir]# tr ‘[:blank:][:punct:]‘ ‘\n‘ < word |tr -s ‘\n‘ hello this is the CCTV welcome to listen our news you can look the news happend everywhere at once
二、用户、组及其权限管理练习
1、创建用户gentoo,附加组为bin和root,默认shell是/bin/sch,注释信息为“Gentoo Distributuon”
[[email protected] ~]# useradd -G bin,root -s /bin/sch -c "Gentoo Distribution" geetoo [[email protected] ~]# id geetoo uid=1002(geetoo) gid=1002(geetoo) groups=1002(geetoo),0(root),1(bin)
2、创建下面的用户、组和组成员关系
名字为admins的组
用户natasha,使用admins作为附加组
用户harry,也使用admins作为附属组
用户sarah,不可交互登录系统,且不是admins的成员,natasha,harry,sarah密码都是centos
[[email protected] ~]# groupadd admins [[email protected] ~]# useradd -G admins natasha [[email protected] ~]# useradd -G admins harry;useradd -s /sbin/nologin sarah [[email protected] ~]# echo centos |passwd --stdin natasha harry sarah passwd: Only one user name may be specified. [[email protected] ~]# echo centos |passwd --stdin natasha Changing password for user natasha. passwd: all authentication tokens updated successfully. [[email protected] ~]# echo centos |passwd --stdin harry Changing password for user harry. passwd: all authentication tokens updated successfully. [[email protected] ~]# echo centos |passwd --stdin sarah Changing password for user sarah. passwd: all authentication tokens updated successfully. [[email protected] ~]# id natasha uid=1003(natasha) gid=1004(natasha) groups=1004(natasha),1003(admins) [[email protected] ~]# id harry uid=1004(harry) gid=1005(harry) groups=1005(harry),1003(admins) [[email protected] ~]# id sarah uid=1005(sarah) gid=1006(sarah) groups=1006(sarah)