LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-02-2005, 05:28 AM   #1
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Rep: Reputation: 30
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
 
Old 02-02-2005, 06:21 AM   #2
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
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
 
Old 02-02-2005, 06:34 AM   #3
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
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
 
Old 02-02-2005, 06:40 AM   #4
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
You can do that or

mount | grep YOUR-MOUNT-POINT-DIR

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

echo "Mounted"

else
code to mount it
fi
 
Old 02-02-2005, 07:17 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
What about:
grep "..." /etc/mtab

Yves.
 
Old 02-02-2005, 07:18 AM   #6
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
nixcraft - That works, but can you explain to me how the grep is working in the mount line.

Thanks
 
Old 02-02-2005, 08:17 AM   #7
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
Yes

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mount does not detect a vfat filesystem in my usb stick ernesto_cgf Linux - Hardware 3 12-07-2005 07:19 AM
Suse 10.0 not detect USB Zip drive Donb Suse/Novell 1 10-21-2005 03:51 PM
How to auto-detect USB drive in RedHat 9? pelakh Red Hat 1 05-06-2004 01:02 PM
bash script to detect scsi address at boot ewto Programming 2 10-20-2003 02:47 AM
mount usb module then mount usb hard drive guanyu Linux - Hardware 1 10-08-2003 11:50 AM


All times are GMT -5. The time now is 01:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration