管理linux系统中输入输出
输入
单行输入和多行输入
[root@workstation Desktop]# touch file
[root@workstation Desktop]# vim file
[root@workstation Desktop]# cat file
qwertyuiop
[root@foundation Desktop]# tr 'a-z' 'A-Z' < file
QWERTYUIOP
test.sh文件内容
passwd >>EOF
123
123
EOF
[root@workstation Desktop]# rm -rf text.sh
[root@workstation Desktop]# vim text.sh
[root@workstation Desktop]# cat text.sh
passwd <<EOF
123
123
EOF
[root@foundationt Desktop]# sh text.sh
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.
用来修改密码
重定向输出
符号 | 用途 |
---|---|
> | 重定向正确输出 |
2> | 重定向错误输出 |
&> | 重定向全部输出 |
##定向正确输出到file
[student@workstation Desktop]$ find /etc/ -name passwd > file
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##查看正确输出
[student@workstation Desktop]$ cat file
/etc/passwd
/etc/pam.d/passwd
##定向错误输出到file.err
[student@workstation Desktop]$ find /etc/ -name passwd 2> file.err
/etc/passwd
/etc/pam.d/passwd
##查看错误输出
[student@workstation Desktop]$ cat file.err
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##定向所有输出到file.all
[student@workstation Desktop]$ find /etc/ -name passwd &> file.all
##查看所有输出
[student@workstation Desktop]$ cat file.all
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
/etc/passwd
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: Permission denied
/etc/pam.d/passwd
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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##清空file文件
[student@workstation Desktop]$ > file
##查看file文件为空
[student@workstation Desktop]$ cat file
重定向会覆盖原文件内容;该内容必须在普通用户中完成,因为超级用户不会有错误输出
输出的追加
符号 | 用途 |
---|---|
>> | 追加正确输出到文件最后 |
2>> | 追加错误输出到文件最后 |
&>> | 追加全部输出到文件最后 |
##追加正确输出到文件file最后
[student@workstation Desktop]$ find /etc/ -name passwd >> file
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##查看file文件 由于执行了> file 所以file文件为空
[student@workstation Desktop]$ cat file
##定向正确输出到file
[student@workstation Desktop]$ find /etc/ -name passwd > file
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##查看file文件
[student@workstation Desktop]$ cat file
/etc/passwd
/etc/pam.d/passwd
##追加错误输出到文件file.err最后
[student@workstation Desktop]$ find /etc/ -name passwd 2>> file.err
/etc/passwd
/etc/pam.d/passwd
##查看file.err文件
[student@workstation Desktop]$ cat file.err
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##后面为追加的错误文件
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##追加所有输出到file.all文件最后
[student@workstation Desktop]$ find /etc/ -name passwd &>> file.all
#3查看file.all文件
[student@workstation Desktop]$ cat file.all
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
/etc/passwd
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: Permission denied
/etc/pam.d/passwd
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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
##追加的所有文件
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
/etc/passwd
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: Permission denied
/etc/pam.d/passwd
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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
管道符 I
管道只允许正确输出通过;
通过管道的输出将会变成新的输入,这些新的输出会被第二条指令处理。
##结果为2,因为错误的输出不能通过管道符
[student@workstation Desktop]$ find /etc/ -name passwd | wc -l
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/modules/active’: 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/firewalld’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
2
##编号为1表示正确的输出,编号为2表示错误的输出
##2>&1将编号为2的转换成编号为1的 故结果为19
[student@workstation Desktop]$ find /etc/ -name passwd 2>&1| wc -l
19
##保存一份到file再输出 管道符可以多次被使用
[student@workstation Desktop]$ find /etc/ -name passwd 2>&1 | tee file | wc -l
19