LinuxQuestions.org
Register a domain and help support LQ
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, 04:08 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
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 07:02 PM.
 
Old 02-03-2009, 05: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, 06:05 AM   #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 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, 12:38 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
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, 02:51 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 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, 05:42 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,491

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
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, 06:04 PM   #7
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 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


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


All times are GMT -5. The time now is 06:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration