LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Script to Detect USB drive mount status (https://www.linuxquestions.org/questions/programming-9/bash-script-to-detect-usb-drive-mount-status-285269/)

nutthick 02-02-2005 05:28 AM

Bash Script to Detect USB drive mount status
 
I'm trying to write a script to detect if a USB drive mounted correctly. The problem I'm having is that the error code returned from mount /mnt/usb by $? when a drive is missing, is the same as if the drive has already been mounted, or is incompatible. I've tried getting $? from ls, du and df but they all respond with 0 whether the drive is there or not.

Does anyone know how to get around this? I just need to know if the drive mounted OK and if not what the problem was.

Thanks

nixcraft 02-02-2005 06:21 AM

let us say your mount command is as follows in script

Code:

mount /from /to
ST="$?"

[ "$ST" == "0" ] && echo "Mounted" || echo "Error"

So ST will store status code of mount command and if it is zero it means mounted otherwise not mounted

nutthick 02-02-2005 06:34 AM

nixcraft - That is producing the same problem. If the USB drive has already been mounted, or is not plugged in (two very different state) then ST returns the same value of 32.

I'm just having a look at /etc/mtab as I think that hold a record of what is mounted and what isn't

nixcraft 02-02-2005 06:40 AM

You can do that or

mount | grep YOUR-MOUNT-POINT-DIR

if [ "$?" == "0" ]; then

echo "Mounted"

else
code to mount it
fi

theYinYeti 02-02-2005 07:17 AM

What about:
grep "..." /etc/mtab

Yves.

nutthick 02-02-2005 07:18 AM

nixcraft - That works, but can you explain to me how the grep is working in the mount line.

Thanks

nixcraft 02-02-2005 08:17 AM

Yes

If mounted directory found exit status will be zero otherwise it will be nonzero value.


All times are GMT -5. The time now is 10:10 PM.