AppleScript 和 MD5
我只是尝试使用 AppleScript 计算字符串的 MD5:
set hash_string to do shell script "echo -n hi | md5"
在上面的代码中,我实际上用“hi”替换了传递给 shell 的字符串作为测试。 AppleScript 报告:
tell current application
do shell script "echo -n hi | md5"
--> "5ea3a74c5a5c60abd09da8c5f85fa0a5"
end tell
直接在终端中运行相同的命令会导致:
% echo -n hi | md5
49f68a5c8493ec2c0bf489821c21fc3b
虽然命令相同,但计算出的哈希值不同。显然,AppleScript 中正在传递其他字符,但我似乎无法弄清楚它们是什么。我很感激任何指导。
I'm simply trying to compute the MD5 of a string using AppleScript:
set hash_string to do shell script "echo -n hi | md5"
In the code above, I've actually replaced the string passed to shell with "hi" as a test. AppleScript reports:
tell current application
do shell script "echo -n hi | md5"
--> "5ea3a74c5a5c60abd09da8c5f85fa0a5"
end tell
Running the same command directly in a terminal results in:
% echo -n hi | md5
49f68a5c8493ec2c0bf489821c21fc3b
Though the command is identical, the computed hash is different. Obviously, additional characters are being passed in AppleScript, but I can't seem to figure out what they are. I appreciate any guidance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果删除
| 您可以看到发生了什么md5
。在 AppleScript 情况下,您将看到-n hi
作为输出。这是因为由do shell script
调用的 POSIXsh
echo
不支持-n
,而bash
或csh
echo
内置命令或/bin/echo
命令都支持-n.
请参阅 Mac OS X
echo
手册页 或echo
的 POSIX 文档了解更多信息。请注意,POSIX 合规性要求,echo
的一致实现必须不支持任何选项,包括-n
;为了跨 shell 和系统兼容,POSIX 编辑器建议使用 printf 来执行诸如抑制换行之类的操作。You can see what is happening if you remove the
| md5
. In the AppleScript case, you'll see-n hi
as output. That's because the POSIXsh
echo
, which is invoked bydo shell script
, does not support-n
whereas thebash
orcsh
echo
builtins or the/bin/echo
command all do support-n
.See the Mac OS X
echo
man page or the POSIX documentation forecho
for more info. Note, POSIX compliance requires that conforming implementations ofecho
must not support any options including-n
; for compatibility across shells and systems, the POSIX editors suggest usingprintf
to do things like suppressing newlines.如果你从 echo 命令中删除 -n ,那么它应该可以正常工作。不完全确定为什么......但这似乎解决了它!
If you drop the -n from the echo command, then it should work fine. Not totally sure why... but that seems to fix it!
这有效
相同
,结果与命令提示符
this works
this results and the same as
from command prompt