BASH scripting - detect if cdrom/pendrive inserted
Linux - NewbieThis 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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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
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!
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.