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 ...

Get Solaris™ Operating Environment Boot Camp 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.