如何找到在bash可用的可执行文件 [英] How to find a usable executable file in bash

查看:177
本文介绍了如何找到在bash可用的可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个小脚本发现作为参数传递例如可执行文件。

  ./ testexec杜LS MD

我如何拿到剧本不输出命令未找到 - 例如不显示MD命令??

错误输出

 #!/ bin / sh的    在文件名$ @
    做
    其中$文件名
    DONE


解决方案

如果你正在使用bash,你应该使用内置的类型,而不是外部工具,它。 type命令将返回一个非零的退出状态,如果没有找到该命令,这使得它很容易与有条件使用。

 在$ @的文件名;做
   如果type -P$文件名>的/ dev / null的;然后
       回声在PATH中找到:$文件名
   科幻
DONE

I have written this little script to find executables that are passed as arguments e.g.

./testexec du ls md 

How do I get the script to not output commands that are not found - e.g. not to display error output for "md" command ??

  #!/bin/sh

    for filename in "$@"
    do
    which $filename
    done

解决方案

If you are using bash, you should use the builtin "type" rather than the external utility "which". The type command will return a non-zero exit status if the command is not found, which makes it easy to use with a conditional.

for filename in "$@"; do
   if type -P "$filename" >/dev/null; then
       echo "found in PATH: $filename"
   fi
done

这篇关于如何找到在bash可用的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆