LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script to detect IDE devices (https://www.linuxquestions.org/questions/programming-9/shell-script-to-detect-ide-devices-363641/)

kushalkoolwal 09-15-2005 11:52 AM

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

Matir 09-15-2005 12:27 PM

Look at
Code:

dmesg | grep '^hd'

kushalkoolwal 09-15-2005 12:54 PM

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

Matir 09-15-2005 01:00 PM

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. :)

kushalkoolwal 09-15-2005 01:13 PM

[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.

Matir 09-15-2005 01:30 PM

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. :)

kushalkoolwal 09-15-2005 02:15 PM

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.

Matir 09-15-2005 02:19 PM

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.

kushalkoolwal 09-28-2005 11:15 AM

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.


All times are GMT -5. The time now is 09:38 AM.