Wolverines

Public Forums => Tech Talk => Topic started by: Krogoth on October 05, 2008, 06:32:02 PM



Title: Unix Scripting
Post by: Krogoth on October 05, 2008, 06:32:02 PM
Hopefully there are some linux gurus around.

I'm writing a bash script to compare a logfile to the contents of a directory. The logfile gets written on first running the script. Every subsequent run compares the logfile to the directory, tells the user when a file from the log is missing from the directory, and the rewrites the logfile. Its so specific because its just for my scripting lab.

My problem lies in the red colored line. I need to have the script compare the file from the log to the directory. If it can't find it print "$file missing from directory $DIR", flag a file as missing (to prevent the "no files missing" message), and continue with the loop. Our professor likes to leave a few things to us to figure out, and apparently nesting a command as the test in an if statement was one of them. Anyone know the syntax for it? The closest thing I could figure out invloved curly braces, but it throws an error:

./dirlog: line 20: syntax error near unexpected token `then'
./dirlog: line 20: `    if { ! find $file } ; then'

Any ideas?

Quote
#! /bin/bash

FILEMSNG=0
DIR=$1
if [ ! -d $DIR ] ; then
        echo "usage: dirlog directory_name"
        exit
fi

cd $DIR

if [ ! .logfile ] ; then
        echo ".logfile created for $DIR"
        ls > .logfile
        exit
fi

for file in .logfile
do
        if { ! find $file } ; then
                echo "$file missing from directory $DIR"
                FILESMNG=1
        fi

done

if [ FILEMSNG -eq 0 ] ; then
        echo "no files missing from directory $DIR"
fi

rm .logfile
ls > .logfile
echo ".logfile updated for directory $DIR"




Title: Re: Unix Scripting
Post by: Ziprar on October 06, 2008, 03:11:10 AM
My shell scripting is a little rusty, but couldn't you just use -e?

if [ ! -e $file ] ; then


Title: Re: Unix Scripting
Post by: Krogoth on October 06, 2008, 04:26:52 AM
yea, I was quickly informed by a friend that if [ ! -f $file ] ; then would've worked just fine. Now, though, I'm having problems with even the first if statement. I pass no parameters into the file, which should kickback "usage: dirlog directory_name" and exit, but instead it continues running the script normally. The second if gets skipped as well. In the for loop, it only checks for the actual file ".logfile" and not the list in the file (even though the syntax is supposedly right).


fmclip.com