Help - Search - Members - Calendar
Full Version: bash script help
Linuxhelp > Support > Programming in Linux
sparda963
I am fairly new to my job and trying to learn linux as I go. I have a job at a high school and the admin is in charge of maintaing the acceptable use policy and reporting any violations to the principal. One of the things he needs to do is look at users files to make sure they arnt writing letters with threatening subjects such as plans to kill a teacher or place a bomb somewhere in the building.

I know there has to be an easier way to do this then reading through each persons files every once in a while, but I really have no idea where to start. From my somewhat limited knowledge of linux so far I know I must use the grep command for the bad words on the all the users files. I also need to loop this untill it has scanned all the files and then redirect the output to a file.

To make the report easy to read I would need it to say something like "User `username` has the word `badword` in the file `directory path to file`". That would have to happen for each file that has one of the words in the file.

I know there has to be a way to do this, any help would be greatly appreciated.
DS2K3
There are a couple of problems with that method - Firstly, scanning files that aren't plain-text is a bit hit and miss, since they often dont contain portions of text which can be read without using the correct filter, so would need to be converted to a plain-text version first, which would be quite processor intensive. Secondly, in order to maximise the efficieny of the scanner you would need to keep a list of files which have been scanned, and when they were scanned, so that you only need to scan files that have changed since that time - That would require soe kind of database. So, you would probably be better of using a proper application to do it for you. (Perhaps a modified virus scanner?)

I'm not particularly hot on shell scripting, so I'm not really sure how you would go about implementing it in a shell script.

If you want to filter email, then there are numerous mail-scanning packages available for servers like postfix.

D
sparda963
Ok after doing some work on this yesterday I have come up with the following code, but it has one problem. I will post the code first, then post the problem im having after the code.

CODE
for BADWORD in kill bomb steal
do
GLOBAL=`grep -r -F -i "$BADWORD" /home`
UNAME=`echo $GLOBAL | cut -d "/" -f 3`
WORD=`echo $BADWORD`
LOC=`echo $GLOBAL | cut -d ":" -f 1`
echo "User $UNAME has the word $WORD in $LOC"
done


Now this almost works perfectly, except that there is one problem. When more then one file is found with one of the words, we will say kill for example, the output to the GLOBAL variable puts all the information for each of the files found into one line with a ":" delimiter. What I need to have happen is that each of the delimited lines needs to be a seperate line in the output. Here is an example.

Current output of GLOBAL variable
/home/testa/bad:I am going to kill you /home/testb/bad:I am going to kill you /home/testc/bad:I am going to kill you
/home/testa/bad:I am making a bomb /home/testb/bad:I am making a bomb /home/testc/bad:I am making a bomb

What I need the output to the GLOBAL variable to be
/home/testa/bad:I am going to kill you
/home/testb/bad:I am going to kill you
/home/testc/bad:I am going to kill you
/home/testa/bad:I am making a bomb
/home/testb/bad:I am making a bomb
/home/testc/bad:I am making a bomb

Is there anyway i can controll the grep command to get this to happen?
Hemant
I would rather advise you to use another loop:
CODE
#!/bin/bash
for badword in kill bomb steal
do
 for file in `find .`
 do
   if [ `grep -c -h $badword "$file"` -ne 0 ]
   then
    uname='do the name cut here from file'
    word=$badword
    loc=$file
   fi
 done
done
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.