cmd中 &含義.
linux shell 中"2>&1"含義
http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.html
為何2>&1要寫在后面?
command > file 2>&1
首先是command > file將標(biāo)準(zhǔn)輸出重定向到file中, 2>&1 是標(biāo)準(zhǔn)錯(cuò)誤拷貝了標(biāo)準(zhǔn)輸出的行為,也就是同樣被重定向到file中,最終結(jié)果就是標(biāo)準(zhǔn)輸出和錯(cuò)誤都被重定向到file中。
command 2>&1 >file
2>&1 標(biāo)準(zhǔn)錯(cuò)誤拷貝了標(biāo)準(zhǔn)輸出的行為,但此時(shí)標(biāo)準(zhǔn)輸出還是在終端。>file 后輸出才被重定向到file,但標(biāo)準(zhǔn)錯(cuò)誤仍然保持在終端。
Linux Shell 1>/dev/null 2>&1 含義 - CSDN blog
用strace可以看到:
1. command > file 2>&1
這個(gè)命令中實(shí)現(xiàn)重定向的關(guān)鍵系統(tǒng)調(diào)用序列是:
open(file) == 3
dup2(3,1)
dup2(1,2)
2. command 2>&1 >file
這個(gè)命令中實(shí)現(xiàn)重定向的關(guān)鍵系統(tǒng)調(diào)用序列是:
dup2(1,2)
open(file) == 3
dup2(3,1)
可以考慮一下不同的dup2()調(diào)用序列會(huì)產(chǎn)生怎樣的文件共享結(jié)構(gòu)。請(qǐng)參考APUE 3.10, 3.12
參考:
Mac-shell 變量,數(shù)據(jù)重定向和管道
http://cnbin.github.io/blog/2015/06/02/mac-shell-bian-liang-,shu-ju-zhong-ding-xiang-he-guan-dao/
posted on 2015-07-19 23:11 小高 閱讀(508) 評(píng)論(0) 編輯 收藏