O'Reilly logo

Solaris™ Operating Environment Boot Camp by Dominic Butler, David Rhodes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example Shell Scripts

This script does the same thing as the which command. It will search the user's PATH for the first occurrence of the command supplied as a parameter:

#!/bin/ksh
if [[ -z ${1} ]]
then
  echo "usage: ${0} command" >&2
  exit 1
else
  cmd=${1}
fi
dirs=$(echo ${PATH} | sed 's/^:/. /
                      s/::/ . /
                      s/:$/ ./
                      s/:/ /g')
for dir in ${dirs}
do
  if [[ -f ${dir}/${cmd} ]]
  then
    echo ${dir}/${cmd}
    exit
  fi
done
echo "no ${cmd} in ${dirs}"

(Note: Some implementations of which do not display anything if the command supplied is not found. If you want to emulate this behavior, simply remove the final echo statement.)

This second example is a script that will allow you to change permission of a file (or files) using the full text representation of the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required