LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2015, 10:51 AM   #1
annabell265
LQ Newbie
 
Registered: Mar 2015
Posts: 2

Rep: Reputation: Disabled
Bash Shell Scripting need help figuring out process for homework - JUST WANT DIRECTIO


Ok yes this is a homework assignment BUT I am NOT looking to have the answers given to me. I am in the 6th week of my first Linux class ever and we are in our few weeks of beginning scripting. I have some ideas of what to do or where to start but not many and no one to bounce any ideas off...we are using UNIX Bash shell so any others I have no clue. 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...sorry if this is crazy I just really need some direction. I am trying to learn so please don't give me the answer...that will do nothing for me and I will not be able to explain the code I came up with.
 
Old 03-06-2015, 11:13 AM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
We at LQ do not promote homeworks but as you are not looking for answer just the guidance that should be ok. Few things to start off with:

1. Did you put all the requirements on a piece of paper or notepad?
2. You asking for the loop but did you check how the work flow should be?
3. Script should look for bad words, machine wouldn't know what is bad word. Did you define how it will identify bad word.
4. How system will check for those bad words in files?

It will be good if you can share what you have come up with so far. Also, how you are planning to solve / work on each requirement they have given.
 
Old 03-06-2015, 11:16 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Another option is to use the 'find' command to exec your script on each file.
 
Old 03-06-2015, 12:50 PM   #4
annabell265
LQ Newbie
 
Registered: Mar 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Yes I AM NOT LOOKING FOR ANSWERS, I want to learn and understand everything I put in my scripts...otherwise I will never truly get UNIX haha...I'm only 6 weeks in so bare with me.

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:

"script4" 19L, 382C written
[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...

Like I said I am VERY new to this and don't even know if this way of doing this is right or wrong...Thank you
 
Old 03-06-2015, 01:14 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
What I would say is:
"Determine how you would accomplish this by typing on the command line. Once you've determined this, then you know what you need to put into the script and it becomes a matter of how you code the script."
Other things to consider is how to "bound" the problem. For instance whether or not you'll look for bad words within files or just as part of the file names. Whether or not a word is bad if seen as part of another word, for instance butterfly. Is that bad, is part of it bad? If there's a folder named butterfly-pictures is that bad versus good? Now that I mention folders, there's another consideration, whether or not something is a file, a symbolic link, or a folder.
 
Old 03-06-2015, 08:07 PM   #6
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
Alright, I am assuming that rest of your script is working fine. And you are only stuck at the following section:

[quote}
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...[/quote]

For this section I would suggest the following:

1. You can run the loop on the basis of number of entries you have in the file. You can get the number of entries using
Code:
 cat badwords.txt | wc -l
2. In the loop you can put the question whether the file is legit or not. If a user say the file is legit then you have to do 2 things:
a. cut / awk the second section (the reason I am using cut / awk against second section because we are concentrating on content of the file) and store it in a variable
b. do an inverse match. Example: cat badwords.txt | grep -v "variable_from_section_2.a" > badwords.txt.new
3. Before ending the loop copy badwords.txt.new to badwords.txt so that for the next user that line doesn't show up.

Hope it helps.
 
Old 03-07-2015, 08:37 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Member response

Hi,

Welcome to LQ!

I suggest that you look at the following to enhance your class text;
Quote:
Just a few links to aid you to gaining some understanding;
Rute looping section can help you to understand the necessary techniques. The links to other BASH guides are very useful. Of course knowing Linux Command line will be a necessity to lead to a solution.
Hope this helps.
Have fun & enjoy!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash scripting homework question arungala Linux - Newbie 53 05-06-2012 06:22 AM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 01:51 PM
bash scripting question, homework related cybergeek11235 Linux - General 4 10-12-2008 11:59 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 02:19 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration