Detecting A USB Storage Device From A Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Detecting A USB Storage Device From A Script
# 1  
Old 01-18-2010
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.
# 2  
Old 01-18-2010
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:
Code:
#!/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.
# 3  
Old 01-19-2010
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?
# 4  
Old 01-19-2010
See:
Code:
man hal-find-by-capability

for further info and references
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Mounting USB Mass Storage

Hi experts, recently i'm exploring USB with filesystem FAT32 mounting on my aix oslevel 6100-04-02-1007. I tried to google to get solutions but failed. Thus, i post it here hope to get solution. Appreciate :) This is my usb drives: (5 Replies)
Discussion started by: polar
5 Replies

2. Shell Programming and Scripting

Auto run script on USB device

Okay I want to write a script that I put on a jump drive. And when you plug the USB in this script executes. How would i go about doing this? I don't care what the program does right now. I just want to know how this would be done. Any ideas? (1 Reply)
Discussion started by: cbreiny
1 Replies

3. Solaris

Detecting wirelss USB Adapter

Hi, i try to plug in my USB wireless adapter to my sun solaris server. However, it cannot detect it. How do i set it so that it can detect it? (1 Reply)
Discussion started by: lkyow
1 Replies

4. BSD

Detecting usb stick in freebsd

I inserted a 8GB usb stick in a number of machine with FreeBSD 7.1, but the medium was not detected: $ dmesg | grep MB usable memory = 4263022592 (4065 MB) avail memory = 4082540544 (3893 MB) pci0: <serial bus, SMBus> at device 31.3 (no driver attached) ad0: 238475MB <WDC WD2500BEVT-00ZCT0... (6 Replies)
Discussion started by: figaro
6 Replies

5. UNIX for Dummies Questions & Answers

Device Probe freezes after detecting Ethernet address while installing FreeeBSD 7.0

I'm trying to install FreeBSD 7.0 from CDs I made a few months ago on a brand new system with parts I just got in from NewEgg. I select the default option from the Boot Loader and it goes into the device probe. Several devices are recognized, but then it freezes at: mskc0: <Marvell Yukon... (2 Replies)
Discussion started by: ideogon
2 Replies

6. UNIX for Advanced & Expert Users

How to enumerate USB Mass Storage devices?

Hi all, I want to write a program in C that can enumerate all USB massand their mount point storage on my system. i want to give ability to copy one file to desired USB mass storage or read a file from it. I have posted another question about how can recieve USB arrival in this forum. I think... (0 Replies)
Discussion started by: aghashahi
0 Replies

7. Solaris

Usb Device

AM TRYING TO CONNECT A USB EXTERNAL DRIVE FOR BACKUP,THE USB SLOT IS AVAILABLE,BUT I DONT KNOW IF IT IS READY WHEN I CONNECT IT,AND WHAT COMMANDS DO I NEED. tHANKS (10 Replies)
Discussion started by: tomjones
10 Replies

8. UNIX for Dummies Questions & Answers

path_to_inst output for usb storage device

Hello, I have a sun blade 100 with solaris 10 and am perplexed by the instances of devices that I see when I attach a usb flash stick to one of the ports in the back of the chassis. Here is what I see for USB storage in /etc/path_to_inst: "/pci@1f,0/usb@c,3/storage@3" 0 "scsa2usb"... (0 Replies)
Discussion started by: montana77
0 Replies

9. Linux

Some problem about usb mass storage device

Dear linuxers, I have a usb mess storage device. My OS is rh as3 update2. Each time I use the command mount -t vfat /dev/sda1 /mnt/usb I got the error "the device is not a valid block device". I found from google that I should install the module sd_mod I use the command insmod sd_mod... (2 Replies)
Discussion started by: niukun
2 Replies
Login or Register to Ask a Question