Jordan, thanks for the help
Unfortunately, the "insmod regexp" line is not working for me, at least not from the configuration file. This my grub.cfg:
search.fs_uuid 1b3fdfe0-99cb-4aba-9acd-695b84be63cc btrfs hd0,gpt3
set prefix=($btrfs)'/os/gubuntu-15.10/boot/grub'
configfile $prefix/grub.cfg
(hd0,gpt3) is a btrfs partition with multiple subvolumes under the "os" directory, each containing a full linux distro(with its own grub config) that I can boot to. In the above snippet, ubuntu gnome 15.10 is hardcoded, but I'd like to make this a bit more dynamic with something similar to the for/discover loop you suggested.
To test if the loop/discovery works in grub interactive shell, I did the following:
search.fs_uuid 1b3fdfe0-99cb-4aba-9acd-695b84be63cc btrfs hd0,gpt3
insmod regexp
for os_path in ($btrfs)/os/*; do
echo $os_path
regexp --set=os_name '^\([^)]*\)/os/(.*)$' $os_path
done
Notice that I removed the configfile line to enter the interactive shell. I was expecting to see a list of distro names printed but nothing happens. Then I did: `echo $os_path` which printed `(hd0,gpt3)/os/*`(the glob didn't expand even with the `insmod regexp` module).
Normally I would assume that the regexp module wasn't available, but after typing `insmod regexp` in the interactive shell and running `echo $os_path` again I saw the expansion. What am I missing here?
On Thu, Jan 28, 2016 at 5:56 PM, Thiago Padilha <address@hidden> wrote:
> I want to execute one "menuentry" command for each file in a directory.
> What's the best way to do it? In bash I would use something like `for file
> in $(ls); do`, but can't seem to find a way to use grub's `for` command.
You shouldn't try to parse the output of ls, even in bash:
http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29
For both grub and bash, to iterate over files you should use a for
loop with globs. The difference with grub being that you need to first
"insmod regexp" to enable globbing, for example:
insmod regexp # This is grub specific
# The following lines work exactly the same in bash and grub
for file in /boot-isos/*.iso; do
echo found iso file "$file"
done
For a more complete real world example, the following grub script will
search for all files, on all devices/partitions, in a directory named
"/boot-isos/" and ending in ".iso" and will assume that they have a
loopback.cfg ( http://www.supergrubdisk.org/wiki/Loopback.cfg ). For
each iso found it will create an appropriate menuentry for booting the
iso using the loopback.cfg found within that iso:
insmod regexp
insmod all_video
for iso_with_device in (*)/boot-isos/*.iso; do
# Remove the grub device name to get just the path to the iso
relative to its root.
regexp --set=iso_path '^\(.*\)(.*$)' "$iso_with_device"
menuentry "loop boot $iso_with_device" "$iso_path" {
iso_path="$2"
export iso_path
search --set=root --file "$iso_path"
loopback loop "$iso_path"
root=(loop)
configfile /boot/grub/loopback.cfg
loopback --delete loop
}
done
--
Jordan Uggla (Jordan_U on irc.freenode.net)