2014-05-02 109 views
0

爲什麼sort -c輸出沒有被重定向到文件temp.txt? 如果刪除-c,它確實重定向,如下所示:基於sort命令的文件sort -c輸出被重定向到一個文件:Linux命令

$ cat numericSort 
33:Thirty Three:6 
11:Eleven:2 
45:Forty Five:9 
01:Zero One:1 
99:Ninety Nine:9 
18:Eighteen:01 
56:Fifty Six:4 
78:Seventy Eight:2 


$ sort numericSort > temp.txt 


$ cat temp.txt 
01:Zero One:1 
11:Eleven:2 
18:Eighteen:01 
33:Thirty Three:6 
45:Forty Five:9 
56:Fifty Six:4 
78:Seventy Eight:2 
99:Ninety Nine:9 


$ rm temp.txt 


$ sort -c numericSort > temp.txt 
sort: numericSort:2: disorder: 11:Eleven:2 


$ cat temp.txt 
# No Output Here 

回答

4

sort -c輸出去stderr,不stdout

如果你想重定向來代替:

$ sort -c numericSort 2> temp.txt 
+1

+1;應該注意的是,POSIX指定使用'-c'產生NO輸出(「不應該產生輸出;只有退出代碼會受到影響。」 - http://man.cx/sort),所以'stderr '輸出(至少)GNU'sort'和BSD'sort'在輸入未被排序的情況下產生的是非POSIX擴展。 – mklement0

3

-c,--check,--check =診斷-第一

  check for sorted input; do not sort 

檢查給定文件是否已經排序:如果他們不 所有排序,打印錯誤消息和退出與1

所以你的命令sort -c numericSort > temp.txt基本上只是檢查文件是否numericSort排序與否狀態。如果未在STDERR上排序打印錯誤,並且因此看不到任何輸出到temp.txt。也許你要重定向STDERR,而不是像

sort -c numericSort 2> temp.txt 
相關問題