LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-22-2007, 12:57 AM   #1
ElectroLinux
Member
 
Registered: Feb 2004
Posts: 74

Rep: Reputation: 15
Question parsing for hda or sda in limited shell


i'd like to parse the ouput of
sfdisk -s

This would give me something like:
/dev/sda: 10485760
total: 10485760

Since I'm pxe booting a limited shell, i'll have grep and mawk at my disposal. I'm not sure what else I would need since I don't know what is available in this shell until i try it.

I basically want to take the "sda" out of the line "/dev/sda: 10485760" and assign "sda" to a variable that I can use throughout the /bin/sh program.


We'll assume we only have one type of drive per system which means it will be either sda, hda, cciss, etc....

How can I do this?


Some background info: I'm trying to customize a systemimager script to recognize the drive automatically...so whether it is scsi, hp scsi device, sata, pata...i want to be able to automaticaly recognize what device to use in order to create the partitions, fstab file, and boot loader config file in the script. This comes in hand when having 1 OS being installed onto different hardware.
 
Old 05-22-2007, 09:27 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,043
Blog Entries: 14

Rep: Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212
Haven't used mawk - below uses awk and may work with mawk:

sfdisk -s |grep dev |awk -F: '{print $1}' |awk -F/ '{print $3}'

Note: If you have more than one drive you'll get more than one line in the output.

If you have cut available you can simplify it a bit:

sfdisk -s |grep dev |cut -c6-8
 
Old 05-22-2007, 11:53 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Ironically, because there is no uniform field delimiter, awk is not as useful as it usually is. Here are some ideas using grep & sed:
  1. If your grep (as it should) has the "-o" option, try:
    Code:
     sfdisk -s  | grep -o '[^/]d[^:]'
    Here, I'm assuming the value you want is always 3 letters & has a 'd' in the middle.


  2. If sed is available, try:
    Code:
    sfdisk -s  | sed '2,$d;s,^.*/,,;s,:.*$,,'
    Although this one is longer, it is also more precise -- deleting everything before & after the part you want.



  3. If, for some weird reason, you have grep but not sed, then:
    Code:
    sfdisk -s  | grep -o '/[^:]*'  | grep -o '[^/]*$'
    Like the sed example, it deletes everything before & after the part you want.


  4. Finally, example 1 can be extended:
    Code:
     sfdisk -s  | grep -o '[^/]+d[^:]+'
    This still assumes that there is a 'd' in the middle of the value you want, but not that it is limited to only 3 letters.
 
Old 05-22-2007, 12:34 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
var=$(sfdisk -s | awk -F "[ :/]" '/^\/dev/{print $3}')
echo $var
output:
Code:
# ./test.sh
sda sdb
 
Old 05-22-2007, 01:44 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,043
Blog Entries: 14

Rep: Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212
! mawk me
 
Old 05-23-2007, 10:18 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Wow! HTF did you figure out the -F "[ :/]" part? -- I re-read the gawk info page regularly & everything I have seen there strongly implies that -F takes a 1 character value. Of course the fact that the IFS environment variable is multi-character might be a hint, but everything overt in that <expletives_deleted /> info page says the opposite. AFAIAC, this is a bug in the documentation or else the authors aren't aware of this capability.

BTW, this works in mawk, which is a separate program on my MEPIS box, & in awk in "traditional" mode:
Code:
$ V=$(sfdisk -s | awk --compat -F "[ :/]" '/dev/{print $3}')
$ echo $V; echo "$V"
hda hdb hde hdg
hda
hdb
hde
hdg
So, this should work on any older ver. of awk.


You now have multiple good answers of varying length & precision, your choice will be according to what will work in the variety of environments you expect your script to encounter; and, to a lesser extent, perhaps to aesthetics.


In my mind, the major unknown is the limits on sfdisk output. -- Are all /dev/'s 3 characters w/ a 'd' in the middle? If this is so, always so, it radically simplifies the design of the parsing regex. If it is true today, will it stay that way?

W/o reading the source, the best indicator may be:
Code:
$ strings `which sfdisk` |grep -i dev |less -N
         ... (excerpt from less' output)
      9 device: something like /dev/hda or /dev/sda
 
  


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
Why sda* and not hda* ? hepburnenthorpe Arch 9 06-27-2007 05:18 PM
Grub: works on hda, but not sda mattd7591 Linux - General 1 04-01-2007 04:38 PM
/hda to /sda - help! dodgydodgy Linux - General 9 10-20-2006 07:07 PM
Do I use hda or sda? jumico Linux - General 2 02-12-2006 06:55 PM
hda? sda? what do i have? nr5 Linux - Newbie 12 02-24-2005 07:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration