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 |
let us say your mount command is as follows in script
Code:
mount /from /to |
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 |
You can do that or
mount | grep YOUR-MOUNT-POINT-DIR if [ "$?" == "0" ]; then echo "Mounted" else code to mount it fi |
What about:
grep "..." /etc/mtab Yves. |
nixcraft - That works, but can you explain to me how the grep is working in the mount line.
Thanks |
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. |