LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-02-2009, 03:08 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Grab results of GRUB's find command into a shell script variable


hi,

I am running the following command from my bash shell script:
Code:
#!/bin/bash

/usr/sbin/grub --no-floppy --batch << EOF
find /sbin/init
quit
EOF
Now I know that (manually doing this) GRUB's find command returns the following on bash prompt:
Code:
debian:~#grub --no-floppy
grub> find /sbin/init
 (hd0,8)
grub>
All I want to do is grab the drive letter ("hd0" or whatever grub returns) into a variable in my shell script.

Thanks

Last edited by kushalkoolwal; 02-02-2009 at 06:02 PM.
 
Old 02-03-2009, 04:54 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
To capture the output of a program, just use backticks (or an equivalent syntax), like this:

Code:
var=$(grub whatever)
 
Old 02-03-2009, 05:05 AM   #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
To capture the output of a program, just use backticks (or an equivalent syntax), like this:

Code:
var=$(grub whatever)
Thanks for your reply. The thing is that I am trying to run GRUB as a a batch program (--batch) rather than simply a command from my bash shell script. So I am not 100% sure how can I use what you suggested in this case.

Thanks
 
Old 02-03-2009, 11:38 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think this should work:
Code:
#!/bin/bash

var=$(/usr/sbin/grub --no-floppy --batch << EOF
find /sbin/init
quit
EOF
)
I don't believe the quit command is necessary though, grub will exit when there is no more input. To get just the "hd0":
Code:
var=$(echo find /sbin/init | /usr/sbin/grub --no-floppy --batch | grep -o 'hd[0-9]*')
 
Old 02-03-2009, 01:51 PM   #5
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 ntubski View Post
Code:
var=$(echo find /sbin/init | /usr/sbin/grub --no-floppy --batch | grep -o 'hd[0-9]*')
Hi ntubski,

The code worked just fine. Thanks!
 
Old 02-03-2009, 04:42 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Be prepared for the possibility that your variable may have more than one value in it when you go to use it.
 
Old 02-03-2009, 05:04 PM   #7
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 syg00 View Post
Be prepared for the possibility that your variable may have more than one value in it when you go to use it.
Thanks for the heads up. I think I have taken care of that.
 
  


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
Shell Script format question - output command results in quotes Barefootpanda Linux - General 5 10-13-2008 11:40 PM
Shell script: How to include a variable between apostrophes within a command guarriman Programming 3 02-23-2007 03:12 AM
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 02:14 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell command to grab filetypes recursively Nice-n-Slow Programming 1 05-13-2005 07:05 AM

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

All times are GMT -5. The time now is 04:59 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