1.3 连续与重定向指令

来源:互联网 发布:淘宝退款率高被清退 编辑:程序博客网 时间:2024/06/05 02:52

一、连续指令

1. 两者都执行

command1command2

利用分号 [;] 来分隔,这个分号的意思,代表不论 command1 执行结果为何,command2 都会被执行!

2. 前者正确,后者才执行

command1 && command2

那么 && 就是代表,当 command1 执行结果传回值为 0 的时候,也就是没有错误讯息时,则 command2 才会开始执行

3. 前者错误,后者才执行

command1 ||command2

而 || 恰恰相反,当 command1 有错误讯息时, command2 才会执行!

二、重定向

标准输入:代码为 0 ;或称为 stdin ; 使用的方式为 <

标准输出:代码为 1 ;或称为 stdout;使用的方式为 1>

错误输出:代码为 2 ;或称为 stderr;使用的方式为 2>

注意了!那个 1> 与 2> 之间并没有空格符

1. 标准输出

>  :将原本由屏幕输出的正确数据输出到 > 右边的 file ( 文件名称 ) 或 device ( 装置,如 printer )去;

>> :将原本由屏幕输出的正确数据输出到 >> 右边,与 > 不同的是,该文件将不会被覆盖,而新的数据,添加到该文件的最后面;

[test @test test]# ls -al >  list.txt [test @test test]# ls -al >> list.txt 
2. 错误输出

2> :将原本应该由屏幕输出的错误数据输出到 2> 的右边去。

[test @test test]# ls -al 1> list.txt 2> list.err  将显示的数据,正确的输出到 list.txt 错误的数据输出到 list.err [test @test test]# ls -al 1> list.txt 2>&1  将显示的数据,不论正确或错误均输出到 list.txt 当中! 
3. 丢弃输出

/dev/null:丢弃

[test @test test]# ls -al 1> list.txt 2> /dev/null 将显示的数据,正确的输出到 list.txt 错误的数据则予以丢弃!