IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> linux文件的输入与输出 -> 正文阅读

[系统运维]linux文件的输入与输出


1、什么是输入输出

在linux系统中,0表示为标准输入,1表示为标准输出,2表示为错误输出。

上图中,键盘为标准输入也就是编号0,而标准输出1和标准错误2都可以显示在显示屏上。
运行一个程序时 , 需要从某个位置读取输入信息 , 然后 cpu 处理, 最后将输出显示到屏幕或文件进程通过文件描述符的编号通道来获取输入并输出。

2、输出重定向

1>
重定向正确输出
利用root用户在/etc目录下查询passwd文件:将查询的正确结果保存在file文件中,使用cat查询。

[root@foundation50 Desktop]# find /etc/ -name passwd > file
[root@foundation50 Desktop]# cat file 
/etc/pam.d/passwd
/etc/passwd

2>
重定向错误输出
利用root用户在/etc目录下查询passwd文件:将查询的错误结果保存在file1文件中,使用cat查询,没有!。
利用普通用户在/etc目录下查询passwd文件:将查询的错误结果保存在file2文件中,使用cat查询。

[root@foundation50 Desktop]# su -- westos
[westos@foundation50 Desktop]$ find /etc -name passwd  
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
[westos@foundation50 Desktop]$ find /etc -name passwd 2> file2  把错误输出存到副ile2
/etc/pam.d/passwd
/etc/passwd
[westos@foundation50 Desktop]$ cat file2
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

&>
重定向所有输出
在/etc目录下查询passwd文件:将查询的所有结果保存在file3文件中,使用cat查询

[westos@foundation50 Desktop]$ find /etc -name passwd &> file3
[westos@foundation50 Desktop]$ cat file3
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

利用重定向清空文件

[westos@foundation50 Desktop]$ > file3
[westos@foundation50 Desktop]$ cat file3
[westos@foundation50 Desktop]$ 

输出正确的追加,原内容不会被清空

[westos@foundation50 Desktop]$ cat file3  原文件
/etc/pam.d/passwd
/etc/passwd
[westos@foundation50 Desktop]$ find /etc -name passwd >> file3
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
[westos@foundation50 Desktop]$ cat file3
/etc/pam.d/passwd
/etc/passwd
/etc/pam.d/passwd
/etc/passwd

2>> 追加错误输出
[westos@foundation50 Desktop]$ cat file3 原文件
/etc/pam.d/passwd
/etc/passwd
[westos@foundation50 Desktop]$ find /etc -name passwd 2>> file3
/etc/pam.d/passwd
/etc/passwd
[westos@foundation50 Desktop]$ cat file3
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

&>>

追加所有输出到文件最后
[westos@foundation50 Desktop]$ cat file3
[westos@foundation50 Desktop]$ find /etc -name passwd &>> file3
[westos@foundation50 Desktop]$ cat file3
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

输出重定向会覆盖文件内容,追加重定向不会覆盖文件原来的内容

| 管道
"|"管道的作用是把前一条命令的正确输出变成后一条命令的输入
通过管道的输出将会变成输入,会被第二条命令处理

[westos@foundation50 Desktop]$ find /etc -name passwd | wc -l  正确的输出会被管道符通过,错误的不会通过
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
2

如果让错误的输出也要被管道符通过,则需要把错误的输出追加到正确的输出里 2>&1

[westos@foundation50 Desktop]$ find /etc -name passwd 2>&1 | wc -l
17

若需要将输出内容保存一份,可以通过tee 复制两份,一份保存到文件里,一份通过管道让wc -l 来处理

[westos@foundation50 Desktop]$ find /etc -name passwd 2>&1 |tee file1 | wc -l
17
[westos@foundation50 Desktop]$ cat file1
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

3 、输入重定向

<

[root@foundation50 Desktop]# cat test 
hello
HELLO
[root@foundation50 Desktop]# tr 'a-z' 'A-Z' < test   将shell的0(输入)定向到tr 程序的0里面,因为shell不具备转换大小写的功能
HELLO
HELLO

<<
多行输入重定向

vim westos.sh
date
passwd root << EOF        << 表示多行输入  EOF 表示起始字符串
westos
westos
EOF   遇到EOF j结束
[root@foundation50 Desktop]# sh westos.sh    运行
Wed Aug  4 21:50:50 CST 2021
Changing password for user root.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.

练习:
在这里插入图片描述

4、[westos@foundation50 Desktop]$ find /etc -name passwd 2>&1 | tee /dev/pts/0 | wc -l  #/dev/pts/o 放入字符设备,能显示出来的就叫字符设备
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/dhcp’: Permission denied
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
17
5、[westos@foundation50 Desktop]$ tr 'a-z' 'A-Z' < /etc/passwd | cat -n
     1	ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
     2	BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
     3	DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN
     4	ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
     5	LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN
     6	SYNC:X:5:0:SYNC:/SBIN:/BIN/SYNC
     7	SHUTDOWN:X:6:0:SHUTDOWN:/SBIN:/SBIN/SHUTDOWN
     8	HALT:X:7:0:HALT:/SBIN:/SBIN/HALT
     9	MAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGIN
    10	OPERATOR:X:11:0:OPERATOR:/ROOT:/SBIN/NOLOGIN
    11	GAMES:X:12:100:GAMES:/USR/GAMES:/SBIN/NOLOGIN
    12	FTP:X:14:50:FTP USER:/VAR/FTP:/SBIN/NOLOGIN
    13	NOBODY:X:65534:65534:KERNEL OVERFLOW USER:/:/SBIN/NOLOGIN
    14	DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN
    15	SYSTEMD-COREDUMP:X:999:997:SYSTEMD CORE DUMPER:/:/SBIN/NOLOGIN
    16	SYSTEMD-RESOLVE:X:193:193:SYSTEMD RESOLVER:/:/SBIN/NOLOGIN
    17	TSS:X:59:59:ACCOUNT USED BY THE TROUSERS PACKAGE TO SANDBOX THE TCSD DAEMON:/DEV/NULL:/SBIN/NOLOGIN
    18	POLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGIN
    19	GEOCLUE:X:997:995:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN
    20	RTKIT:X:172:172:REALTIMEKIT:/PROC:/SBIN/NOLOGIN
    21	PIPEWIRE:X:996:994:PIPEWIRE SYSTEM DAEMON:/VAR/RUN/PIPEWIRE:/SBIN/NOLOGIN
    22	PULSE:X:171:171:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN
    23	LIBSTORAGEMGMT:X:995:991:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN
    24	QEMU:X:107:107:QEMU USER:/:/SBIN/NOLOGIN
    25	USBMUXD:X:113:113:USBMUXD USER:/:/SBIN/NOLOGIN
    26	UNBOUND:X:994:989:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN
    27	RPC:X:32:32:RPCBIND DAEMON:/VAR/LIB/RPCBIND:/SBIN/NOLOGIN
    28	GLUSTER:X:993:988:GLUSTERFS DAEMONS:/RUN/GLUSTER:/SBIN/NOLOGIN
    29	CHRONY:X:992:987::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
    30	SETROUBLESHOOT:X:991:985::/VAR/LIB/SETROUBLESHOOT:/SBIN/NOLOGIN
    31	SASLAUTH:X:990:76:SASLAUTHD USER:/RUN/SASLAUTHD:/SBIN/NOLOGIN
    32	DNSMASQ:X:984:984:DNSMASQ DHCP AND DNS SERVER:/VAR/LIB/DNSMASQ:/SBIN/NOLOGIN
    33	RADVD:X:75:75:RADVD USER:/:/SBIN/NOLOGIN
    34	CLEVIS:X:983:982:CLEVIS DECRYPTION FRAMEWORK UNPRIVILEGED USER:/VAR/CACHE/CLEVIS:/SBIN/NOLOGIN
    35	COCKPIT-WS:X:982:980:USER FOR COCKPIT WEB SERVICE:/NONEXISTING:/SBIN/NOLOGIN
    36	COCKPIT-WSINSTANCE:X:981:979:USER FOR COCKPIT-WS INSTANCES:/NONEXISTING:/SBIN/NOLOGIN
    37	SSSD:X:980:978:USER FOR SSSD:/:/SBIN/NOLOGIN
    38	FLATPAK:X:979:977:USER FOR FLATPAK SYSTEM HELPER:/:/SBIN/NOLOGIN
    39	COLORD:X:978:976:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN
    40	GDM:X:42:42::/VAR/LIB/GDM:/SBIN/NOLOGIN
    41	RPCUSER:X:29:29:RPC SERVICE USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
    42	GNOME-INITIAL-SETUP:X:977:975::/RUN/GNOME-INITIAL-SETUP/:/SBIN/NOLOGIN
    43	SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN
    44	AVAHI:X:70:70:AVAHI MDNS/DNS-SD STACK:/VAR/RUN/AVAHI-DAEMON:/SBIN/NOLOGIN
    45	RNGD:X:976:974:RANDOM NUMBER GENERATOR DAEMON:/VAR/LIB/RNGD:/SBIN/NOLOGIN
    46	TCPDUMP:X:72:72::/:/SBIN/NOLOGIN
    47	WESTOS:X:1000:1000::/HOME/WESTOS:/BIN/BASH
    48	APACHE:X:48:48:APACHE:/USR/SHARE/HTTPD:/SBIN/NOLOGIN
7、cat > westos.file << EOF
hello linux
hello westos
hello linux
westos linux is very nice 
EOF
1、[westos@foundation50 Desktop]$ find /etc -name passwd 2> /dev/null 
/etc/pam.d/passwd
/etc/passwd

内容补充
若让文件内容倒则显示 使用tac 命令

[root@foundation50 Desktop]# cat test
1
2
3
4
[root@foundation50 Desktop]# tac test 
4
3
2
1
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:44:38  更:2021-08-05 17:45:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 3:51:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计