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!

grep, sed, awk challenge

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
I am trying to use this in a script
to analyze what commands another script is using.

Here's a sample script file.
I am trying to write a grep,egrep sed or awk
command that will figure out which of
the lines below is actually a command

#!/usr/bin/ksh
echo "Running wc command"
echo "hello"|wc
# this is where I run wc
wc
wc
wc # running command
TEXT=`echo wc`
TEXT2=`echo "wc"`

In this particular instance,
only lines 3,5,6, and 7
should be returned. Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Extremely difficult to do with any robustness.

Not worth my time, IMHO. Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
yes, that's why it's a challenge :)

although I am learning perl and like it very much,
it isn't alway available to use.

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Well, I'm not going to write it, but I do have some comments.

Every leading word is either a command, a variable assignment or an error. A ". command" also counts.

If it's a command, it's either a builtin or an external command.

Variable assignments are easily stripped out by a pattern
just a little more complex than "^ *[^ ]*=". The actual pattern required would be MUCH easier in Perl.

To distinguish builtins, you either need a list to compare against or you need to run "file" on each candidate. Errors are those things that aren't builtins, aren't found in PATH, or aren't called with a specific PATH.

Then you need to check if the thing called is executable, but if it's called with ". command" it doesn't have to be.

Much more than "awk", "grep", "sed".



Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Thanks for the additional info, Tony.
I already have a script that's about 80-90% accurate.
Under certain circumstances it fiinds commands
which are actually echo statements or comments.

I think eventually I will have shell version
and perl version of this script.

Robert
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top