Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check_script script to find out what commands your script is using

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
#!/usr/bin/ksh
#
# Date: 2/28/2002
# Name: check_script
# Ver: 1.0
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Purpose: 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
}

Validate () # validate that commands are available for script to use
# Validate $1 r (test if file is readable)
# Validate $1 rw (test if file 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 /usr/bin/ls rx
{
case $2 in
r)
if test -a $1
then
FOUND="true"
else
echo "WARNING: $1 not found"
fi
if test -r $1
then
FOUND="true"
else
echo "WARNING: $1 is not readable"
exit 1
fi
;;

rw)
if test -a $1
then
FOUND="true"
else
echo "WARNING: $1 not found"
fi
if test -w $1
then
FOUND="true"
else
echo "WARNING: $1 is not writable"
exit 1
fi
;;
rx)
if test -a $1
then
FOUND="true"
else
echo "WARNING: $1 not found"
fi
if test -x $1
then
FOUND="true"
else
echo "WARNING: $1 is not executable"
exit 1
fi
;;
*)
echo "Improper use of Validate function!"
echo $*
echo "Please view function comments for usage"
exit 1
;;
esac
}

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|sort >> $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 LINE
do ###
LOCATION=$LINE
COMMAND=`echo $LINE|awk -F"/" '{print $NF}'`
cat $SCRIPT|awk -F"#" '{print $1}'|grep "$COMMAND "|egrep -v "[a-z]$COMMAND|$COMMAND[a-z]|[A-Z]$COMMAND|$COMMAND[A-Z]|-$COMMAND|_$COMMAND" >> $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 $LOCATION 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 "}

################################################################################
# Validate - check that commands and resources are available for script to use
################################################################################

Validate /usr/bin/awk rx
Validate /usr/bin/cat rx
Validate /usr/bin/echo rx
Validate /usr/bin/expr rx
Validate /usr/bin/grep rx
Validate /usr/bin/head rx
Validate /usr/bin/more rx
Validate /usr/bin/netstat rx
Validate /usr/bin/nslookup rx
Validate /usr/bin/read rx
Validate /usr/bin/sort rx
Validate /usr/bin/tail rx
Validate /usr/bin/tee rx
Validate /usr/bin/test rx
Validate /usr/bin/uname rx
Validate /usr/bin/wc rx

################################################################################
# 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
SEARCH_PATH="/usr/bin/* /usr/sbin/* /usr/contrib/bin/*"
# SEARCH_PATH="/usr/bin/* /usr/sbin/*"
# SEARCH_PATH="/usr/bin/*"

################################################################################
# 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 [ ${#SCRIPT} -lt 1 ] # 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

Validate $SCRIPT r

Check_Script

--------------------------------------------
Sample of script running...

note: This script may falsely return matches
from print or echo statements that are not acutally commands

Searching [/usr/bin/* /usr/sbin/* /usr/contrib/bin/*] for commands...DONE!
793 commands found
Scanning [test] script for command use...
........................
----------------------------------------
[1] occurrences of [cat] in [test]
..............................................................
----------------------------------------
[3] occurrences of [echo] in [test]
..........................................
----------------------------------------
[1] occurrences of [grep] in [test]
............................................................................................................................................................
----------------------------------------
[1] occurrences of [read] in [test]
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE!

Command list can be found in: /tmp/TMP_LIST.check_script.26082.commands
Results can be found in: /tmp/TMP_LIST.check_script.26082.results

Validate list can be found in: /tmp/TMP_LIST.check_script.26082.Validate
Validate list is meant to be used with the Validate function
Robert G. Jordan

Robert@JORDAN2000.com
Unix Admin, United Airlines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top