1、基本概念:
1.1、 标准输入(0)、标准输出(1)、标准错误输出(2) ,文件描述符FD
文件描述符(File Descriptions 或 Process I/O Channel ):进程使用FD来管理打开的文件,可以在目录/proc/$$(进程id)/fd目录下查看进程打开的文件;
实例:
[root@centos6 ~]# ls /proc/$$/fd
0 1 2 255
[root@centos6 ~]# ls -l /proc/$$/fd
总用量 0
lrwx------. 1 root root 64 9月 23 14:37 0 -> /dev/pts/2
lrwx------. 1 root root 64 9月 23 15:31 1 -> /dev/pts/2
lrwx------. 1 root root 64 9月 23 14:38 2 -> /dev/pts/2
lrwx------. 1 root root 64 9月 23 15:31 255 -> /dev/pts/2
文件描述符与标准输入输出对应关系:
名称 | 文件描述符 | 默认设备 | 说明 |
---|---|---|---|
stdin(标准输入) | 0 | 键盘 | 只读 |
stdout(标准输出) | 1 | 终端 | 只写 |
stderr(标准错误) | 2 | 终端 | 只写 |
其他文件) | 3+ | 文件 | 读写 |
1.2、输出重定向,控制输出到文件
符号 | 功能 |
---|---|
1>,> | 正确覆盖重定向 |
1>>,>> | 正确追加重定向 |
2> | 错误覆盖重定向 |
2>> | 错误追加重定向 |
>outfilename 2>&1 或 &>outfilename | 正确与错误覆盖重定向 |
>>outfilename 2>&1 或 &>>outfilename | 正确与错误追加重定向 |
在ksh中 &> 不好用
1.3、输入重定向
< 或 0<
1.4、子shell(subshell):
命令用小括号()括起来,就会在子shell中执行,这样不会影响当前shell环境;实例如下:
##在当前shell中执行下面命令,当前shell会切换到新目录,然后显示新目录内容
[root@centos6 etc]# cd /var;ls
account cache crash cvs db empty games gdm lib local lock log mail nis opt preserve run spool tmp www yp
[root@centos6 var]# pwd
/var
[root@centos6 var]# cd
##在子shell中执行下面命令&#