Tom Badran <[EMAIL PROTECTED]> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> How can i use an if in a bash script so that it will only run commands if the
> specified file is empty?
>
> Basically, ive set up a cache system by which the output of ifconfig is
> stored in /var/cache/IP/1
>
> Then, every minute my script is run. It first puts the output of ifconfig in
> /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then
> want to run a series of commands if the file diff is not empty.
>
> What im trying to do is get an email sent to me every time the IP changes on
> a specific machine, so i always have a record of it for logging in via ssh.
> The specific connection is ppp0 which auto redials on disconnect. If there is
> an easier way of doing this please share with me.
One way to do it is this sort of hack:
[rustyc@fw rustyc]$ cat bin/watchmail
#!/bin/bash
foo="`ls -l /var/spool/mail/rustyc`"
while : ; do
for j in 1 2 3 4 5 6 7 8 ; do
for i in 1 2 3 4 5 6 7 8 9 0 ; do
if [ "`ls -l /var/spool/mail/rustyc`" = "$foo" ] ; then
echo -n '.'
sleep 30
else
echo ""
echo `date`
#echo "Was $foo"
echo "`ls -l /var/spool/mail/rustyc`"
foo="`ls -l /var/spool/mail/rustyc`"
egrep '^Subject' /var/spool/mail/rustyc | tail -2
sleep 30
fi
done
# date #; echo -n '.'>>~/rnlog
done
echo ""
#echo -n `date ; ls -l /var/spool/mail/rustyc`
done
(that script watches for a change to file /var/spool/mail/rustyc and shows the
change (and a few lines from the file) when it does - sort of a poor man's
textual biff ;-)
The key lines are:
foo="`ls -l /var/spool/mail/rustyc`"
while : ; do
if [ "`ls -l /var/spool/mail/rustyc`" = "$foo" ] ; then
# no change
else
# file has changed
fi
done
On the other hand, diff will return nonzero return status if the files are different,
so you can just say something like:
diff foo1 foo2
if [ $? -eq 0] ; then
# no change
else
# change
fi
(Assuming I'm remembering the syntax for 'return status from last command' right,
if not I'm sure someone will fix this for us ;-)
rc
Want to buy your Pack or Services from MandrakeSoft?
Go to http://www.mandrakestore.com