练习 - 重定向标准输入输出和错误输出
概念类
1、Linux 系统中标准输入,标准输出和错误输出的文件描述符是什么?
标准输入 0<
标准输出 1>
错误输出 2>
2、Linux 系统中重定向符号 > 和 >> 的区别是什么?
> 表示写入文件,如果文件中存在内容,会覆盖
>> 表示追加内容
3、Linux 系统中管道的作用是什么?
将管道符前命令的标准输出做为管道符后的命令的标准输入
命令类
1、将/etc/issue 文件中的内容转换为大写后保存至/tmp/issue.out 文件中
[root@centos8 ~]#cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
[root@centos8 ~]#cat /tmp/issue.out
\S
KERNEL \R ON AN \M
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out 文件中
[root@centos8 ~]#who | tr 'a-z' 'A-Z' > /tmp/who.out
[root@centos8 ~]#cat /tmp/who.out
DAWN TTY1 2020-08-02 13:08
DAWN PTS/1 2020-08-02 12:44 (10.0.0.1)
ROOT PTS/2 2020-08-02 13:09 (10.0.0.1)
3、一个linux用户给root 发邮件,要求邮件标题为“help” ,邮件正文如下:
Hello,I am 用户名。
The system version is here,please help me to check it,thanks!
操作系统版本信息
[dawn@centos8 ~]$ mail -s "help" root <<EOF
> Hello,I am `whoami`.
> The system version is here,please help me to check it,thanks!
> `cat /etc/redhat-release`
> EOF
#切换账号为root
[root@centos8 ~]#mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 messages 1 new
>N 1 dawn@centos8.dawn Sun Aug 2 14:27 20/648 "help"
>& 1
Message 1:
From dawn@centos8.dawn Sun Aug 2 14:27:30 2020
Return-Path: <dawn@centos8.dawn>
X-Original-To: root
Delivered-To: root@centos8.dawn
Date: Sun, 02 Aug 2020 14:27:30 +0800
To: root@centos8.dawn
Subject: help
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: dawn@centos8.dawn
Status: R
Hello,I am dawn.
The system version is here,please help me to check it,thanks!
CentOS Linux release 8.1.1911 (Core)
& q
Held 1 messages in /var/spool/mail/root
4、将/root/ 下文件列表,显示成一行,且文件名之间用空格隔开
[root@centos8 ~]#ls -1 /root/ | tr '\n' ' '
anaconda-ks.cfg bc [root@centos8 ~]#
5、计算1+2+3+…+99+100 的总和
[root@centos8 ~]#echo {1..100} | tr ' ' '+' | bc
5050
[root@centos8 ~]#seq -s + 1 100 | bc
5050
6、删除windows 文本文件中的回车字符,即“\r”
[root@centos8 ~]#hexdump -c window.txt
0000000 a \r \n b \r \n c \r \n
0000009
[root@centos8 ~]#cat window.txt | tr -d "\r" | hexdump -c
0000000 a \n b \n c \n
0000006
7、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格
[root@centos8 ~]#echo "xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4" | tr -dc "[:digit:][:space:]"
1 2 3 4
8、将PATH变量每个目录显示在独立的一行
[root@centos8 ~]#echo $PATH | tr ":" "\n"
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
9、将指定文件中0-9分别替代成a-j
[root@centos8 ~]#cat test.txt
1 2 3 4 5 6 7 8 9 0
0123
4566
987120
123640985
[root@centos8 ~]#cat test.txt | tr '[0-9]' '[a-j]'
b c d e f g h i j a
abcd
efgg
jihbca
bcdgeajif
10、将文件/etc/centos-release 中每个单词(由字母组成)显示在独立一行,且无空行
[root@centos8 ~]#cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core)
[root@centos8 ~]#cat /etc/centos-release | tr -sc '[:alpha:]' '\n'
CentOS
Linux
release
Core