在 $PATH 上查找命令

发布于 2024-12-02 08:13:11 字数 569 浏览 1 评论 0原文

我正在编写一个脚本,我需要在用户的 $PATH 上查找命令并获取该命令的完整路径。问题是我不知道用户的登录 shell 是什么,或者他们的 do 文件中可能有什么奇怪的东西。我使用 bourne shell 来编写简单的小脚本,因为它需要在一些可能没有 bash 的旧版 Solaris 平台上运行。

“which”和“whence”的某些实现将获取用户的点文件,但这并不是真正适合所有用户。我想要一个简单的 UNIX 实用程序,它只执行扫描可执行文件的 PATH 并报告第一个匹配的完整路径的基本工作。

但我会选择任何对所有用户都稳定的 /bin/sh 解决方案。

我正在寻找一种比编写自己的 /bin/sh 循环更好的解决方案,该循环会切碎 $PATH 并一次搜索一行。这似乎很常见,应该有一种可重用的方法来做到这一点。

我对“漫长的道路”的第一个近似是:

   IFS=:
   for i in $PATH; do
      if [ -x $i/$cmd ]; then
          echo $i/$cmd
      fi
   done

有没有更简单和便携的东西?

I'm writing a script, and I need to look up a command on the user's $PATH and get the full path to the command. The problem is that I don't know what the user's login shell is, or what strange stuff might be in their do files. I'm using the bourne shell for my simple little script because it needs to run on some older Solaris platforms that might not have bash.

Some implementations of "which" and "whence" will source the user's dot files, and that isn't really portable to all users. I'd love a simple UNIX utility that would just do the basic job of scanning PATH for an executable and reporting the full path of the first match.

But I'll settle for any /bin/sh solution that is stable for all users.

I'm looking for a solution that is better than writing my own /bin/sh loop that chops up $PATH and searches it one line at a time. It would seem that this is common enough that there should be an reusable way to do it.

My first approximation of the "long way" is this:

   IFS=:
   for i in $PATH; do
      if [ -x $i/$cmd ]; then
          echo $i/$cmd
      fi
   done

Is there something simpler and portable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2024-12-09 08:13:11

答案似乎是内置的“类型”。

% /bin/sh
$ type ls
ls is /bin/ls

The answer seems to be the 'type' built-in.

% /bin/sh
$ type ls
ls is /bin/ls
吻风 2024-12-09 08:13:11

也许 whereis 命令适合您?

whereis -b -B `echo $PATH | sed 's/:/ /g'` -f [commands]

例如,在我的计算机上,这有效:

whereis -b -B `echo $PATH | sed 's/:/ /g'` -f find man fsc

并导致:

find: /usr/bin/find
man: /usr/bin/man
fsc: /opt/FSharp-2.0.0.0/bin/fsc.exe /opt/FSharp-2.0.0.0/bin/fsc

whereis 手册页中的一个警告:

   Since whereis uses chdir(2V) to run faster, pathnames given
   with the -M, -S, or -B must be full; that is, they must begin
   with a `/'.

Maybe the whereis command will work for you?

whereis -b -B `echo $PATH | sed 's/:/ /g'` -f [commands]

e.g. on my computer, this works:

whereis -b -B `echo $PATH | sed 's/:/ /g'` -f find man fsc

And results in:

find: /usr/bin/find
man: /usr/bin/man
fsc: /opt/FSharp-2.0.0.0/bin/fsc.exe /opt/FSharp-2.0.0.0/bin/fsc

One caveat from the whereis man page:

   Since whereis uses chdir(2V) to run faster, pathnames given
   with the -M, -S, or -B must be full; that is, they must begin
   with a `/'.
翻了热茶 2024-12-09 08:13:11

这个问题在这里详细回答:https://unix。 stackexchange.com/questions/85249/why-not-use-which-what-to-use-then。底线:使用命令 -v ls

This question is answered in details here: https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then. Bottom line: use command -v ls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文