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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-10-2009, 06:40 AM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 2
Rep:
|
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
|
|
|
12-10-2009, 09:26 AM
|
#2
|
Member
Registered: Jun 2009
Posts: 87
Rep:
|
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!
|
|
|
12-10-2009, 09:38 AM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 2
Original Poster
Rep:
|
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?
|
|
|
12-10-2009, 03:00 PM
|
#4
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
Quote:
Originally Posted by siema
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.
|
|
|
12-11-2009, 01:04 AM
|
#5
|
Member
Registered: Feb 2009
Posts: 347
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 09:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|