LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-10-2009, 05:40 AM   #1
siema
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
BASH scripting - detect if cdrom/pendrive inserted


I'm currently working on a script which would check if a cdrom/pendrive is inserted (and not yet mounted) and mount it to some directory (name determined by for example device name).

However I'm stuck at the very beginning - how to check if cdrom/pendrive is inserted?
What I'm aiming for is to get its /dev/ location.

Yea I know that it's already implemented in 90% of distros, the script is for educational purposes;P
 
Old 12-10-2009, 08:26 AM   #2
hunternet93
Member
 
Registered: Jun 2009
Posts: 87

Rep: Reputation: 31
If you know what the device name of the pendrive is, you could check if it has a device file in /dev/, or could read the output of lsusb and see if you find a pendrive. Hotplugd can detect USB stuff and run scripts in response. As for cdrom, the device is always in /dev/ even if no cd is inserted, so the only way I can think of to detect it is to try mounting it and reading mount's respone. If no CD is inserted, mount will say "Mount: no medium found." Hope this helps!
 
Old 12-10-2009, 08:38 AM   #3
siema
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the info, the cdrom part should do the trick.

As for the pendrive, let's assume that we don't know the name.
Anyway how would it be named in /dev/ ? tty, usb-something or what?
 
Old 12-10-2009, 02:00 PM   #4
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
Quote:
Originally Posted by siema View Post
Thanks for the info, the cdrom part should do the trick.

As for the pendrive, let's assume that we don't know the name.
Anyway how would it be named in /dev/ ? tty, usb-something or what?
you can get the dev file names from /sys system i hope. /sys is used by a lot of things like hal and other similar programs. you should able to get info if cd is in the drive or not, too.
 
Old 12-11-2009, 12:04 AM   #5
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
may be you could use a combination of dmesg and grep to detect the usb insertion. Because every time a usb(or any other device) is inserted, the complete details of the device and under what is it mounted in /dev is listed in the dmesg.
so if you could grep through the last 10 or 20 lines of dmesg you should be able to get the information.

hope it helps.
 
  


Reply

Tags
bash, scripts


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
Help: Detect if CD-ROM is inserted ohmygoth Programming 3 12-06-2009 02:50 AM
corrupted X gets allright when Photon Plus pendrive inserted! adityavpratap Ubuntu 2 10-01-2009 05:58 AM
multi mem card reader doesnt detect newly inserted cards qwijibow Linux - Hardware 1 08-24-2005 03:51 PM
usb multi card reader doesnt detect newly inserted cards qwijibow Linux - Hardware 1 08-24-2005 03:50 PM
Wierd noise when CDROM is inserted. covana Suse/Novell 1 01-28-2005 09:27 PM


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

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