马哥Linux培训课后作业(2)

2017-11-14|Categories: Magedu-training|

Table of Contents

Q1

将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[liyang@centos7 testing]$ cat /etc/issue | tr 'a-z' 'A-Z' | tee /tmp/issue.out
[liyang@centos7 testing]$ cat /tmp/issue.out
\S
KERNEL \R ON AN \M

CURRENT TTY IS: \L
CURRENT HOSTNAME IS: \N
CURRENT TIME IS: \T

Q2

将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[liyang@centos7 testing]$ who | tr 'a-z' 'A-Z' > /tmp/who.out
[liyang@centos7 testing]$ cat /tmp/who.out
ROOT     PTS/0        2017-11-14 10:56 (172.18.1.72)
LIYANG   PTS/1        2017-11-14 16:09 (172.18.1.72)

Q3

一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名, the system version is here, please help me to check it, thanks!
操作系统版本信息

[liyang@centos7 testing]$ cat > mail.txt <<eof
> Hello, I am $USER,
> The system version is here, please help me to check it, thanks!
> `uname -r`
> eof

[liyang@centos7 testing]$ mail -s help root < mail.txt

Q4

/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@centos7 ~] ls -1 /root | tr '\n' ' '
anaconda-ks.cfg anton-repponen-101539.jpg Desktop Documents Downloads hardcopy.0 initial-setup-ks.cfg Music Pictures Public Templates Videos

Q5

计算1+2+3+..+99+100的总和

[liyang@centos7 testing]$ seq -s + 1 100 | bc
5050

Q6

删除Windows文本文件中的^M字符

# 生成文件
[liyang@centos7 testing]$ echo -e 'I am a Windows file.\nThere are some new line,\nwhich will display as a ^M via cat -A after unix2dos.' > unix.txt

# 转换文件格式
[liyang@centos7 testing]$ unix2dos -n unix.txt win.txt
unix2dos: converting file unix.txt to file win.txt in DOS format ...

# 显示文件,换行符已经是Windows格式
[liyang@centos7 testing]$ cat -A win.txt
I am a Windows file.^M$
There are some new line,^M$
which will display as a ^M via cat -A after unix2dos.^M$

# 用`tr`删除Windows换行符
[liyang@centos7 testing]$ cat win.txt | tr -d '\r' > win-without-new-line-symbol

# 再次显示文件,Windows换行符已删除
[liyang@centos7 testing]$ cat -A win-without-new-line-symbol
I am a Windows file.$
There are some new line,$
which will display as a ^M via cat -A after unix2dos.$

Q7

处理字符串xt.,l 1 jr#!$mn2 c*/fe3 uz4,只保留其中的数字和空格

[liyang@centos7 testing]$ echo 'xt.,l 1 jr#!$mn2 c*/fe3 uz4' | tr -cd '0-9 '
 1 2 3 4

Q8

将PATH变量每个目录显示在独立的一行

[liyang@centos7 testing]$ echo $PATH | tr : '\n'
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
/home/liyang/.local/bin
/home/liyang/bin

Q9

将指定文件中0-9分别替代成a-j

[liyang@centos7 testing]$ openssl rand -base64 40 | tr '0-9' 'a-j'
/RUkJkdmKPhpxcziIy+hflQdoh+XdwkAuoaaDskBODvgWDcQoQ+whg==

Q10

将文件/etc/centos-release中每个单词(由字母组成)显示在独立的一行,并无空行

[liyang@centos7 testing]$ cat /etc/centos-release | tr -dc '[:alpha:] ' | tr -s ' ' '\n'
CentOS
Linux
release
Core

Leave A Comment