符号 | 功能用途 |
---|---|
>或1> | 重定向正确输出 |
2> | 重定向错误输出 |
&> | 重定向所有输出 |
实验:
1.从 /etc/ 下查找 passwd文件 (普通用户hauyu下操作) 未重定向,默认输出到/dev/pts/0
命令:find /etc/ -name passwd
[huayu@localhost 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
find: ‘/etc/cups/ssl’: Permission denied
/etc/pam.d/passwd #####正确输出
/etc/passwd
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
未重定向的情况下会默认输出到/dev/pts/0,所以会全部打印
2.从 /etc/ 下查找 passwd文件 (普通用户huayu下操作)重定向,正确输出到 right.out文件 错误的默认输出
命令:find /etc/ -name passwd >right.out
[huayu@localhost Desktop]$ find /etc/ -name passwd >right.out
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/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
重定向后会根据重定向的命令,将正确的结果输出到right.out文件中,错误的还是默认输出,所以下面只打印了错误结果
3.从 /etc/ 下查找 passwd文件 (普通用户huayu下操作)重定向,错误输出到 wrong.out 文件 正确的默认输出
命令:find /etc/ -name passwd 2>wrong.out
[huayu@localhost Desktop]$ find /etc/ -name passwd 2>wrong.out
/etc/pam.d/passwd
/etc/passwd
2>wrong.out 指定将错误输出到wrong.out文件,正确的依旧为默认,所以结果只打印了正确输出
4.从 /etc/ 下查找 passwd文件 (普通用户huayu下操作)全部输出到all.out 文件
命令:find /etc/ -name passwd &>all.out
[huayu@localhost Desktop]$ find /etc/ -name passwd &>all.out
[huayu@localhost Desktop]$
全部重定向到all.out文件,所以没有打印结果,可以用cat指令查看all.out文件验证
[huayu@localhost Desktop]$ cat all.out
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
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
最后需要说明下,> ,2>,&>重定向指令都会覆盖原文件内容
符号 | 功能用途 |
---|---|
>> | 追加正确输出 |
2>> | 追加错误输出 |
&>> | 追加所有输出 |
重定向会覆盖原文件内容,而追加不会,下面我们用实验来验证
实验:
1. 验证>会覆盖源文件内容
建立一个有内容的文件test,用cat来查看内容
[huayu@localhost Desktop]$ echo hello > test
[huayu@localhost Desktop]$ cat test
hello
查找结果正确的写入文件使用重定向的方式 >
命令:find /etc/ -name passwd >test
[huayu@localhost Desktop]$ find /etc/ -name passwd >test
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/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
使用cat查看文件内容
[huayu@localhost Desktop]$ cat test
/etc/pam.d/passwd
/etc/passwd
可以发现原来的内容“hello”被覆盖了
2.验证>>追加指令
同上一个实验,建立一个有内容的文件test,用cat来查看内容
[huayu@localhost Desktop]$ echo world > test2
[huayu@localhost Desktop]$ cat test2
world
查找结果正确的写入文件使用追加的方式 >> ,再使用cat命令查看
[huayu@localhost Desktop]$ find /etc/ -name passwd >>test2
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/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
[huayu@localhost Desktop]$ cat test2
world
/etc/pam.d/passwd
/etc/passwd
可以看到原来的文件内容world依旧存在
定义:将本次输出定义为另一个命令的输入
符号 | 功能用途 |
---|---|
| | 管道符 |
2>&1 | 将编号为2(stderr)的输出转换到1(stdout) |
tee | 复制输出到指定位置 |
实验:
1.通过管道符统计/bin下有多少文件 命令ls /bin/ | wc -l
[huayu@localhost Desktop]$ ls /bin/ | wc -l
1610
统计/etc下游多少名字为passwd的文件,默认只有正确的输出会输送到管道,find /etc/ -name passwd | wc -l
[huayu@localhost 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/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
2
可以看到打印了所有错误输出,正确的输出通过管道,成为了wc -l 命令的输入,所以最后一行为2
2.统计/etc下游多少名字为passwd的文件,将错误输出定向到正确输出存放位置 再通过管道统计,find /etc/ -name passwd 2>&1 | wc -l
[huayu@localhost Desktop]$ find /etc/ -name passwd 2>&1 | wc -l
16
也可以将输出保存到file文件中,再统计,代码如下:
[huayu@localhost Desktop]$ find /etc/ -name passwd 2>&1 | tee file | wc -l
16
[huayu@localhost Desktop]$ cat file
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
/etc/pam.d/passwd
/etc/passwd
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/sssd’: 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/firewalld’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
符号 | 功能用途 |
---|---|
< | 一次输入重定向 |
<< | 多行输入录入 |
实验:
1.一次输入重定向
新建一个file文件包含大小写字母,使用cat查看文件
[huayu@localhost Desktop]$ echo hihi hehe HAHA > file
[huayu@localhost Desktop]$ cat file
hihi hehe HAHA
将file作为输入传递给 tr命令,小写转换大写
[huayu@localhost Desktop]$ tr 'a-z' 'A-Z' < file
HIHI HEHE HAHA
通过<将file重定向为tr命令的输入
2.多行录入重定向
建立一个脚本 内容为 passwd,运行脚本(root用户下)
[root@localhost ~]# vim test.sh
[root@localhost ~]# sh test.sh
Changing password for user root.
New password:
可以看到阻塞在输入密码,输入密码后会再重复一次,需要两次输入,才能执行完命令,更改密码
可以通过多行录入来解决,在脚本中添加内容如下:
passwd << EOF ###EOF为多行录入起始
REDHAT
REDHAT
EOF ###再次表示录入结束
再次运行脚本
[root@localhost ~]# sh test.sh
test.sh: line 4: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
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.
可以看到,命令执行成功,root密码更改为REDHAT