sort-c输出被重定向到file:Linux命令

sort-c输出被重定向到file:Linux命令,linux,shell,Linux,Shell,为什么sort-c输出没有被重定向到文件temp.txt? 如果我删除-c,它会重定向,如下所示: $ 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:Z

为什么
sort-c
输出没有被重定向到文件
temp.txt
? 如果我删除
-c
,它会重定向,如下所示:

$ 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

基于
排序
命令的文档

-c、 --检查,--检查=先诊断

          check for sorted input; do not sort
检查给定文件是否已排序:如果未排序 所有排序后,打印一条错误消息,并以1状态退出

因此,您的命令
sort-c numericSort>temp.txt
实际上只是检查文件
numericSort
是否已排序。如果未排序,
STDERR
上的打印错误,因此您不会看到任何输出到
temp.txt
。可能您想要重定向STDERR,而不是像

sort -c numericSort 2> temp.txt

sort-c
的输出转到
stderr
,而不是
stdout

如果您想将其重定向,请执行以下操作:

$ sort -c numericSort 2> temp.txt

+1; 应该注意的是,POSIX指定不使用
-c
生成输出(“不应生成输出;只有退出代码会受到影响。”-),因此在输入未排序的情况下(至少)GNU
sort
和BSD
sort
生成的
stderr
输出是非POSIX扩展。