LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting: how to tell when an nfs connection... (https://www.linuxquestions.org/questions/programming-9/bash-scripting-how-to-tell-when-an-nfs-connection-109464/)

jimieee 10-28-2003 09:02 AM

bash scripting: how to tell when an nfs connection...
 
... can't be made.

I'm writing a bash script that will run on computer a and open an nfs connection between computer a and computer b. Then it's going to copy some files accross and close the connection. I have it working, just a slight problem - if a connection to computer b can't be made (say for example because it's switched off) then the files just get copied to the directory where the nfs filesystem would have been.

Is there some easy what to tell when this has happened? At the moment I'm trying to print the output of the the mount statement to a variable and then read that variable to decide what to do next. I'm having trouble with this (probably because my code is all wrong)

MOUNTERROR=/root/backup-keeper/errorlog.txt

mount -t nfs 192.168.0.63:/mnt/FARMER-mirror/ /mnt/keeper/ > MOUNTERROR
if [ MOUNTERROR= "mount: RPC: Remote system error - No route to host" ] then
exit
fi

I turned off computer b to test this, but when I look in errorlog.txt there's nothing in there. Oh and bash doesn't seem to think much of everything after then.

Can anybody help me out? There's probably a way better way to tackle this isn't there?

Cheers,

~James~

david_ross 10-28-2003 01:23 PM

I think that you are trying is:
Code:

MOUNTERROR=/root/backup-keeper/errorlog.txt
mount -t nfs 192.168.0.63:/mnt/FARMER-mirror/ /mnt/keeper/ > $MOUNTERROR
if [ `cat $MOUNTERROR` -eq "mount: RPC: Remote system error - No route to host" ]; then
echo FAILED
fi

There are a few problems:
It may give the error on stderr
It may fail for another reason

Solution - look at the return code and only continue if it passed - ie:
Code:

mount -t nfs 192.168.0.63:/mnt/FARMER-mirror/ /mnt/keeper/
if [ $? -eq 0 ]; then
echo Mount OK
fi


jimieee 10-28-2003 01:53 PM

Thanks! I've made the changes, but will wait until tomorrow to test it cos I'm not near computer a or b until then!

~James~

jimieee 10-29-2003 03:23 AM

Ok First of all thanks for that, it seems to work a lot better than my first dismal attempt. Second, how do I tell it to actually stop processing the script? Third, how do I go telling if any error message has come up. I think the best way to tackle that one is to have something a long the line of "....if not null..." seeing as the mount command gives no feedback if the mount is successful....

Thanks for this!

~James~


All times are GMT -5. The time now is 06:23 AM.