[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Activating a configuration file within a submenu or a menuentry

From: Arbiel Perlacremaz
Subject: Re: Activating a configuration file within a submenu or a menuentry
Date: Mon, 09 Apr 2012 15:20:54 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120310 Thunderbird/11.0

Thank you Jordan

It works perfectly after I have added

set prefix=${2}/boot/grub
export prefix

inside the loop, in front of the configfile command

Le 09/04/2012 06:01, Jordan Uggla a écrit :
On Sun, Apr 8, 2012 at 2:58 PM, Arbiel Perlacremaz
<address@hidden>  wrote:
function other_cfg {
insmod regexp

for device in (*); do
# il ne faut pas inclure dans le menu l\'amorçage actuel, pour éviter de
tourner en rond
# the present grub.cfg file is to be excluded from the menu, as to avoid
looping on it
    if [ "${device}/boot/grub" != "${prefix}" ]; then
       if [ -e ${device}/boot/grub/grub.cfg ]; then
        echo J\'ai trouvé ${device}/boot/grub/grub.cfg;
         if probe --set=label --label "$device"; then
            menuentry "$label" {
                configfile  ${device}/boot/grub/grub.cfg

Here is your problem. The menuentry command does not act like a
closure, but rather more like a normal bash function; All variables in
grub-script are global; And commands in menu entries are by definition
executed after all of the commands in the script which created said
menu (Selecting a menu entry can be thought of as calling that
function, and the menu isn't displayed until your script finishes).
This means that when the menu entry is selected, $device will have the
value from the last iteration of the for loop, not from the iteration
of the for loop in which the menu entry was defined. Below is an
analogous bash script with the same incorrect assumption. If you can
understand why this bash script fails, you should understand why your
grub-script fails:


for word in foo bar baz; do
   if [ "$word" = foo ]; then
     function print_foo {
       echo "$word"

   if [ "$word" = bar ]; then
     function print_bar {
       echo "$word"

   if [ "$word" = baz ]; then
     function print_baz {
       echo "$word"

# Now that the for loop has defined the funtions (much like your for loop
# defined the menu entries) let's try calling these functions.


# You might expect this to print "foo\n bar\n baz"
# but instead it prints "baz\n baz\n baz\n"
# End of example code

Grub menuentry commands can take arguments though, and these arguments
are available via positional parameters ($1 $2 etc). In fact they
always take at least one (which is used as the title of the menu
entry), where they differ from bash functions though is that these
arguments are given at the time the menu entry is defined, rather than
when it's selected or "called". The following example creates a menu
entry where the first positional is the title, "Fancy title", and
whose second positional parameter is the string "foo":

menuentry "Fancy title" foo {
   echo "The title of this menu entry is: $1"
   echo "The second positional parameter is $2"
   # Will print:
   # The title of this menu entry is: Fancy title
   # The second positional parameter is foo

   #sleep for 10 seconds so you can actually see the output
   sleep -v  --interruptible 10

# End example code

and what you probably want is a menu entry like this:

menuentry "Boot $label" "$device"{
   configfile  "${device}/boot/grub/grub.cfg"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]