我想清楚什么时候管道|或重定向>在命令中优先使用?
这是我的想法,但需要确认这是它的工作原理.
示例1:
sort < names | head The pipe runs first: names|head then it sorts what is returned from names|head
示例2:
ls | sort > out.txt This one seems straight forward by testing,ls|sort then redirects to out.txt
示例3:
Fill in the blank? Can you have both a < and a > with a | ???
在语法分组方面,>和&优先权较高;也就是说,这两个命令是等效的:
sort < names | head ( sort < names ) | head
这两个是一样的:
ls | sort > out.txt ls | ( sort > out.txt )
但是在顺序排序方面首先执行;所以,这个命令:
cat in.txt > out1.txt | cat > out2.txt
将填充out1.txt,而不是out2.txt,因为> out1.txt在|之后执行,因此取代它(因此没有输出被输出到cat> out2.txt).
同样,这个命令:
cat < in1.txt | cat < in2.txt
将打印in2.txt,而不是in1.txt,因为< in2.txt在|之后执行,因此取代它(因此没有输入从cat< in1.txt中输入).