[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to enumerate files in a directory using the builtin shell?
From: |
Jordan Uggla |
Subject: |
Re: How to enumerate files in a directory using the builtin shell? |
Date: |
Thu, 28 Jan 2016 21:54:09 -0800 |
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)