简体   繁体   English

找到没有哪个二进制文件的路径

[英]Find path to binary without which

An Open Source project I contribute to has a dependency on which that I'd like to remove, as some Linux distros (like the cloud version of Fedora 20) don't ship with it by default, and I believe it's ugly to force its install, given how trivial our use of it is.一个开源项目,我贡献,对依赖which ,我想删除,一些Linux发行版(如Fedora 20的云版本),默认情况下不与它船,我相信它的丑陋,迫使其安装,考虑到我们对它的使用是多么微不足道。

All we're doing is finding the path to the Java binary, then using that info to set $JAVA_HOME .我们所做的就是找到 Java 二进制文件的路径,然后使用该信息设置$JAVA_HOME

Is there a way to do that with Bash built-ins?有没有办法用 Bash 内置插件来做到这一点? In general, how can I somewhat-elegantly find a binary while minimizing dependencies (like which )?一般来说,我怎样才能优雅地找到一个二进制文件,同时最小化依赖关系(比如which )? Or is it a better call to just use which , for example if the only alternative is to run find against directories in $PATH and the community believes that to be exceedingly inelegant?或者,仅使用which是否更好,例如,如果唯一的选择是针对$PATH中的目录运行find并且社区认为这非常不雅?

Note that it's extremely ineffective to Google for which -related things.请注意,对于与which相关的事情,它对 Google 极其无效。

POSIX-ly 正确方法:

cmd=$(command -v whatever)

Maybe by parsing the output of the shell builtin type ?也许通过解析 shell 内置type的输出?

~/ type python
python is /usr/bin/python
~/ type type
type is a shell builtin

Tested with zsh , bash and shzshbashsh测试

If you are using bash , the option -P gives you directly the result you want:如果您使用的是bash ,则-P选项会直接为您提供所需的结果:

~/ type -P python
/usr/bin/python

My preference is already mentioned "type".我的偏好已经提到了“类型”。 But there is an additional command for this task:但是这个任务还有一个额外的命令:

 whereis -b <file>

locates binary for specified files定位指定文件的二进制文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM