#!/bin/bash
# inpath - Verifies that a specified program is either valid as is,
#   or that it can be found in the PATH directory list.
############################################
# 3.1 
############################################
in_path()
{
 
  cmd=$1        path=$2         retval=1
  oldIFS=$IFS   IFS=":"   # 取消IFS分隔符影响
  for directory in $path
  do
    if [ -x $directory/$cmd ] ; then #  给定PATH路径以及命令可执行返回的0值,为真则说明在PATH目录下
      retval=0      # if we're here, we found $cmd in $directory
    fi
  done
  IFS=$oldIFS
  return $retval
}
####################################
# 3.
###################################
checkForCmdInPath()
{
  var=$1
  if [ "$var" != "" ] ; then              #函数非空
    if [ "${var%${var#?}}" = "/" ] ; then # {var#?}去掉第一个, 判断是否为“/”路径开头
                                          #再以"%"去掉剩下的为 “/”
      if [ ! -x $var ] ; then    #不可以执行返回1,判断其实路径
        return 1
      fi
    elif ! in_path $var $PATH ; then
      return 2
    fi
  fi
}
############################################
#1.检测是否已经传递1个参数,否则退出且提示
############################################
if [ $# -ne 1 ] ; then
 echo "Usage: $0 command" >&2 ; exit 1
fi
#========================================
##########################################
#2.给函数传入参数
##########################################
checkForCmdInPath "$1"
case $? in
  0 ) echo "$1 found in PATH"                  ;;
  1 ) echo "$1 not found or not executable"    ;;
  2 ) echo "$1 not found in PATH"              ;;
esac
exit 0

#==================================================

测试

[root@www ~]# ./check_path_command.sh ls
ls found in PATH
[root@www ~]# ./check_path_command.sh /etc
/etc found in PATH
[root@www ~]# ./check_path_command.sh sdfadsfa
sdfadsfa not found in PATH