exec: "cksum": executable file not found in $PATH in golang

huangapple go评论80阅读模式
英文:

exec: "cksum": executable file not found in $PATH in golang

问题

我正在尝试使用Go执行命令cksum来获取一个文件的校验和。

出现以下错误:

exec: "cksum": 在$PATH中找不到可执行文件

以下是代码:

cmd := exec.Command("/bin/cksum", dst)

谢谢。

英文:

I'm trying to get the cksum of one file using Go by executing the command cksum.

Getting the below error:

exec: "cksum": executable file not found in $PATH

Below is the code:

cmd := exec.Command("/bin/cksum",dst)

Thanks.

答案1

得分: 3

exec.Command的文档中可以得知:

如果name不包含路径分隔符,Command会使用LookPath来解析路径,以获取完整的name。否则,它直接使用name。

所以最好使用cmd := exec.Command("cksum", …),让它在路径中找到它所在的位置。
或者你可以运行which cksum,在几乎所有的Unix系统上都会得到/usr/bin/cksum

但更好的做法是,使你的代码在任何可以运行Go的操作系统上都可移植,并使用hash/crc32
或者,如果你可以移除对使用CRC32的任何要求(这是古老的cksum使用的方法),从hash/…crypto/…(例如:sha256),或golang.org/x/crypto/…(例如:sha3)中选择一个更优秀的哈希算法。

英文:

From the documentation for exec.Command:

> If name contains no path separators, Command uses LookPath to resolve the path to a complete name if possible. Otherwise it uses name directly.

So it's slightly better to use cmd := exec.Command("cksum", …) and let it be found where ever it exists on the path.
Alternatively you should have run which cksum which on nearly every unix system will give: /usr/bin/cksum.

But better yet, make your code portable to any OS that can run Go and use hash/crc32.
Or even better, if you can remove any requirements on having to use CRC32 (which is what the ancient cksum uses),
pick one of the other far superior hashes from
hash/…,
crypto/… (e.g. sha256),
or golang.org/x/crypto/… (e.g. sha3).

答案2

得分: 1

大部分可执行二进制文件都位于/usr/bin目录下,因此您需要将代码修改如下。

cmd := exec.Command("/usr/bin/cksum", dst)

英文:

> Most of the executable binaries are present under /usr/bin
> directory,so you need to modify your code as below.

cmd := exec.Command("/usr/bin/cksum",dst)

huangapple
  • 本文由 发表于 2015年4月8日 14:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/29507467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定