perl的shasum VS庆典shasum [英] perl shasum vs bash shasum
问题描述
以下bash和Perl脚本神秘给出不同的结果。为什么呢?
The following bash and Perl scripts mysteriously give different results. Why?
#!/bin/bash
hash=`echo -n 'abcd' | /usr/bin/shasum -a 256`;
echo $hash;
#!/usr/bin/perl
$hash = `echo -n 'abcd' | /usr/bin/shasum -a 256`;
print "$hash";
在bash脚本:
The bash script:
$ ./tst.sh
88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589 -
的Perl脚本:
The Perl script:
$ ./tst.pl
61799467ee1ab1f607764ab36c061f09cfac2f9c554e13f4c7442e66cbab9403 -
到底?
推荐答案
摘要:在你的Perl脚本, -n
被当作参数传递给输出包括回声
,而不是一个标志燮preSS的换行符。 (尝试
Summary: In your Perl script, -n
is being treated as an argument to include in the output of echo
, not a flag to suppress the newline. ( Try
$hash = `echo -n 'abcd'`;
确认)。使用的printf
代替。
Perl使用 / bin / sh的
执行中回抽动code。即使 / bin / sh的
是庆典
的链接,当通过像调用它会表现不同。在POSIX模式,
Perl uses /bin/sh
to execute code in back tics. Even if /bin/sh
is a link to bash
, it will behave differently when invoked via that like. In POSIX mode,
echo -n 'abcd'
将输出
-n abcd
也就是 -n
选项不能被识别为一个标志燮preSS一个换行符,而是被当作常规参数进行打印。替换回声-n
与的printf
中的每个脚本,您应该从每个脚本相同SHA散列。
that is, the -n
option is not recognized as a flag to suppress a newline, but is treated as a regular argument to print. Replace echo -n
with printf
in each script, and you should get the same SHA hash from each script.
(更新:庆典
3.2时为 SH
调用,显示此行为较新版本的庆典
似乎继续治疗 -n
当为 SH
。)
(UPDATE: bash
3.2, when invoked as sh
, displays this behavior. Newer versions of bash
seem to continue treating -n
as a flag when invoked as sh
.)
更妙的是,不要掏出做的事情,你可以在Perl做的。
Even better, don't shell out to do things you can do in Perl.
use Digest::SHA;
$hash = Digest::SHA::sha256('abcd');
有关好奇,这里是什么POSIX规范有什么看法<$ C $ C>回声。我不知道做XSI一致性的东西; 庆典
回声
要求 -e
选项,可将逃生字符特别,但几乎每个壳MDASH; 除了的旧版本庆典
,然后只有在特殊情况下&MDASH;治疗 -n
作为一个标志,而不是一个字符串。哦。
For the curious, here's what the POSIX spec has to say about echo
. I'm not sure what to make of XSI conformance; bash
echo
requires the -e
option to treat the escape characters specially, but nearly every shell—except old versions of bash
, and then only under special circumstances—treats -n
as a flag, not a string. Oh well.
The following operands shall be supported:
string
A string to be written to standard output. If the first operand is -n, or
if any of the operands contain a <backslash> character, the results are
implementation-defined.
On XSI-conformant systems, if the first operand is -n, it shall be treated
as a string, not an option. The following character sequences shall be
recognized on XSI-conformant systems within any of the arguments:
\a
Write an <alert>.
\b
Write a <backspace>.
\c
Suppress the <newline> that otherwise follows the final argument in the output. All characters following the '\c' in the arguments shall be ignored.
\f
Write a <form-feed>.
\n
Write a <newline>.
\r
Write a <carriage-return>.
\t
Write a <tab>.
\v
Write a <vertical-tab>.
\\
Write a <backslash> character.
\0num
Write an 8-bit value that is the zero, one, two, or three-digit octal number num.
这篇关于perl的shasum VS庆典shasum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!