文件描述符:
[root@centos7 ~]#cp /etc/hosts /data [root@centos7 ~]#ll /proc/$$/fd total 0 lrwx------ 1 root root 64 Feb 6 09:26 0 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 1 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 2 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 255 -> /dev/pts/0 自定义文件描述符: [root@centos7 ~]#exec 8<>/data/hosts [root@centos7 ~]#!?ll ll /proc/$$/fd total 0 lrwx------ 1 root root 64 Feb 6 09:26 0 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 1 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 2 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 255 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:27 8 -> /data/hosts 1668 取消文件描述符 [root@centos7 ~]#exec 8>&- [root@centos7 ~]#ll /proc/$$/fd total 0 lrwx------ 1 root root 64 Feb 6 09:26 0 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 1 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 2 -> /dev/pts/0 lrwx------ 1 root root 64 Feb 6 09:26 255 -> /dev/pts/0
linux 给程序提供三种io 设备:
标准输入:0
标准输出:1 默认输出终端窗口
标准错误:2 默认输出终端窗口
[root@centos7 ~]#tty /dev/pts/0 [root@centos7 ~]#ls > /dev/pts/1 [root@centos7 ~]#tty /dev/pts/1 [root@centos7 ~]#1 Centos-7.repo Documents f1.bak index.html index.html.2 Pictures Templates 2 Desktop Downloads f1.bak0 index.html.1 Music Public Videos
标准错误输出:
[root@centos7 data]#cmd 2> /data/f3.out [root@centos7 data]#cat /data/f3.out bash: cmd: command not found... Similar command is: 'mcd'
分类重定向:
ls /error /data/ > f1 2>f2
错误/标准 输出到 f1
ls /error /data >>all.log 2>&1 (注意顺序)
ls /error /data &>all.log
[root@centos7 data]#cat all.log ls: cannot access /error: No such file or directory /data: all.log err.log f1 f1.out f2.out f3.out hosts hostsbak httpd-2.4.6-80.el7.centos.x86_64.rpm repodata
[root@centos7 data]#ls /error /data &>all.log
[root@centos7 data]#cat all.log ls: cannot access /error: No such file or directory /data: all.log err.log f1 f1.out f2.out f3.out hosts hostsbak httpd-2.4.6-80.el7.centos.x86_64.rpm repodata
cmd > log 2&>1 cmd 2&>1 >log (正确的重定向到log 中,错误标准输出) cmd &>log cmd 2>log >&2
(ls;pwd) > all.log
hostname > /dev/null (吸收屏幕的过程)
echo $passwd |passwd --stdin sun &> /dev/null
标准输入:
cat
cat < /etc/hosts