LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Grab results of GRUB's find command into a shell script variable (https://www.linuxquestions.org/questions/programming-9/grab-results-of-grubs-find-command-into-a-shell-script-variable-701722/)

kushalkoolwal 02-02-2009 03:08 PM

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

burschik 02-03-2009 04:54 AM

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

Code:

var=$(grub whatever)

kushalkoolwal 02-03-2009 05:05 AM

Quote:

Originally Posted by burschik (Post 3430077)
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

ntubski 02-03-2009 11:38 AM

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]*')

kushalkoolwal 02-03-2009 01:51 PM

Quote:

Originally Posted by ntubski (Post 3430573)
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!

syg00 02-03-2009 04:42 PM

Be prepared for the possibility that your variable may have more than one value in it when you go to use it.

kushalkoolwal 02-03-2009 05:04 PM

Quote:

Originally Posted by syg00 (Post 3430958)
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.:)


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