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

 

#!/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


作者: kevin33643   发布时间: 2010-11-27