LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-25-2004, 04:39 PM   #1
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Rep: Reputation: 33
Toggling "eject" and "eject -t"


I would like to write a shell script that invokes either "eject" or "eject -t" depending upon the state of my DVD drive tray. I want to place a shortcut of the script so I can click on it to open/close the tray. How can I check the state of the tray from within the script?
Code:
#! /bin/sh

if [ <tray_is_closed> ]; then
    eject
else
    eject -t
fi
Thanks!
Dai
 
Old 12-25-2004, 08:07 PM   #2
dizzutch
Member
 
Registered: Sep 2004
Location: Worcester, MA USA
Distribution: Debian, Gentoo, Fedora FC2
Posts: 66

Rep: Reputation: 15
some pseudocode for ya
write an integer to a file that sets the state of the tray
Code:
read open_closed.txt into open
if [ open == 1] 
eject -t 
write 0 to open_closed.txt
else
eject
write 1 to open_closed.txt
 
Old 12-25-2004, 11:14 PM   #3
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
there is a way to get information in c
int test = (ioctl(fdcdrom, CDROM_DRIVE_STATUS));

but it's limited like it won't just open an empty drive
try this and look at linux/cdrom.h for the rest of the possible return values
i don't think you will get much farther than this -- i tried once -- but i might be wrong

Code:
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdlib.h>

int main() {
  
  int test = 0;
  int fdcdrom = open("/dev/cdrom", O_RDONLY);
		
	
  test = (ioctl(fdcdrom, CDROM_DRIVE_STATUS));
// if it's open close it
  if (test == 2) { 
                 ioctl(fdcdrom, CDROMCLOSETRAY);
                 close(fdcdrom);
                 return 0;
 } else {
// i think here it will only eject if there is a disk !
                 
                 ioctl(fdcdrom, CDROMEJECT);
                 close(fdcdrom);
                 return 0;
          }
}
 
Old 12-27-2004, 04:01 AM   #4
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Original Poster
Rep: Reputation: 33
Thanks guys!

I looked at both solutions. It seems like the C program is a better fit for me. Not that there's anything wrong with the first one - it's just that writing the status to a file means I always have to use that script to open/close the tray in order to keep the file content in sync with the actual state of the tray. The C program seems more flexible. I'll try that and see how it goes.

Again, I appreciate both you guys help!

Dai

[EDIT] The C program worked beautifully. The only thing I had to fix was the parameter to the open function. You have to specify O_NONBLOCK in addition to O_RDONLY in order for the ioctl function to work. I'm a happy man!

Last edited by daihard; 12-27-2004 at 04:34 AM.
 
  


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
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 04:07 PM
eject -v says "unable to open /dev/hdc" Simon Bridge Slackware 2 11-05-2005 02:55 AM
"Enhanced" CDs refusing to eject. Tyke Fedora 3 04-09-2005 08:25 AM
JDS "Live CD" eject? Ryptyde JDS 3 11-02-2004 10:15 AM
CD Won't Eject With "eject" Command Dr. Ephemeron Slackware 13 11-13-2003 01:27 AM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration