gfxHomeForumHelpLoginRegistergfx
gfxgfx
      « previous next »
Pages: [1] Go Down Print
Author Topic: Unix Scripting  (Read 1978 times)
Krogoth
Has a PS2 Hardon
WoW Member
Hero Member
***

Karma: +6/-0
Offline Offline

Posts: 709


« 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"


Logged

Ziprar
WoW Member
Full Member
***

Karma: +4/-1
Offline Offline

Posts: 182



« Reply #1 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
Logged

Veadria/Veadric/Groxxarn/Grylyn
Krogoth
Has a PS2 Hardon
WoW Member
Hero Member
***

Karma: +6/-0
Offline Offline

Posts: 709


« Reply #2 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).
Logged

gfx
Pages: [1] Go Up Print 
gfx
Jump to:  
gfx
Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
Helios / TinyPortal v0.9.8 © Bloc
gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!
fmclip.com