Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know my CD-ROM device (/dev/sr0) but how can I detect from a script whether the drive is empty or whether there is a disk in it?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can get information about any block device using the command blkid.

[root@arch32-vm ~]# blkid /dev/sr0
/dev/sr0: UUID="2013-05-31-23-04-19-00" LABEL="ARCH_201306" TYPE="iso9660" PTTYPE="dos"
[root@arch32-vm ~]# echo $?
0

If I remove the disk, I don't get any output and exit value is 2. (0 means success. A non-zero value will typically mean something abnormal happen or an error occurred)

[root@arch32-vm ~]# blkid /dev/sr0
[root@arch32-vm ~]# echo $?
2
share|improve this answer
    
blkid detects partitions, If i put a audio CD, it is not detected. Didn't find a easier solution than create a program using ioctl as described in stackoverflow.com/questions/15652520/… –  BatchyX May 17 at 19:55

Try mounting the device.

mount -t iso9660 /dev/sr0 /mnt/cdrom

Then check the return value $?

If the return is 0, "good" then there was disc present. Else, it will return not good "1" or anything but "0"

So to check silently, I would script it as so.

cdrom_mount=0
mount -t iso9660 /dev/sr0 /mnt/cdrom >/dev/null 2>&1
if [[ $? -gt 0 ]]
    then
    cdrom_mount=true
else
    cdrom_mount=false
fi

This is a very simplistic example, but you could do something similar...

share|improve this answer
    
Isn't that -gt should be -eq ? –  SHW Feb 11 at 6:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.