Detecting A USB Storage Device From A Script

I need a way to reliably detect a USB storage device from a bash script. I know how to use 'lsusb' to list the USB devices - but then how can I match a device listed in 'lsusb' output to an actual disk device? Is there some way to map one to the other?

Any help appreciated.

You have not said what operating system you are using but assuming that you are on Linux you can use hal functionality to do what you want to do. For example, here is what I use to list information about all attached removable usb storage devices:

#!/bin/ksh93

for udi in $(/usr/bin/hal-find-by-capability --capability storage)
do
     device=$(hal-get-property --udi $udi --key block.device)
     vendor=$(hal-get-property --udi $udi --key storage.vendor)
     model=$(hal-get-property --udi $udi --key storage.model)
     if [[ $(hal-get-property --udi $udi --key storage.bus) = "usb" ]]
     then
         parent_udi=$(hal-find-by-property --key block.storage_device --string $udi)
         mount=$(hal-get-property --udi $parent_udi --key volume.mount_point)
         label=$(hal-get-property --udi $parent_udi --key volume.label)
         media_size=$(hal-get-property --udi $udi --key storage.removable.media_size)
         size=$(( ceil(media_size/(1000*1000*1000)) ))
         printf "$vendor  $model  $device  $mount  $label "${size}GB" \n"
    fi
done

This is a ksh93-specific script but it should give you some ideas on how to write your own bash-based script.

Awesome! Thanks, that looks like just the kind of thing I was looking for. Your assumption is right - the OS is Ubuntu Linux 9.10. Is there somewhere I can find more documentation on the HAL stuff?

See:

man hal-find-by-capability

for further info and references