Linux-管道与输入输出重定向

文章目录

  • [12. 管道与输入输出重定向](#12. 管道与输入输出重定向)
        • [12.1 输出重定向](#12.1 输出重定向)
        • [12.2 输入重定向](#12.2 输入重定向)
        • [12.3 管道](#12.3 管道)

12. 管道与输入输出重定向

bash 复制代码
# 输入设备 input device 鼠标/键盘
# 输出设备 output device 显示器

$# 计算机信息输入(只要输入,计算机都认为是正确的) stdin - standard input device
   计算机信息输出
	1. 正确输出  stdout - standard output device  标准正确输出设备
	2. 错误输出  stderr - standard error device   标准错误输出设备
# 	
[zy@localhost ~]$ ls   # 输入正确
1234.txt  Documents  Music     Public  zt.txt  zy.cpp   zy.mp3  # 输出正确
Desktop   Downloads  Pictures  Videos  zy.c    zy.html  zy.txt  # 输出正确
[zy@localhost ~]$ 
[zy@localhost ~]$ LS   # 计算机认为输入正确
bash: LS: command not found...  # 输出错误
Similar command is: 'ls'        # 输出错误
[zy@localhost ~]$ 

在linux中,一切皆文件。  (输入设备 | 输出设备)
[zy@localhost ~]$ ls /dev/

0 stdin - standard input device
1 stdout - standard output device
2 stderr - standard error device

如果信息输出到了stdout/stderr设备,信息会从屏幕中显示出来
[zy@localhost ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Videos
[zy@localhost ~]$ ls > /dev/stdout # 默认输出
Desktop  Documents  Downloads  Music  Pictures  Public  Videos
[zy@localhost ~]$ ls >/dev/stderr
Desktop  Documents  Downloads  Music  Pictures  Public  Videos
[zy@localhost ~]$ ls > /dev/null
[zy@localhost ~]$ 

# 在某些场景下不想让别人看到输出信息
[root@localhost zy]# echo "Epotywbo" | passwd --stdin zy    (默认输出到/dev/stdout)
Changing password for user zy.
passwd: all authentication tokens updated successfully.
[root@localhost zy]# 
[root@localhost zy]# echo "Epotywbo" | passwd --stdin zy > /dev/null
[root@localhost zy]# 

# & 将所有结果 
[zy@localhost ~]$ date &> output01
[zy@localhost ~]$ more output01
Tue Jan 31 23:37:14 CST 2023
[zy@localhost ~]$ Date &> output01
[zy@localhost ~]$ more output01
bash: Date: command not found...
Similar command is: 'date'
[zy@localhost ~]$ 

# null(黑洞设备)不输出正确或者错误信息
[zy@localhost ~]$ ls
Desktop  Documents  Downloads  Music  output01  Pictures  Public  Videos
[zy@localhost ~]$ LS > /dev/null
bash: LS: command not found...
Similar command is: 'ls'
[zy@localhost ~]$ LS 2> /dev/null
[zy@localhost ~]$ more /dev/null
[zy@localhost ~]$ 
12.1 输出重定向
bash 复制代码
$# 将标准正确输出重定向输出到文件
[zy@localhost ~]$ ls > output01
[zy@localhost ~]$ more output01
Desktop
Documents
Downloads
Music
output01
Pictures
Public
Videos
[zy@localhost ~]$ 

# > (正确输出)覆盖重定向
[zy@localhost ~]$ date
Tue Jan 31 23:27:09 CST 2023
[zy@localhost ~]$ date > /dev/stdout
Tue Jan 31 23:27:30 CST 2023
[zy@localhost ~]$ date > output01
[zy@localhost ~]$ more output01
Tue Jan 31 23:27:39 CST 2023
[zy@localhost ~]$ date > output01 # 覆盖了
[zy@localhost ~]$ more output01
Tue Jan 31 23:27:59 CST 2023
[zy@localhost ~]$ 

# >> (正确输出)追加重定向
[zy@localhost ~]$ more output01
Tue Jan 31 23:27:59 CST 2023
[zy@localhost ~]$ date >> output01
[zy@localhost ~]$ more output01
Tue Jan 31 23:27:59 CST 2023
Tue Jan 31 23:30:10 CST 2023
[zy@localhost ~]$ 

#*************************************************************************************
 >  (错误输出)覆盖重定向
 >> (错误输出)追加重定向
[zy@localhost ~]$ Date > output01
bash: Date: command not found...
Similar command is: 'date'
[zy@localhost ~]$ more output01
[zy@localhost ~]$ 

[zy@localhost ~]$ Date 2> output01
[zy@localhost ~]$ more output01
bash: Date: command not found...
Similar command is: 'date'
[zy@localhost ~]$ 

# 结果转换重定向(正确转换)
[zy@localhost ~]$ LS
bash: LS: command not found...
Similar command is: 'ls'
[zy@localhost ~]$ 
[zy@localhost ~]$ LS >> output02
bash: LS: command not found...
Similar command is: 'ls'
[zy@localhost ~]$ LS >> output02 2>&1
[zy@localhost ~]$ more output02
bash: LS: command not found...
Similar command is: 'ls'
[zy@localhost ~]$ 


# 结果转换重定向(错误转换)
[zy@localhost ~]$ ls 2> output03
[zy@localhost ~]$ more output03
[zy@localhost ~]$ ls 2> output03 1>&2
[zy@localhost ~]$ more output03
Desktop
Documents
Downloads
Music
output01
output02
output03
Pictures
Public
Videos
[zy@localhost ~]$ 



# 实战
[zy@localhost ~]$ find / -name "useradd" 2> /dev/null
/etc/default/useradd
/usr/sbin/useradd
/usr/share/bash-completion/completions/useradd
[zy@localhost ~]$ 
[zy@localhost ~]$ find / -name useradd 2> ~/find01_false  >~/find01_true
[zy@localhost ~]$ ls
Desktop    Downloads     find01_true  Public
Documents  find01_false  Music       Pictures  Videos
[zy@localhost ~]$ 
12.2 输入重定向
bash 复制代码
linux 内部邮件
[zy@localhost ~]$ mail -s "zy_varning_01" root

I am zy
.  
EOT
[zy@localhost ~]$ 

[root@localhost ~]#
You have mail in /var/spool/mail/root
[root@localhost ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 zy                    Wed Feb  1 00:18  19/611   "zy_varning_01"
& 1
Message  1:
From zy@localhost.localdomain  Wed Feb  1 00:18:00 2023
Return-Path: <zy@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Date: Wed, 01 Feb 2023 00:18:00 +0800
To: root@localhost.localdomain
Subject: zy_varning_01
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: zy@localhost.localdomain (zy)
Status: R

I am zy

& q
Held 1 message in /var/spool/mail/root
You have mail in /var/spool/mail/root
[root@localhost ~]# 
[root@localhost ~]# 


& 1 # 查看第一封邮件
& header # 查看邮件简要信息
& delete 1 2 3 4 5 6 # 删除邮件信息

# ctrl + delete
[root@localhost ~]# ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H
#*************************************************************************************


$# 将find01_true文件作为输入,给邮件,发给root账户
[zy@localhost ~]$ mail -s "zy_varning_02" root < ~/find01_true
[zy@localhost ~]$ 
[zy@localhost ~]$ mail -s "zy_varning_03" root <<END # 以END作为结束字符
> Hello
> I am zy
> ...   #  (.不能退出了)
> END
[zy@localhost ~]$ 
12.3 管道
bash 复制代码
$# 
主要作用:  衔接 / 两个命令之间的衔接
			将命令1的标准输出(stdout)作为命令2的标准输入(stdin)
[zy@localhost ~]$ grep "zy" /etc/passwd   # grep拿到的内容是一个文件作为输入的
zy:x:1000:1000:zy:/home/zy:/bin/bash
[zy@localhost ~]$ 

[zy@localhost ~]$ cat /etc/passwd | grep "zy"
zy:x:1000:1000:zy:/home/zy:/bin/bash
[zy@localhost ~]$ 

[zy@localhost ~]$ locate passwd | wc -l
126
[zy@localhost ~]$ 

# -E(正则表达式)
[zy@localhost ~]$ ls | grep -E "output.*"
output01
output02
output03
[zy@localhost ~]$ 

# 查看本地路径目录数量
[zy@localhost ~]$ ls -lh | grep -E "^d" | wc -l
7
[zy@localhost ~]$ 

# 查看本地路径文件/文件夹数量
[zy@localhost ~]$ ls -lh | wc -l
13
[zy@localhost ~]$ 

[zy@localhost ~]$ echo "I am zy,varning" | mail -s "zy_sent_root" root
[zy@localhost ~]$ 
[root@localhost ~]# 
You have new mail in /var/spool/mail/root
[root@localhost ~]# 
相关推荐
2301_819287121 小时前
ce第六次作业
linux·运维·服务器·网络
CIb0la2 小时前
GitLab 停止为中国区用户提供 GitLab.com 账号服务
运维·网络·程序人生
武汉联从信息2 小时前
如何使用linux日志管理工具来管理oracle osb服务器日志文件?
linux·运维·服务器
天天进步20152 小时前
STUN服务器实现NAT穿透
运维·服务器
月如琉璃2 小时前
1.gitlab 服务器搭建流程
服务器·gitlab
Kika写代码2 小时前
【微信小程序】页面跳转基础 | 我的咖啡店-综合实训
服务器·微信小程序·小程序
PieroPc2 小时前
Python 自动化 打开网站 填表登陆 例子
运维·python·自动化
Aileen_0v02 小时前
【AI驱动的数据结构:包装类的艺术与科学】
linux·数据结构·人工智能·笔记·网络协议·tcp/ip·whisper
州周3 小时前
Ftp目录整个下载
linux·服务器·数据库
Jackey_Song_Odd3 小时前
Ubuntu 24.04.1 解决部分中文字符(门、径)显示错误的问题
linux·ubuntu