Avatar of skullnobrains
skullnobrains asked on

grub2 scripting. looking for complete documentation in order to sort items automatically

hello all

i'm trying to do various stuff in grub2 shell but i have a hard time finding proper documentation.


anybody knows where to find a proper exhausive documentation containing namely the supported syntaxes for commands such as regexp and test ? ( which are not available in any of the pages i visited at gnu.org and namely not in http://www.gnu.org/software/grub/manual/html_node/Shell_002dlike-scripting.html )


i have a script that detects available kernels and provides a boot entry for each of them with the corresponding initrd.

i have a hard time to sort the kernel by reverse version order :
- i have no idea if a sort command is available and anyway it does not seem like pipes are supported so it probably would be useless
- i tried various shell syntaxes for the test command but i'm only able to sort numerically using "test NUMBER -gt NUMBER" which work for decimal numbers but will not allow version comparison (i'd need to compare stuff such as 3.8.1-11)
- i have no idea what kind of string manipulation functions are available other than "regexp" and i only know the basic usage since i have yet to find where it is documented
- there migh be a way to list files matching a pattern and output them sorted somehow but i have not found it yet

obviously i can parse the major, minor, sub-minor and whatever you would call the last number, store them into as many variables and use a bunch of if-else comparisons to reach this goal

but it is a major pita and it is not generic enough since it will not allow to compare versions such as 11a with 11b (quite useless for linux kernels but i'd like it more generic)

i'd also be interested in a sort based on mtime or ctime for filesystems that handle it but i have no idea if/how that would be supported in grub shell

i am NOT interested by solutions that involve using a shell script to write grub.cfg : this is both trivial and not what i'm looking for


thanks for any hints you can provide
Operating SystemsInstallationShell Scripting

Avatar of undefined
Last Comment

8/22/2022 - Mon
Duncan Roe

The most exhaustive documentation for Grub is grub.info, which you access by typing info grub on your keyboard.
You will not be pleased to hear that a search for regexp found only the following
20 Regexp

Regexps work on unicode characters, however no attempt at checking
cannonical equivalence has been made. Moreover the classes like
[:alpha:] match only ASCII subset.
Unfortunately, this means that Gnu have not seen fit to document grub regexp further.

unfortunately, I already looked at most info pages to no avail, but thanks nevertheless

currently i'm using regexp like this
regexp -s varname pattern subject

Open in new window

i found out this undocumented syntax in somebody else's code by accident
when the pattern captures something, it gets stored into the specified variable
i still did not figure out how to capture several strings at the same time (if feasible)
and i yet have to try various syntaxes in order to do a replacement

any ideas or links still welcome
Gary Patterson, CISSP

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

pretty useful yeah : though i actually had figured out those syntaxes empirically, i had yet to realize the -s option could be repeated, and no other options were available

as far as regexp is concerned, this is what i understood so far
- regexp will return true if the regexp matches, false otherwise with or without -s switch
- regular expressions are not "docked" (there is no implicit ^ or $ at the beginning/end)
- no modificators are available (unsure about that one)
- "-s VARNAME" will store whatever is captured by parenthesis in VARNAME
- nested parenthesis work as expected
- using -s without parenthesis is an error (regexp returns false even if there is a match)
- when regexp does not match, VARNAME is left unchanged
- variables can be assigned to a specific captured string using "-s NUM:VARNAME"
- "-s" can be repeated
- "-s" is the only available option (not sure about than one either)
i have not needed or tried anything like (?:...), backreferences or the likes. apparently the engine is using extended-regexp-like syntax

as far as test is concerned, there seem to be a whole wealth of available options including -nt and -ot (newer/older than)... which are unfortunately unavailable in the version i've been playing with (2.00-19ubunt). i yet have to check if this is a google-hacked version or if newer versions feature the same.

it also seems easier to patch the source code rather than actually implement version comparison in the grub shell. unfortunately, i'm unsure I can do that given my requirements but i can definitely impose a minimal grub version

thanks a lot for your help so far. any other thoughts are more than welcome. i'll keep this thread updated
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Gary Patterson, CISSP

You've done a lot of work.  Maybe you can update the Grub2 documentation with it so others don't have to go through the same thing.

Looking at the regexp.c source code link above, the call to regcomp (Compile Regular Expression) specifies REG_EXTENDED.  Assuming the version you are using does the same, then the POSIX standard should apply (any exceptions are supposed to be explicitly documented in utilities that make exceptions to the standard):


I don't think I have a lot more to contribute here.  This is a little out of my area of expertise, but I thought I'd chip in what I could.  It has been interesting, and I learned a little about grub myself.

- Gary Patterson

gary, you were actually very helpful in making me realize that

- although i was testing with 2.00_19ubunt..., the version this is based on is actually most likely somewhere along the 1.98.x or 1.99.x versions, (and the version numbering is so messed up i'm not even sure about that)

- looking at the source myself was probably much better use of my time than empirically toying with various options or other dumb reverse engineering techniques.

i'll accept your answer at some point, but keep this thread open until i have time to work on this again and post some useful code. i spent quite a long time expecting grub2 to compile on ubuntu and ended up compiling it from ports in a freebsd vm... which took quite some time and i had to switch to more urgent matters ( like Xmas ;)

feel free to post any relevant thoughts in the meantime

as far as the test command is concerned :

i was wrong, there is some kind of unusable explanation in the "other" section of the documentation

version comparisons using '-plt' and '-pgt' work pretty much as expected for number-based version comparison. it cannot be fooled too easily by different prefix or wrong separators. strange comparisons produce a negative answer. (aka comparing weird stuff such as "bar-1" with "baz-1" will produce a negative answer both ways. same applies to "11a" and "11b")

newer and older than work using -ot and -lt. i have not digged into bias yet. if either file does not exist, the comparison returns false

lexicographical comparison work for ascii in what seems to be a reliable way. the comparison is case insensitive says the manual. i have not checked. some comparators starting with < or > need to be double-quoted. quoting other comparators produces an error

-gt -eq -le and the likes apparently work as expected

i have not checked these yet, but -z and -n should work as expected

-f -d -e should be ok. i only used -f for now

-a -o and parenthesis seem to be implemented somehow, but i never tried using them. parenthesis are likely to require quoting

the understanding of how the shell actually works, of variable scopes, and how to use functions is out of the scope of this question, but i'm currently trying to figure those out
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

best answer