如何在 sh shell 中透明地用 sha1sum 替代 shasum?

如何在 sh shell 中透明地用 sha1sum 替代 shasum?

我有一个需要在多个平台上运行的测试。在所有其他平台上,测试都使用sha1sum,但在我的 Mac OSX El Capitan 上没有安装这样的程序,但可以shasum作为替代品正常工作。

我尝试创建一个从/usr/bin/sha1sum到 的链接/usr/bin/shasum,但是失败了,即使以 root 身份:

bash-3.2$ cd /usr/bin
bash-3.2$ sudo ln shasum sha1sum
ln: sha1sum: Operation not permitted

然后我把它放进去/usr/local/bin

bash-3.2$ cd /usr/local/bin
bash-3.2$ ln -s /usr/bin/shasum sha1sum

而且它似乎有效:

bash-3.2$ sha1sum -c files.sha1sum
smallData.txt: OK

但是,我的测试工具现在找到了 sha1sum,但仍然失败:

perl version 5.18.2 can't run /usr/local/bin/sha1sum.  Try the alternative(s):

(Error: no alternatives found)

Run "man perl" for more information about multiple version support in
Mac OS X.

我也尝试使用,alias sha1sum=shasum但线束失败:

sh: line 1: sha1sum: command not found

我发现我的线束出现故障,因为它使用的是sh而不是bash

sh-3.2$ sha1sum -c files.sha1sum
perl version 5.18.2 can't run /usr/local/bin/sha1sum.  Try the alternative(s):

(Error: no alternatives found)

Run "man perl" for more information about multiple version support in
Mac OS X.
sh-3.2$ shasum -c files.sha1sum
smallData.txt: OK

那么为什么它sha1sum无法在下面运行sh但在下面却可以shasum正常工作呢?我怎样才能在下面运行呢?sha1sumbashsha1sumsh

我也很困惑为什么会失败:

bash-3.2$ echo "sha1sum -c files.sha1sum" | bash
perl version 5.18.2 can't run /usr/local/bin/sha1sum.  Try the alternative(s):

(Error: no alternatives found)

Run "man perl" for more information about multiple version support in
Mac OS X.

我现在注意到/usr/bin/shasum文件中有此评论:

The contents of this script should normally never run!  The perl wrapper
should pick the correct script in /usr/bin by appending the appropriate version.
You can try appending the appropriate perl version number.  See perlmacosx.pod
for more information about multiple version support in Mac OS X.

答案1

shasum解决方案是链接到附加了版本的版本perl

cd /usr/local/bin
ln -s /usr/bin/shasum5.18 sha1sum

或者给它取别名:

alias sha1sum=shasum5.18

答案2

echo "" | $(which shasum 2>/dev/null || which sha1sum 2>/dev/null) 

输出:

adc83b19e793491b1c6ea0fd8b46cd9f32e592fc  -

在 MacOS/Darwin 或 Linux 上

相关内容