Hour 14

1: Write a function that determines whether a command is located in one of the directories in $PATH. The command will be supplied as the first argument. If the command is located in one of the directories in $PATH, your function should print the full path to the command and return 0. Otherwise your function should return 1 and optionally print an error message.
A1: A possible implementation is
inPath () {
   OLDIFS="$IFS"
   IFS=:
   RC=1
   for i in $PATH
   do
      if [ -x "$i/$1" ] ; then
         echo "$i/$1"
         RC=0
         break
      fi
   done
   IFS="$OLDIFS"
   return $RC
}
2: Write a function to make a directory (and all of its parents) change to that directory and then print the full path of that directory. Please include error checking at all levels. Make sure that your function ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.