LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2009, 09:05 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Storing USB device name in separate variables dynamically.


hi,

I am trying to write a bash shell script to detect USB devices (hard drives) on my Debian Lenny system. This is how I am trying to detect USB devices:

Code:
#!/bin/bash

echo "Finding USB devices. Please wait..."

find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0|xargs -0 -iD readlink -f D

exit 0
The output of the above "find" commands gives me something like this:
Code:
/dev/sda1
/dev/sda5
/dev/sda2
/dev/sda
/dev/sdb5
/dev/sdb2
/dev/sdb1
/dev/sdb
I would like to achieve the following two things:
1. I just want:
Code:
/dev/sda
/dev/sdb
from the above output to be stored in two variables say:
Code:
USB_DEVICE_1 = /dev/sda
USB_DEVICE_2 = /dev/sdb
2. Also this process should be dynamic. For example, if there are 3 USB devices instead of the two (like above) then my shell script should store something like this:
Code:
USB_DEVICE_1 = /dev/sda
USB_DEVICE_2 = /dev/sdb
USB_DEVICE_3 = /dev/sdc
For just 1 USB device attached to the system:
Code:
USB_DEVICE_1 = /dev/sda
I am assuming some kind of "for" loop will be required but I am not sure how to do that.

Thanks
 
Old 01-29-2009, 01:38 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
array=($(find whatever | grep -v '[0-9]'))
echo ${array[1]}
-> /dev/sdb
 
Old 01-29-2009, 01:05 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by burschik View Post
Code:
array=($(find whatever | grep -v '[0-9]'))
echo ${array[1]}
-> /dev/sdb
I think that works. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: storing all data form a db row in variables with the same name as the db column konqi Programming 2 07-10-2008 05:13 AM
What filesystem for storing photos on separate drive.? ciden Linux - General 12 06-16-2008 01:32 PM
another simple unix scripting question! storing values in variables christianunix Linux - Newbie 4 10-30-2007 01:13 PM
Linux Dynamically Loaded Libraries and Global Variables Millenniumman Programming 3 06-13-2007 11:37 AM
Variables invloved with working camera/usb storage device connection jimdaworm Slackware 2 04-25-2004 11:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration