Sponsored Content
Top Forums Shell Programming and Scripting BASH Script to Detect and List USB Flash Drives Post 302297958 by fpmurphy on Monday 16th of March 2009 09:39:26 AM
Old 03-16-2009
Another way to do what you want is to query the devices using the hal-* utilities which are available on most modern GNU/Linux distributions.
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

Here is the output for two USB thumb drives
Code:
$ ./listusb   
Kingston  DataTraveler 2.0  /dev/sdd  /media/KINGSTON  KINGSTON 1GB 
Kingston  DataTraveler 2.0  /dev/sdc  /media/USB-4GB  USB-4GB 4GB

 

9 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

Mounting USB Drives in Solaris 9 x86

I'm trying to moun my external USB Mass Storage Drive (80GB) in my Solaris 9 box, I am new to Solaris, and kind of new to linux / unix variants. The external HD contains windows files, but I will be using it as a central storage area for my windows/ linux clients. Thanks -- N:confused:C (1 Reply)
Discussion started by: N0C717
1 Replies

2. What is on Your Mind?

USB Thumb Drives

I'm hunting around for thumb drives that aren't working. Something you may have kicking around in a drawer that you don't need any more. One you've upgraded and just don't need. I'm willing to pay a couple bucks for you to drop it into an envelope with a few stamps and send it along. I'm... (0 Replies)
Discussion started by: BOFH
0 Replies

3. Shell Programming and Scripting

how floppy disks, CDs and flash drives (pen drives) are accessed in UNIX

hi how floppy disks, CDs and flash drives (pen drives) are accessed in UNIX? thanks (0 Replies)
Discussion started by: nokia1100
0 Replies

4. Filesystems, Disks and Memory

USB Flash Drives

Can Solaris/any GNU/Linux distros/ any flavor of FreeBSD be booted, right from aUSB flash drive? Mine's the one pictured here. SanDisk | Products | USB Flash Drives | SanDisk CruzerŽ Titanium Plus USB Flash Drive Also, on a completely unrelated note (just so I don't have to fill up forum... (4 Replies)
Discussion started by: led3234
4 Replies

5. Solaris

Does Solaris Volume Manager support USB flash drives?

I would like to mirror or stripe across multiple USB flash drives on a Sun Blade 100 workstation running Solaris 10. Thanks! (6 Replies)
Discussion started by: yoda9999
6 Replies

6. Shell Programming and Scripting

script to detect a file from inserted usb and puts into a Variable

There is a same named log file that I have on my 2 different android phones. When I plug it into my computer, it appears in the media folder, For example the first android phone: /media/F6BA-0AF5/folder/A.log I want to put that into a variable to be manipulated.... (3 Replies)
Discussion started by: tobenguyen
3 Replies

7. What is on Your Mind?

Idea: selling Puppy Linux flash drives

What do you think of the idea of selling flash drives already prepared to boot Puppy Linux on PCs? (I still need to see if I can boot on Macs using online instructions I've found.) I know it's not too hard to prep your own flash drive, even if you have to buy one first, but just think about why... (8 Replies)
Discussion started by: MrMormon
8 Replies

8. Shell Programming and Scripting

Bash script to detect nonpingable hosts

I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far: for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip... (5 Replies)
Discussion started by: newbie2010
5 Replies

9. Solaris

Anyway to do ufsdump os backup to usb drives

hi folks happy new year to all, I haven't been coming round for a long time. I've been googling about this can't seem to find any good examples, I need to know if it's possible to do a ufsdump os backup into a usb flash drive. Then what about in event of os recovery when we boot to single... (2 Replies)
Discussion started by: sparcguy
2 Replies
hal-get-property(1M)					  System Administration Commands				      hal-get-property(1M)

NAME
hal-get-property, hal-set-property - get and set HAL device properties SYNOPSIS
hal-get-property --udi udi --key key [--help] [--verbose] [--version] hal-set-property --udi udi --key key {--int value | --uint64 value | --string value | --bool value | --strlist-pre value | --strlist-post value | --strlist-rem value | --double value | --remove value} [--help] [--version] DESCRIPTION
The Hardware Abstraction Layer (HAL) provides a view of the various hardware attached to a system. This view is updated dynamically as hardware configuration changes by means of hotplug or other mechanisms. HAL represents a piece of hardware as a device object. A device object is identified by a unique identifer and carries a set of key/value pairs, referred to as device properties. Some properties are derived from the actual hardware, some are merged from device information files (.fdi files), and some are related to the actual device configuration. The hal-get-property and hal-set-property commands allow you to get and set properties of hardware that conforms to HAL specifications. OPTIONS
The following options are supported: --udi udi Unique device ID. --key key Key of the property to set. --int Set value to an integer. Accepts decimal or hexadecimal value prefixed with 0x or x. --uint64 Set value to an integer. Accepts decimal or hexadecimal value prefixed with 0x or x. --string value Set value to a string. --double value Set value to a floating point number. --boolean value Set value to a boolean, that is, true or false --strlist-pre value Prepend a string to a list. --strlist-post value Append a string to a list. --strlist-rem value Remove a string from a list. --remove value Indicates that the property should be removed. --version Display version and exit. --help Display list of options and exit ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWhalr | +-----------------------------+-----------------------------+ |Interface Stability |Volatile | +-----------------------------+-----------------------------+ SEE ALSO
hald(1M), attributes(5), hal(5) SunOS 5.11 18 Aug 2006 hal-get-property(1M)
All times are GMT -4. The time now is 09:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy