英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论