LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-15-2005, 11:52 AM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
shell script to detect IDE devices


Hi folks,

Actually I am writing a shell script on Debian Linux which will do several tasks like copying, extracting, installing grub and all that.

One of the task that I have to do first is to recognize the IDE devices that are attach to the system.

Does any one know how can I write a simple shell script which will recognize what devices are connected to hda,hdb,hdc,hdd. I just need to probe for the IDE devices(Hard disk,CD-ROM,) that are attach to the system. Or is there any file/log in the system which maintains what/where IDEdevices are connected(hda/hdb/hdc/hdd).

thanks
 
Old 09-15-2005, 12:27 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Look at
Code:
dmesg | grep '^hd'
 
Old 09-15-2005, 12:54 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally posted by Matir
Look at
Code:
dmesg | grep '^hd'
Thanks what does symbol '^' do? Actually I want to store the letter hda/hdb/hdc/hdd into one of my variable in the script. Suppose I out the result of dmesg | grep '^hd' into a file say temp.txt, how do I extract the letter hda/hdb/.... into a variable in my script install.sh

thanks
 
Old 09-15-2005, 01:00 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
The letter '^' indicates that the pattern MUST be at the beginning of a line. If you would like a list of all hd devices on the system, here's a code snippet you might like.

Code:
dmesg | egrep '^hd.?:' | cut -d':' -f1 | sort | uniq
Simply, this gets the kernel ringbuffer, looks for lines beginning with 'hd<someletter>:', strips everything after and including the colon, sorts it, and removes duplicates.

Note that this will only work if the kernel ringbuffer has not yet been filled to the point that those lines have been removed.

Alternatively, and I feel stupid for forgetting this, you could try:
Code:
ls -1d /proc/ide/hd?
That may be much simpler, but I still choose to share my nice one-liner above.
 
Old 09-15-2005, 01:13 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
[QUOTE]Originally posted by Matir
[B]The letter '^' indicates that the pattern MUST be at the beginning of a line. If you would like a list of all hd devices on the system, here's a code snippet you might like.

Code:
dmesg | egrep '^hd.?:' | cut -d':' -f1 | sort | uniq
Simply, this gets the kernel ringbuffer, looks for lines beginning with 'hd<someletter>:', strips everything after and including the colon, sorts it, and removes duplicates.

Excellent!!! I was looking exactly something like this. You seem to be an expert in this. Well my second phase of the problem is how to read the out of your one-line command into some Variables in my script. Suppose I get the output from the command as:
hda
hdb

How do I store each of them in variable like IDE_1 and IDE_2 in my shell script.

Many thanks to you.
 
Old 09-15-2005, 01:30 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Code:
N=1
FILE=`mktemp`
ls -1d /proc/ide/hd? | while read dev
    do echo IDE_${N}=`basename $dev` >> $FILE
    N=$(($N+1))
done
source $FILE
rm $FILE
The FILE bit is a bit hackish, unfortunately, but it's pretty much the best way.
 
Old 09-15-2005, 02:15 PM   #7
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Thanks a bunch Matir. You are the man.....

Can you give me some pointers to resources where I can learn basic and advanced shell scripting.......

Thank you once again.
 
Old 09-15-2005, 02:19 PM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
The best way to learn shell scripting is to do it. That being said, you need some references to get you going. My favorite is the Advanced Bash-Scripting Guide. Don't let the word Advanced scare you off, it's quite good. To quote from it:
Quote:
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little snippets of UNIX® wisdom and lore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts.
 
Old 09-28-2005, 11:15 AM   #9
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally posted by Matir
Code:
N=1
FILE=`mktemp`
ls -1d /proc/ide/hd? | while read dev
    do echo IDE_${N}=`basename $dev` >> $FILE
    N=$(($N+1))
done
source $FILE
rm $FILE
The FILE bit is a bit hackish, unfortunately, but it's pretty much the best way.
hi Matir, I need your help again. The above code gives me the devices hda hdb hdc and so on in variables IDE_1, IDE_2, IDE_3 and so on.

Now I would like to have a complete information of the devices like this in three variables or more depending upon the number of devices in my system:
hda: WDC WD800BB-22JHC0, ATA DISK drive
hdb: HITACHI CDR-8130, ATAPI CD/DVD-ROM drive
hdc: SILICONSYSTEMS INC 1GB, ATA DISK drive

I have copy pasted the above 3 lines from the output of this command(which you gave me earlier)
dmesg | egrep '^hd.?:'
The entire output of this command was:
debian:~/Scripts# dmesg | egrep '^hd.?:'
hda: WDC WD800BB-22JHC0, ATA DISK drive
hdb: HITACHI CDR-8130, ATAPI CD/DVD-ROM drive
hda: max request size: 128KiB
hda: 156301488 sectors (80026 MB) w/2048KiB Cache, CHS=65535/16/63, UDMA(33)
hdc: SILICONSYSTEMS INC 1GB, ATA DISK drive
hdc: max request size: 128KiB
hdc: 2046240 sectors (1047 MB) w/0KiB Cache, CHS=2030/16/63, DMA
hdb: ATAPI 16X CD-ROM drive, 128kB Cache, DMA

Any help will be greatly appreciated.

Last edited by kushalkoolwal; 09-28-2005 at 11:17 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Shell Script Ide sharadgana Programming 5 12-21-2004 10:55 AM
Shell Script Ide sharadgana Linux - Software 2 12-21-2004 10:44 AM
How to use shell script to get removable devices? slownet Programming 2 03-23-2004 10:16 AM
Problem with RH 7.1 (doesn't detect IDE devices) jcontess Linux - Software 3 06-14-2001 12:43 PM


All times are GMT -5. The time now is 11:11 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration