Ok yes this is a homework assignment BUT I am NOT looking to have the answers given to me.
The scenario is that I need a script that searches all my users home directories for bad words. I need the script to report to the screen certain info like username and word found and path. It should ask a user if it is good or bad and if bad be put into a file of list of bad file names, if good remove from list and no longer flagged by the script.
What I have so far is wanting to somehow do a loop. I do know that if I do a grep -r -e kill -e steal /home/*
I get a list of what I need. I also know that the list is separated by delimiters which I can pipe to get a variable for the things I need. I also know that I can put it to a file with a > filename.txt
What I have no clue is how to start a loop that would do this...
for each line in filename.txt
UNAME=...
LOC=...
TXT=...
echo "Username: $UNAME, Line with bad word found: $TXT, and Path and file name: $LOC. Is this a BAD file? (Y)"
Read YORN
if ["$YORN" = "Y" ]; then
>> (line of text from grep) badfiles.txt
fi
Next or whatever goes there...
The Requirements: Create a script to run to look for bad words such as (at the minimum) bomb, kill, our full name separated with a space, quit, and steal. You must run once to report to the screen and find all documents to show 1- Username 2- Line containing bad word and 3- path and filename. We are then supposed to change our script or do something to it so that we know where the files are then to ignore the files with legitimate uses for example kill process or quit a program (removes a flag)
What I have done: I have created users and documents to test including these words. I have ran my script (below) and put the output of my command into a file then I loop through the file line by line. For now I just have it echo-ing my variables to see if I am on the right track...
SCRIPT:
grep -r -e kill -e Anne -e bomb -e quit -e steal /home/* > /opt/badword.txt
while read line
do
LOC=`echo -e "$line" | cut -d : -f 1`
TXT=`echo -e "$line" | cut -d : -f 2`
UNAME=`echo -e "$line" | cut -d "/" -f 3`
echo $LOC
echo $TXT
echo $UNAME
done <badword.txt
OUTPUT ON SCREEN FROM RUNNING THIS SCRIPT:
[root@AnneCentOS opt]# ./script4
/home/brownb/doc1
hello my name is xxx i am going to plant a bomb
brownb
/home/brownb/doc2
I want to kill you
brownb
/home/mammaj/doc67
kill process
mammaj
/home/mammaj/doc22
Anne needs to go
mammaj
/home/swiftt/doc
I want to steal a bunch of money so i never have to work again
swiftt
/home/swiftt/doc300
I want to quit this job!
swiftt
[root@AnneCentOS opt]#
IN MY badword.txt:
/home/brownb/doc1:hello my name is xxx i am going to plant a bomb
/home/brownb/doc2:I want to kill you
/home/mammaj/doc67:kill process
/home/mammaj/doc22:Anne needs to go
/home/swiftt/doc:I want to steal a bunch of money so i never have to work again
/home/swiftt/doc300:I want to quit this job!
Finally I want to add a line after each line to ask user is this is a good file? I want to add an if statement that if the answer = Y then I can remove the line from badword.txt, I THINK I know how to create the question and if statement just don't know if there is a way to remove a specific line from the document I created.
grep
command so it finds "Kill" and "KILL", but not "skill", "bombastic", or "stealth". – G-Man Mar 7 at 0:45