LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-20-2009, 06:59 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Detect a CD-ROM device in a bash shell script


My system has an IDE/PATA CD-ROM device that is detected as /dev/sdb right now.

I am trying to write a bash shell script in which I have to detect and get the device name of the CD-ROM that is attached to the system.

For example, let's say I have a variable $CDROM_DEVICE and I would like to have the final value in it as:

Code:
/dev/sdb
Note that since the Linux kernel is using libata driver, my CD-ROM can be /dev/sda also if I don't attach a primary master drive.
 
Old 08-21-2009, 03:27 AM   #2
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Depends on the kerenl version, but later kernels have the cdrom/dvd under /proc/sys/dev/cdrom, this may not be the case in all kernels, might need to have a look in your /proc directory.


If you cat /proc/sys/dev/cdrom/info, you will get some thing like this:

Quote:
drive name: hdd
drive speed: 48
drive # of slots: 1
Can close tray: 1
Can open tray: 1
Can lock tray: 1
Can change speed: 1
Can select disk: 0
Can read multisession: 1
Can read MCN: 1
Reports media changed: 1
Can play audio: 1
Can write CD-R: 1
Can write CD-RW: 1
Can read DVD: 1
Can write DVD-R: 1
Can write DVD-RAM: 1
Can read MRW: 1
Can write MRW: 1
Can write RAM: 1
You could then use something like this:
Quote:
cat /proc/sys/dev/cdrom/info | grep "drive name:" | awk '{print $3}'
Then you can add it to a variable of your choice

Quote:
CDROM_DEVICE=/dev/`cat /proc/sys/dev/cdrom/info | grep "drive name:" | awk '{print $3}'`

Is this what you are after
 
Old 08-21-2009, 12:04 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
That will work. Thanks!
 
Old 08-21-2009, 05:41 PM   #4
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Glad to help
 
  


Reply



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
Detect if a network device is connected in a bash script jordanwb Linux - General 11 12-29-2008 11:55 AM
Lockup of CD Rom/DVD Rom Device by shell process jiju.jacob Linux - Newbie 2 07-19-2008 02:54 AM
Bash script to detect hardware jimigoon Programming 4 06-30-2007 09:22 AM
What is the best way to detect a hard drive in a shell script? Mahonri Programming 1 06-14-2007 01:14 AM
detect shell script language lorebett Programming 10 09-02-2006 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration