用户名: 密码: 忘记密码? 注册

判断命令是否存在(在$PATH中)

作者:  时间: 2010-11-27

 

#!/bin/sh

checkForCmdInPath()
{
    var=$1

    OldIFS=$IFS
    IFS=":"

    for directory in $PATH    
    do    
        if [ -x $directory/${var##/*/} ]
        then
            echo "${var##/*/} found in $directory"
            return
        fi
    done

    echo "${var##/*/} not found"
    IFS=oldIFS
}

if [ $# -ne 1 ]
then
    echo "Usage: $0 command" >&2
    exit 1
fi    

checkForCmdInPath "$1"

exit 0