gpt4 book ai didi

powershell - PowerShell 的 echo 和 CMD 的 echo 的区别

转载 作者:行者123 更新时间:2023-12-02 23:33:20 28 4
gpt4 key购买 nike

我在 PowerShell 中得到以下信息:

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

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

但在 CMD.exe 中:
C:\>echo "Apple Pie" | git hash-object --stdin
bb3918d5053fea31fc9a58fae1e5bdeabe3ec647

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

enter image description here

来自 echo 的确切值是多少?在每种情况下?

如果我转到其中一个在线 SHA1 生成器并输入字符串 Apple Pie,我会得到不同的哈希值。 .从那些我得到:
8d69b7365f59237d3fb052c1f2f15ea33457fe51

最佳答案

据我所理解 :

使用命令:

echo Apple Pie|git hash-object --stdin

在 PowerShell 中返回与以下相同的想法
"Apple Pie" | git hash-object --stdin

也就是说 :
157cb7be4778a9cfad23b6fb514e364522167053

@Mofi 似乎是对的,您可以使用以下命令在 Powershell 中重现 CMD 结果:
'"Apple Pie" ' | git hash-object --stdin

解释 Mac OS 一:要获得 157cb7be4778a9cfad23b6fb514e364522167053,经过哈希处理的真实字符列表是 'Apple Pie\r\n' (带有 carage return 换行符),在 Mac 或 linux 之类的命令行中是 'Apple Pie\r' .

如果你想测试这个:放 'Apple Pie'在带有回车符的文本文件中并将其保存为 Windows 文本样式 (CR+LF),然后使用 git hash-object yourfile.txt .然后将其保存为 Linux 样式(LF)并再次测试,您将找到您的两个哈希值。

关于\r\n的部分。
"Apple Pie" | where {$_.length -eq 9}显示字符串正好是 9 个字符长

对我来说,这是因为在您的情况下,管道位于两个 PowerShell 部件之间,管道传输一个对象。当管道位于 PowerShell 和外部 EXE 之间时,会添加\r\n。这是一种使用 C# 编写的小 exe 文件进行测试的方法:
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 控制台中的结果是:
"Apple Pie" | .\C_Param.exe
41
70
70
6C
65
20
50
69
65
0D
0A

CMD 控制台中的结果是:
echo "Apple Pie" | .\C_Param.exe
22
41
70
70
6C
65
20
50
69
65
22
20
0D
0A

QED ?

关于powershell - PowerShell 的 echo 和 CMD 的 echo 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61153475/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com