在PowerShell中,|和>有什么区别?
dir | CLIP #move data to clipboard
dir > CLIP #not moving, creating file CLIP (no extension)假设|将当前结果移动到管道中的下一个块,并且>将数据保存到文件中,这样做正确吗?
还有其他的区别吗?
发布于 2017-04-26 08:28:11
(不完全)是的。
|和>是两种不同的东西。
>是一个所谓的重定向运算符。
重定向操作符将流的输出重定向到文件或其他流。管道运算符将cmdlet或funtion的返回对象连接到下一个(或管道的末尾)。当管道用它的特性来驱动整个对象时,重定向管道只是它的输出。我们可以用一个简单的例子来说明这一点:
#Get the first process in the process list and pipe it to `Set-Content`
PS> (Get-Process)[0] | Set-Content D:\test.test
PS> Get-Content D:/test.test输出
System.Diagnostics.Process (AdAppMgrSvc)
尝试将对象转换为字符串。
#Do the same, but now redirect the (formatted) output to the file
PS> (Get-Process)[0] > D:\test.test
PS> Get-Content D:/test.test输出
处理NPM(K) PM(K) WS(K) CPU Id SI ProcessName
第三个例子将展示管道操作符的功能:
PS> (Get-Process)[0] | select * | Set-Content D:\test.test
PS> Get-Content D:/test.test这将输出一个包含所有进程属性的Hashtable:
@{en1;Threads=System.Diagnostics.ProcessThreadCollection;;PriorityClass=;FileVersion=;HandleCount=420;WorkingSet=9519104;PagedMemorySize=6045696;PrivateMemorySize=6045696;VirtualMemorySize=110989312;TotalProcessorTime=;SI=0;Handles=420;VM=110989312;WS=9519104;PM=6045696;VM=110989312;VM=110989312;PriorityClass=;PriorityClass=;PriorityClass=24;;#en32;#en33;#en37;#en40;#en41;#en42;;#en44;#en1;#;#en55;en53#en53#en38#en41 en42 en44 en45 en46 en47 en35 en35 en37 en41 en42 en1 44 en45#en1 46#en1 47#en58#47#en58 48#enen5;VirtualMemorySize64=110989312;EnableRaisingEvents=False;StandardInput=;StandardOutput=;StandardError=;WorkingSet64=9519104;Site=;Container=}
发布于 2017-04-26 08:14:35
你说得对:
|将对象输送到成功/输出流中当您将对象从一个cmdlet输送到另一个cmdlet时,您不希望接收cmdlet接收错误、警告、调试消息或详细消息以及它设计用来处理的对象。 因此,管道运算符(X)实际上将对象输送到输出流(流#1)。
>将输出发送到指定的文件>>将输出附加到指定的文件有关重定向的更多信息:Redirection
https://stackoverflow.com/questions/43628341
复制相似问题