Monday, May 7, 2012

[Bash] Check the existence of a command

#!/bin/bash
# check the existence of a command [ base on the returns of which and alias ]
# usage:
#           chkcmd.sh command-list
# e.g.,
#           chkcmd.sh pdfit
#           http://scriptdemo.blogspot.ca

source ~/.bashrc
if [ $# -eq 0 ]; then
   sed -n '2,6p' `which chkcmd.sh`
   exit
else
   for mycmd in $*
   do
      isInPath=`which ${mycmd} 2< /dev/null`
      if [ ${#isInPath} -eq 0 ]; then
         isAlias=`alias ${mycmd} 2< /dev/null`
         if [ ${#isAlias} -eq 0 ]; then
            echo "${mycmd} is not in the path";
         else
            echo "${mycmd} is an aliased command";
         fi
      else
         echo "${mycmd} is in the path";
      fi
   done
fi

No comments:

ShowCalendar