国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

/dev/null 1 & 2的用法

2019-11-06 06:24:19
字體:
來源:轉載
供稿:網友

應用背景devnull 1 2的介紹把錯誤輸出和標準輸出都導入日志testlog里屏蔽標準輸出和錯誤輸出信息tee命令如何把標準輸出和錯誤輸出都導入testlog里

應用背景

如下例所示,aa是不識別的命令。 在執行./test.sh時,輸出兩行,第一行表示錯誤輸出;第二行表示標準輸出。 如果把腳本test.sh執行的所有信息,寫入腳本日志test.log,那么就出現了人為偏差,腳本日志不包含錯誤輸出的內容;這樣不方便腳本執行的debug日志需求。

[qilei@localhost ~]$ cat ./test.sh #!/bin/bashaadate[qilei@localhost ~]$ ./test.sh ./test.sh: line 2: aa: command not foundMon Mar 6 05:54:00 PST 2017[qilei@localhost ~]$ ./test.sh > test.log./test.sh: line 2: aa: command not found[qilei@localhost ~]$ cat test.log Mon Mar 6 05:54:10 PST 2017

/dev/null 1 & 2的介紹

/dev/null :代表空設備文件

> :代表重定向到哪里,例如:echo "123" > /home/123.txt1 :表示stdout標準輸出,系統默認值是1,所以">/dev/null"等同于"1>/dev/null"2 :表示stderr標準錯誤& :表示等同于的意思,2>&1,表示2的輸出重定向等同于1

把錯誤輸出和標準輸出,都導入日志test.log里

把錯誤輸出和標準輸出,都導入日志test.log里;利用的是2>&12>&1,意思是錯誤輸出等同于標準輸出。所以使得./test.sh > test.log,可以把標準輸出和錯誤輸出全部導入日志test.log里。注意:2>&1,中間不能有空格。注意:2>&1,必須寫在最后,否則會失去原有意義。tee命令,不能替代>重定向命令[qilei@localhost ~]$ ./test.sh > test.log 2>&1[qilei@localhost ~]$ cat test.log ./test.sh: line 2: aa: command not foundMon Mar 6 06:00:29 PST 2017[qilei@localhost ~]$ ./test.sh | tee test.log 2>&1./test.sh: line 2: aa: command not foundMon Mar 6 06:00:41 PST 2017[qilei@localhost ~]$ cat test.log Mon Mar 6 06:00:41 PST 2017

屏蔽標準輸出和錯誤輸出信息

如下例所示, 1. /dev/null是空文件,就是為了屏蔽輸出而已。 2. > /dev/null,默認是把標準輸出屏蔽,但不能屏蔽錯誤輸出。等同于 1 > /dev/null 3. 2 > /dev/null,默認是把錯誤輸出屏蔽,但不能屏蔽標準輸出。 4. 2 > /dev/null,2和>之間可以有空格。與2&>1有些不同。具體原因先不管了。

[qilei@localhost ~]$ ./test.sh > /dev/null ./test.sh: line 2: aa: command not found[qilei@localhost ~]$ ./test.sh > /dev/null 2>&1[qilei@localhost ~]$ ./test.sh 2> /dev/nullMon Mar 6 06:04:56 PST 2017[qilei@localhost ~]$ ./test.sh 1> /dev/null./test.sh: line 2: aa: command not found[qilei@localhost ~]$ ./test.sh 1 > /dev/null./test.sh: line 2: aa: command not found[qilei@localhost ~]$ ./test.sh 2 > /dev/null./test.sh: line 2: aa: command not found[qilei@localhost ~]$

tee命令,如何把標準輸出和錯誤輸出都導入test.log里

tee命令,如何把標準輸出和錯誤輸出都導入test.log里?下例解決。 1. 注意,2>&1 要放在tee命令之前。

[qilei@localhost ~]$ ./test.sh | tee test.log 2>&1./test.sh: line 2: aa: command not foundMon Mar 6 06:17:27 PST 2017[qilei@localhost ~]$ cat test.logMon Mar 6 06:17:27 PST 2017[qilei@localhost ~]$ ./test.sh 2>&1 | tee test.log./test.sh: line 2: aa: command not foundMon Mar 6 06:17:48 PST 2017[qilei@localhost ~]$ cat test.log./test.sh: line 2: aa: command not foundMon Mar 6 06:17:48 PST 2017[qilei@localhost ~]$
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄冈市| 黄陵县| 京山县| 阿荣旗| 吉木萨尔县| 民权县| 青田县| 莱阳市| 赣榆县| 会泽县| 宁城县| 依兰县| 宜兰市| 襄汾县| 绥中县| 江达县| 隆回县| 霞浦县| 铜川市| 湾仔区| 镇江市| 南城县| 福贡县| 元阳县| 乐亭县| 西峡县| 曲沃县| 法库县| 两当县| 东山县| 洪泽县| 玉田县| 桐梓县| 淮阳县| 定兴县| 闸北区| 平舆县| 来安县| 邳州市| 邵武市| 万全县|