重定向、管道和用戶管理練習題

一、重定向、管道的用法練習

1、將/etc/issue文件的內容轉換爲大寫保存到/tmp/issue.out文件中

[root@localhost ~]# tr [a-z] [A-Z] < /etc/issue > /tmp/issue.out 
[root@localhost ~]# cat /tmp/issue.out 
\S
KERNEL \R ON AN \M

或者

[root@localhost ~]# tr '[:lower:]' '[:upper:]' < /etc/issue > /tmp/issue.out.bak 
[root@localhost ~]# cat /tmp/issue.out.bak 
\S
KERNEL \R ON AN \M

2、將當前系統登錄用戶信息轉換爲大寫後保存至/tmp/who.out文件中

[root@localhost ~]# w |tr '[:lower:]' '[:upper:]' > /tmp/who.out 
[root@localhost ~]# cat /tmp/who.out 
 10:54:38 UP  1:23,  2 USERS,  LOAD AVERAGE: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   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!

操作系統信息

[nieda@localhost ~]$ mail -s "help" root <<eof
> Hello,I am `whoami`,the system version is here,please help me to check it,thanks!
> `uname -or`
> eof
[nieda@localhost ~]$
[root@localhost ~]# 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/下文件列表,顯示成一行,文件名之間用空格隔開

[root@localhost ~]# ls /root/ |tr '\n' ' '
anaconda-ks.cfg Desktop Documents Downloads f1 file1 initial-setup-ks.cfg Music Pictures Public Templates Videos [root@localhost ~]#

5、file1文件的內容爲“1 2 3 4 5 6 7 8 9 10”計算出所有數字的總和

[root@localhost testdir]# cat file 
1 2 3 4 5 6 7 8 9 10
[root@localhost testdir]# cat file |tr ' ' '+'|bc
55

6、刪除wndows文本文件中的‘^M’字符

操作系統中^是ctrl的顯示方式,在Windows中ctrl+M是回車換行的意思

[root@localhost /testdir]# ll Windows.txt 
-rw-r--r--. 1 root root 122 Aug  1 16:21 Windows.txt
[root@localhost /testdir]# file Windows.txt 
Windows.txt: ASCII text, with CRLF line terminators
[root@localhost /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
[root@localhost /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.[root@localhost /testdir]#
[root@localhost /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和回車換行一樣

[root@localhost /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.[root@localhost /testdir]#

可以看出換行已經被刪除了

7、處理字符串“xt.,| 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的數字和空格

[root@localhost testdir]# cat a
xt.,| 1 jr#!$mn 2 c*/fe 3 uz 4
[root@localhost testdir]# tr -c -d '[:digit:][:space:]' < a
 1  2  3  4

8、將PATH變量每個目錄顯示在獨立的一行

[root@localhost testdir]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost testdir]# echo $PATH|tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

9、刪除指定文件的空行

[root@localhost 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
[root@localhost 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
[root@localhost testdir]#

10、將文件中每個單詞(字母)顯示在獨立的一行,並無空行

[root@localhost 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”

[root@localhost ~]# useradd -G bin,root -s /bin/sch -c "Gentoo Distribution" geetoo
[root@localhost ~]# 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

[root@localhost ~]# groupadd admins
[root@localhost ~]# useradd -G admins natasha
[root@localhost ~]# useradd -G admins harry;useradd -s /sbin/nologin sarah
[root@localhost ~]# echo centos |passwd --stdin natasha harry sarah
passwd: Only one user name may be specified.
[root@localhost ~]# echo centos |passwd --stdin natasha 
Changing password for user natasha.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# echo centos |passwd --stdin harry
Changing password for user harry.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# echo centos |passwd --stdin sarah
Changing password for user sarah.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# id natasha
uid=1003(natasha) gid=1004(natasha) groups=1004(natasha),1003(admins)
[root@localhost ~]# id harry
uid=1004(harry) gid=1005(harry) groups=1005(harry),1003(admins)
[root@localhost ~]# id sarah
uid=1005(sarah) gid=1006(sarah) groups=1006(sarah)








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章