Click to See Complete Forum and Search --> : Bash Scripting: Checking if file contents are valid


digg_guy
04-26-2007, 02:58 PM
Hey everyone, I'm pretty new to bash scripting and I have an assignment that one of our scripts asks us to make a script to check if a file exists inside a log file and compare the log file to the file attributes to see if the file has changes since the last time the script was ran. The user enters an argument for the file name. I'm not quite sure how to find out if the file has an entry inside the log file or not i was using something like if [ `cat log | grep '$1'` ]; but that doesn't seem to work. below Is the specs on the script itself if anyone could help me that would be great !!!

1.(23 pts) Write a script that will perform the following actions:
a.Accept a filename as an argument.
b.Check to see if the file exists.
c.If the file doesn’t exist, give an error message and exit.
d.If the file does exist, Check a log file (of your creation) to see if an entry is listed for the file. If an entry is listed for the file, compare the entry to the file. If the file has been change, print:
File has been changed
Previous File Information:
followed by the entry from the log file.
i.Then update the entry to reflect the new information.
ii.If file has not been changed, Print:
“File has not changed since last check.”
e.If an entry is not listed for the file, then print “No entry for file [filename] and create an entry for the file. Then print: Entry created.
f.The logfile entries should contain:
Filename, modified date, file size.
g.The comparison should check both the file size and date modified to see if the file has changed.
h.The script will have to check and see if the logfile exists. It will have to create the logfile the first time the script is ran.

bwkaz
04-26-2007, 06:51 PM
We generally don't help people with their homework. The primary reason is, you learn more by doing it yourself, but the secondary reason is, whoever assigned it to you should be the one you ask for help if you need it. They know more about what they want you to learn, so they know more about what not to tell you outright. ;)

(And who's to say that whoever assigned this to you isn't reading this forum? :))

ghostdog74
04-26-2007, 07:34 PM
though i will not do your homework, the part where you are not getting results i can suggest to you. you can try this (double quotes) :
grep "$1" logfile

digg_guy
04-26-2007, 08:41 PM
Well, thanks for the help I've tried double quotes doesn't work all I really need is a hint, but I understand where you are coming from. I've asked my teacher, but he doesn't give any hints. I was stumped that was I thought I would see if anyone would give me a tip but I understand why you can't say anything I'll figure it out hopefully

One question I know on the command line grep you use single quotes to look for a word ex. grep 'log' file but when using command substituion do you need the single quotes inside double ? like `cat log | grep '"$1"'` ??? or would you just need the double quotes or single ?

digg_guy
04-26-2007, 08:53 PM
never mind i figured it out. not sure if this is the proper way to do it, but I used command substitution inside a variable then put that variable in an if structure it works, but if anyone can come up with a better way let me know just for personal reasons.

ghostdog74
04-27-2007, 04:04 AM
I don't understand why you said it did not work. an example

# more file
This is first line
This is second line
This is the line with dollar sign and one : $1
this is the final line
Full stop
# grep '$1' file
This is the line with dollar sign and one : $1
# echo $1

# grep "$1" file
This is first line
This is second line
This is the line with dollar sign and one : $1
this is the final line
Full stop

I have created an example file, inside this file, there is a "$1" in line 3. the syntax : grep '$1' file tells grep to find a literal '$1', that means "as it is", no variable substitution. So if your purpose is to find a literal '$1' , then this grep syntax would work. HOWEVER, if you wanted to grep for a value that is referenced by $1, which normally would be your script's first argument, then this syntaxx : grep "$1" file is correct. Notice that in the example, when i echoed $1, it shows null.... so when we do: grep "$1" file, the results shows all lines...

digg_guy
04-27-2007, 11:39 AM
well the reason it didn't work for me is I had it inside a if statment

if [ `grep "$1" log` ]; then ........

it kept on giving me an error saying to many arguments or something but when I encased the argument inside single quotes it would give me no error but when I debugged it it would give say there was no value for that argument

I agree that it works perfectly fine on the command line, but for some reason it won't work inside a script.

ghostdog74
04-27-2007, 11:56 AM
do it step by step:
[/code]
# grep "$1" log
# if [ $? -eq 0] ; then
echo "found"
[/code]

digg_guy
04-27-2007, 12:43 PM
man I never thought about doing it like that ! thanks !

bwkaz
04-27-2007, 09:57 PM
# grep "$1" log
# if [ $? -eq 0] ; then
echo "found" Ah, but in shell, the "if" builtin tests the return value of another command. In this code, it's testing the return value of the square-bracket command (which is the same as "test": your distro should have a /bin/[ symlink to /bin/test, and although the shell treats both test and [ as builtins, they'll work as programs too). But the [ command here is comparing the return value of the grep. So why not just put the grep into the if instead? ;)

And actually, the manpage for grep contains an option that will prevent it from printing the matching line, too, so that may help even more (unless you need that line).

ghostdog74
04-28-2007, 03:16 AM
But the [ command here is comparing the return value of the grep. So why not just put the grep into the if instead? ;)

OP is having problem doing that. so i just showed him another way that is commonly done.

bwkaz
04-28-2007, 02:15 PM
Yes, I know. But the OP is (was?) having the problem that he is (was?) having because there was too much stuff in his if command. The code that you posted is good, and should work fine -- but it can be made even less complicated, and it'll still work. :)