PowerShell的回声和CMD的回声之间的区别 [英] Difference between PowerShell's echo and CMD's echo

查看:92
本文介绍了PowerShell的回声和CMD的回声之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PowerShell中得到以下信息:

I get the following in PowerShell:

D:\> echo "Apple Pie" | git hash-object --stdin
157cb7be4778a9cfad23b6fb514e364522167053

D:\> "Apple Pie" | git hash-object --stdin
157cb7be4778a9cfad23b6fb514e364522167053

但在CMD.exe中:

but in CMD.exe:

C:\>echo "Apple Pie" | git hash-object --stdin
bb3918d5053fea31fc9a58fae1e5bdeabe3ec647

在PluralSight视频中,我看到了与Mac控制台不同的价值:

In a PluralSight video, I see a different value from what seems to be a Mac console:

在每种情况下,从 echo 传递的确切值是多少?

What is the exact value piped from echo in each case?

如果我去那些在线SHA1生成器之一并输入字符串 Apple Pie ,则会得到不同的哈希值.从那些我得到:

I get a different hash if I go to one of those online SHA1 generators and enter the string Apple Pie. From those I get:

8d69b7365f59237d3fb052c1f2f15ea33457fe51

推荐答案

据我了解:

使用CMD:

echo Apple Pie|git hash-object --stdin

在PowerShell中返回与以下内容相同的想法

return the same think as the following in PowerShell

"Apple Pie" | git hash-object --stdin

也就是说:

157cb7be4778a9cfad23b6fb514e364522167053

@Mofi似乎是正确的,您可以使用以下命令在Powershell中重现CMD结果:

@Mofi seems to be right, you can reproduce the CMD result in Powershell using :

'"Apple Pie" ' | git hash-object --stdin

解释Mac OS的一种方法:要获得157cb7be4778a9cfad23b6fb514e364522167053的真实字符列表,经过散列的字符是'Apple Pie \ r \ n'(带有回车换行符),在Mac或linux中命令行是'Apple Pie \ r'.

To explain the Mac OS one : To obtain 157cb7be4778a9cfad23b6fb514e364522167053 the real list of chars that is hashed is 'Apple Pie\r\n' (with carage return line feed), in Mac or linux like command line it's 'Apple Pie\r'.

如果要对此进行测试:将'Apple Pie'放入带有回车符的文本文件中,并将其另存为Windows文本样式(CR + LF),然后使用 git哈希对象yourfile.txt .然后将其保存为Linux样式(LF),然后再次进行测试,您会发现两个哈希值.

If you want to test this : put 'Apple Pie' in a text file with a cariage return and save it as a Windows text style (CR+LF), and use git hash-object yourfile.txt. Then save it in Linux style (LF) and test again, you will find your two hashes.

关于\ r \ n的部分.

The part about \r\n.

苹果派" |其中{$ _.length -eq 9} 显示字符串正好是9个字符长

"Apple Pie" | where {$_.length -eq 9} shows that the string is exactly 9 characters long

对我来说,这是因为在您的情况下,管道位于两个PowerShell部件之间,因此管道传输一个对象.当管道位于PowerShell和外部EXE之间时,将添加\ r \ n.这是一种使用C#编写的小型exe文件进行测试的方法:

For me it's because in your case the pipe is between two PowerShell parts, the pipe transmit an object. When the pipe is between PowerShell and an external EXE then the \r\n are added. Here is a way to test that with a small exe file written in C# :

using System;
namespace C_Param
{
  class Program
  {
    static void Main(string[] args)
    {
      string line = Console.In.ReadToEnd();
      foreach (char character in line){
        Console.WriteLine(String.Format("{0:X2}", Convert.ToByte(character)));
      }
    }
  }
}

PowerShell控制台中的结果是:

The result in a PowerShell console is :

"Apple Pie" | .\C_Param.exe
41
70
70
6C
65
20
50
69
65
0D
0A

在CMD控制台中的结果是:

The result in a CMD console is :

echo "Apple Pie" | .\C_Param.exe
22
41
70
70
6C
65
20
50
69
65
22
20
0D
0A

QED吗?

这篇关于PowerShell的回声和CMD的回声之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆