GREP loop

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I need to search through the users home directories for keywords, display them. The code listed below will show me the first result but I am not sure how to loop through the results. I am not sure how to make a loop work.

I then need to exclude users from the search if what that script found was phrase that is acceptable. So if the script is ran again the user will not come up again in the search.

I am using a file with a list of the word to search, could I do this with the exclusions as well?

  1. Relevant commands, code, scripts, algorithms:
    ECHO
    GREP

  2. The attempts at a solution (include all code and scripts):

RESULT=`grep -irf searchwords /home`
 
FLOC=`echo $RESULT | cut -d : -f 1`
 
UNAME =`echo $FLOC | cut -d / -f 3`
 
PHRASE=`echo $RESULT | cut -d : -f 2`
 
 
 
echo "Username: $UNAME, Line with bad word found: $PHRASE, and Path and file name: $FLOC."
 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Northeast Wisconsin Techical College, Green Bay, WI - Joe Cicero - UNIX I

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

How many keywords in the file, how many bytes? Command lines have their limits.

Right now there are only about 6 words in the file

So, turn them into an egrep pattern. Use a 'while read' on the file and concatenate them with pipe marks.

So here is what a fellow student and I came up with, and looks to be working...

RESULT=($(grep -irf searchwords /home | cut -d / -f3))

for USERLIST in ${RESULT[*]}
do
	CHECKGOOD=`grep $USERLIST /opt/guser`
	if [ "$CHECKGOOD" == "" ]
	then
		PHRASE=`grep -irf searchwords /home/$USERLIST | cut -d : -f2`
		FLOC=`grep -irf searchwords /home/$USERLIST | cut -d : -f1`
		echo "Username: $USERLIST has the following phrase: $PHRASE located at: $FLOC"
		echo "Was this an appropriate use of the phrase?"
		read RESP
		LRESP=`echo $RESP | tr [:upper:] [:lower:] | cut -c 1`
		if [ "$LRESP" == "y" ]
		then
			echo $USERLIST >> guser
		fi
	fi
done	

Good so far. You are making some typical beginners errors, though:

RESULT=($(grep -irf searchwords /home | cut -d / -f3))

The outer "(...)" is superfluous, but the quoting is missing. Whenever a string might contain whitespace in a shell script you should quote it: first, to protect the whitespace from being interpreted away by the shell and second to make clear that the shell should not treat the whitespace as a separator.

Another thing is: you nowhere declare RESULT to be an array, but you treat it as one further down. Even if the shell allows for such liberty you should write your scripts as if it doesn't. In this case the outer braces are necessary but the subshell ("$(...)") isn't. The following should work:

typeset -a RESULT=(grep -irf searchwords /home | cut -d / -f3)
for USERLIST in ${RESULT[*]}

This is working but a dangerous way to loop through an array. The shell has a maximum line length (see "limits.h") and works on a lin in this way: first, all the variables are scanned and replaced by their value. Then the resulting line is evaluated. Depending on how big the array RESULT is the line could be pretty long and this could result in the shell rejecting the line with "line too long". Use a while-loop with an index counter instead:

(( iCnt = 0 ))
while [ $iCnt -lt ${#RESULT[*]} ] ; do
     do-something "${RESULT[$iCnt]}"
     (( iCnt += 1 ))
done
PHRASE=`grep -irf searchwords /home/$USERLIST | cut -d : -f2`
FLOC=`grep -irf searchwords /home/$USERLIST | cut -d : -f1`

This is possible, but a time-killer. You issue the same command twice, every time using a different part of the output instead of doing it once. Here is a trick: "read" can read in not only one but several variables:

grep -irf searchwords /home/$USERLIST | cut -d : -f1,2 | read FLOC PHRASE

This will do the same as your two lines above.

CHECKGOOD=`grep $USERLIST /opt/guser`

You do this - use of backtics - a lot. You shouldn't. backtics are outdated and deprecated and only supported for backwards compatibility reasons. You should use the subshell-command instead:

var=`command-list`    # wrong and to be avoided
var="$(command-list)"  # correct

I hope this helps.

bakunin

1 Like

You do not need " around $(...) in a simple variable assign, as it is already implicitly quoted and will all end up in your variable in one piece. In other situations, you might need that because the shell gives it a second look, but not simple variable assignment.

The indexes can be generated at point of use using $(( iCnt++ )) in bash.

You can have pipeline parallelism and no array variable with a simple pipe:

grep -irf searchwords /home | cut -d / -f3 | while read USERLIST
 
for:
 
RESULT=($(grep -irf searchwords /home | cut -d / -f3))

for USERLIST in ${RESULT
[*]}
1 Like

Thank you for the tips and the explanations, very helpful.