LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH scripting - detect if cdrom/pendrive inserted (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-detect-if-cdrom-pendrive-inserted-774704/)

siema 12-10-2009 05:40 AM

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

hunternet93 12-10-2009 08:26 AM

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!

siema 12-10-2009 08:38 AM

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?

ozanbaba 12-10-2009 02:00 PM

Quote:

Originally Posted by siema (Post 3786722)
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.

bsat 12-11-2009 12:04 AM

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:29 PM.