数据流重导向 (Redirection)
standard output 与 standard error output
- 标准输入 (stdin) :代码为 0 ,使用 < 或 << ;
- 标准输出 (stdout):代码为 1 ,使用 > 或 >> ;
- 标准错误输出(stderr):代码为 2 ,使用 2> 或 2>> ;
标准的输出、标准的错误输出用法
- 1> :以覆盖的方法将『正确的数据』输出到指定的文件或装置上;
[root@www ~]# ll / > ~/rootfile <==屏幕并无任何信息
- 1>>:以累加的方法将『正确的数据』输出到指定的文件或装置上;
[root@www ~]# ll / >> ~/rootfile <==会在文件最末行进行叠加
- 2> :以覆盖的方法将『错误的数据』输出到指定的文件或装置上;
[dmtsai@www ~]$ find /home 2> ~/list_error <== 这里因为没有权限导致了错误的输出! find: /home/lost+found: Permission denied <== Standard error
- 2>>:以累加的方法将『错误的数据』输出到指定的文件或装置上;
[dmtsai@www ~]$ find /home 2>> ~/list_error <== 这里因为没有权限导致了错误的输出,会在文件最末行进行叠加! find: /home/lost+found: Permission denied <== Standard error
- 标准输出、标准错误输出组合命令
范例三:承范例二,将 stdout 与 stderr 分存到不同的文件去 [dmtsai@www ~]$ find /home -name .bashrc > list_right 2> list_error
/dev/null 垃圾桶黑洞装置与特殊写法
范例四:承范例三,将错误的数据丢弃,屏幕上显示正确的数据
[dmtsai@www ~]$ find /home -name .bashrc 2> /dev/null
/home/dmtsai/.bashrc <==只有 stdout 会显示到屏幕上, stderr 被丢弃了
标准的输入的用法
范例六:利用 cat 命令来创建一个文件的简单流程
[root@www ~]# cat > catfile
testing
cat file test
<==这里按下 [ctrl]+d 来离开
[root@www ~]# cat catfile
testing
cat file test
范例七:用 stdin 取代键盘的输入以创建新文件的简单流程
[root@www ~]# cat > catfile < ~/.bashrc
[root@www ~]# ll catfile ~/.bashrc
[root@www ~]# cat > catfile << "eof"
> This is a test.
> OK now stop
> eof <==输入这关键词,立刻就结束而不需要输入 [ctrl]+d
[root@www ~]# cat catfile
This is a test.
OK now stop <==只有这两行,不会存在关键词那一行!
命令运行的判断依据: ; , &&, ||
cmd ; cmd (不考虑命令相关性的连续命令下达)
$? (命令回传值) 与 && 或 ||
- cmd1 && cmd2
- 若 cmd1 运行完毕且正确运行($?=0),则开始运行 cmd2。
- 若 cmd1 运行完毕且为错误 ($?≠0),则 cmd2 不运行。
- cmd1 || cmd2
- 若 cmd1 运行完毕且正确运行($?=0),则 cmd2 不运行。
- 若 cmd1 运行完毕且为错误 ($?≠0),则开始运行 cmd2。
- cmd1 && cmd2