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? |
Quote:
|
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. |