[NBLUG/talk] Testing for the presence of a disk

Lincoln Peters sampln at sbcglobal.net
Wed Aug 30 09:14:13 PDT 2006


I'm trying to rig an old Pentium I system to work as a photo kiosk,  
so that I can insert a memory card from any digital camera and  
quickly copy the contents of the card to the hard disk.   
Consequently, I need a shell script to be able to determine when a  
disk has been inserted into a drive, and when it has been removed-- 
all without any prior knowledge of the nature of the disk (since not  
all cameras use flash memory).


I'm thinking I could use this block of code to test for the presence  
of a card and mount it once inserted:

# Precondition: a file called "sources" exists and lists the mount  
points of every device we might be looking for.

export dev=NULL
while [ $dev == NULL ]
do
	for d in `cat sources`
	do
		mount $d &> /dev/null && dev=$d
	done
	sleep 2
done


And then I could use the following block of code to test for when the  
card has been removed (here I can assume that it was already unmounted):

# Precondition: all devices listed in "sources" are mounted read-only  
by default, and no new cards are inserted while this code is running.

RETVAL=0
while [ $RETVAL == 0 ]
do
	mount $dev &> /dev/null
	RETVAL=$?
	if [ $RETVAL == 0 ]
	then
		umount $dev
	fi
	sleep 1
done


Both of these solutions seem overly complex for such a simple task,  
and I can't help thinking there must be an easier way.  Anyone know  
of an easier way?


Just to be clear: it is very likely that this shell script will need  
to be able to handle a card being inserted into any of more than a  
few different drives.  While I do not need to handle two or more  
cards simultaneously, I do need to be able to handle any one card, no  
matter which reader it was plugged into (multiple cards can be  
handled in any order, as long as they all get handled).

Also, since this project might be of interest to people besides  
myself, I can post complete source code and documentation when it's  
done.


--
Lincoln Peters
<petersl at sonoma.edu>

It is the quality rather than the quantity that matters.
                 -- Lucius Annaeus Seneca




More information about the talk mailing list