如何检测脚本中是否存在外部命令?

chr*_*lee 5 utilities executable shell-script

有没有办法检测是否存在外部命令(即 wget、svn)?

更具体地说,今天,我试图运行我编写的脚本之一,但该人没有wget安装或安装 svn。

该脚本只是下载一个文件提取它或使用 svn 导出主干。

man*_*ork 13

在 Bash 中,typeshell 内置提供了有关可执行事物的信息:别名、函数、可执行文件。详情请参阅help type

# just check for existence
type -t 'yourfunction' > /dev/null || echo 'error: yourfunction not found'

# explicitly check for given type
[[ "$( type -t 'yourfunction' )" != 'function' ]] && \
    echo 'error: yourfunction not found or is not a function'
Run Code Online (Sandbox Code Playgroud)