It determines all the commands a shell script is using.
It is useful for error checking in a script, especially
when someone else is trying to run it on another system.
It will immediately tell them if the script is trying
to run commands are access resources that are not
available.
I have a function called Validate
that checks the availability of commands and resources.
Here's a copy of my current script.
It also produces an output file
which can be used with the validate function below.
#!/usr/bin/ksh
# Name: check_script
# Date: 2/28/2002
# Last Updated: 2/28/2002
# Version: 1.1
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Description: find out what common unix commands your script is using
# To Run: check_script [script name]
#
# note: script may falsely return matches from print or echo statements
# that are not acutally commands
#
################################################################################
# Functions
################################################################################
Write () # echo a message on screen and write to a file
# usage: Write "[Your message here]" [file]
{
MESSAGE="$1"
FILE="$2"
eval "echo \"$MESSAGE\""|tee -a $FILE
}
Check_Script () # find common commands on your system
# and see if they are used in your script
{
echo "Searching [$SEARCH_PATH] for commands...\c"
find $SEARCH_PATH -type f -only -prune|awk -F "/" '{print $NF}'|sort -u >> $TMP_LIST.commands
echo "DONE!"
COUNT=`cat $TMP_LIST.commands|wc -l`
echo "$COUNT commands found"
echo "Scanning [$SCRIPT] script for command use..."
cat $TMP_LIST.commands|while read COMMAND
do ###
cat $SCRIPT|egrep " $COMMAND |^$COMMAND " >> $TMP_LIST.filter1
cat $TMP_LIST.filter1|awk -F'#' '{print $1}'| >> $TMP_LIST.filter2
cat $TMP_LIST.filter2|awk -F'"' '{print $NF}' >> $TMP_LIST.filter3
cat $TMP_LIST.filter3 >> $TMP_LIST.hits
COUNT=`cat $TMP_LIST.hits|wc -l`
echo ".\c"
if [ $COUNT -gt 0 ]
then
echo
Write "----------------------------------------" $TMP_LIST.results
Write "[$COUNT] occurrences of [$COMMAND] in [$SCRIPT]" $TMP_LIST.results
echo "Validate $COMMAND rx" >> $TMP_LIST.Validate
> $TMP_LIST.hits
fi
done ###
echo "DONE!"
echo
echo "Command list can be found in: $TMP_LIST.commands"
echo "Results can be found in: $TMP_LIST.results"
echo
echo "Validate list can be found in: $TMP_LIST.Validate"
echo "Validate list is meant to be used with the Validate function"
echo "
}
################################################################################
# Variables
################################################################################
TMP_LIST=/tmp/TMP_LIST.check_script.$$ # generic tmp file for data
# make sure there is a "/*" at the end of each search path
# SEARCH_PATH will determine where the script will look for commands on your system
# all directory paths must be explicit - sub directories will not be searched
SEARCH_PATH="/usr/bin/* /usr/sbin/* /usr/contrib/bin/* /bin/* /sbin/*"
################################################################################
# Main
################################################################################
echo
echo "Thank you for using check_script version 1.0"
echo "The latest version can always be found out
echo "Please send any questions/comments to Robert@JORDAN2000.com"
echo
echo "note: This script may falsely return matches"
echo "from print or echo statements that are not acutally commands"
echo
SCRIPT=$1
if test -z $SCRIPT # make sure script name was given and exists
then
echo "Missing argument for script name!"
echo "usage: check_script [script name]"
echo
exit 1
fi
Check_Script
#!/usr/bin/ksh
# Name: validate.fun
# Version: 1.3
# Creation Date: 03/01/2002
# Updated: 03/12/2002
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Description: validate that commands or resources are available for script to use
#
# 03/12/2002 - added flag to turn EXIT_ON_ERROR on or off - R. Jordan
# 03/12/2002 - use which command so that full path for commands is not necessary
Validate () # validate that commands or resources are available for script to use
# Validate $1 r (test if file or dir is readable)
# Validate $1 rw (test if file or dir is readable and writable)
# Validate $1 rx (test if file is readable and executable)
# $1 is file or command to test, $2 can be r, rw or rx
# ex: Validate ls rx
# ex: Validate /tmp rw
{
EXIT_ON_ERROR="false" # true/false
Error () # sub function to exit upon errors
{
if [ "$EXIT_ON_ERROR" = "true" ]
then exit 1
fi
}
RESOURCE=$1
PERM=$2
WHICH_RESOURCE=`which $RESOURCE|egrep -vi "not found|no $RESOURCE"`
if test -n "$WHICH_RESOURCE"
then
RESOURCE=$WHICH_RESOURCE
fi
case $PERM in
r)
if test -a $RESOURCE
then
FOUND="true"
else
echo "WARNING: $RESOURCE not found"
fi
if test -r $1
then
FOUND="true"
else
echo "WARNING: $RESOURCE is not readable"
Error
fi
;;
rw)
if test -a $RESOURCE
then
FOUND="true"
else
echo "WARNING: $RESOURCE not found"
fi
if test -w $RESOURCE
then
FOUND="true"
else
echo "WARNING: $RESOURCE is not writable"
Error
fi
;;
rx)
if test -a $RESOURCE
then
FOUND="true"
else
echo "WARNING: $RESOURCE not found"
fi
if test -x $RESOURCE
then
FOUND="true"
else
echo "WARNING: $RESOURCE is not executable"
Error
fi
;;
*)
echo "Improper use of Validate function!"
echo $*
echo "usage: Validate [dir|file] [r|rw|rx]"
echo "Please view function comments for more info"
exit 1
;;
esac
}
# simple test of function
# success test
Validate /tmp rw # tests if the /tmp directory is writable
Validate ls rx # test if the ls command is executable
# failure test
Validate /tmp/abc r # tests if /tmp/abc is readable
Validate /tmp/abc rw # tests if /tmp/abc is writable
Validate /tmp/abc rx # tests if /tmp/abc is executable
Validate abc rx # tests if abc is executable
Robert G. Jordan
Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
![[lightsaber] [lightsaber] [lightsaber]](/data/assets/smilies/lightsaber.gif)