2

It was my understanding that grub supports a small subset of bash. Their documentation doesn't go into super detail, other than it "supports conditionals", etc.

I am trying to run a simple if.

grub> if [ "${myvar}" = "fred" ]; then
> echo "test"
> fi
error: can't find command `['.

Anybody have an idea? I am using grub2-efi 2.00.

Paul Knopf
  • 9,568
  • 23
  • 77
  • 142
  • Does `test` work? (`[` is a--poorly thought-out, IMO--alias for `test`). – chepner Aug 27 '17 at 16:39
  • ```test``` command doesn't exist. Maybe that's the problem? Missing module? – Paul Knopf Aug 27 '17 at 16:41
  • Ah, I see ```test.c``` registers a command for ```[```. That is probably the issue. – Paul Knopf Aug 27 '17 at 16:42
  • Having never used `grub` and just perusing the manual, is it possible you are in rescue mode (where it appears very few commands are available). – chepner Aug 27 '17 at 16:45
  • Usually, `[` is a bash built-in command. In this case, it does not look like this. – Cyrus Aug 27 '17 at 16:53
  • @Cyrus `test` and `[` are normally available as executable programs, though they can also be implemented as shell builtins. – Kenster Aug 27 '17 at 17:35
  • This doc https://www.gnu.org/software/grub/manual/html_node/Shell_002dlike-scripting.html states that `[[` and `]]` are reserved words, so try those instead. – cdarke Aug 27 '17 at 19:52
  • I'm running grub2 on powerpc and getting this error too. I wonder if it's a missing grub module that is preventing additional bash functionality (default is `sh`?) – NuclearPeon Aug 30 '22 at 17:46

1 Answers1

0

You are missing a grub2 module in order to run if tests.

I'm running Gentoo on a PowerPC system (PPC64 G5 machine) and doing a default grub-mkconfig then booting from it gives me the error in your question.

Since bash has that syntax support, I figured it was simply a grub module that needed to be added (I had been doing work with grub modules recently). tl;dr: You need to load the appropriate grub module and then the error goes away.

The first step is to find out what modules you have. For me, it's whatever is available in my /boot/grub/powerpc-ieee1275/ folder. There's also modules in /usr/lib/grub/powerpc-ieee1275/.

I wrote up a list of modules I thought I needed:

normal
eval
read
test
test_blockarg
trig
true

I then added them to my /etc/default/grub file:

GRUB_PRELOAD_MODULES="normal eval read test test_blockarg trig true"

I did not find an entry for GRUB_PRELOAD_MODULES in the config file, so I had to do some searching to find out how. I want these modules to be added every time I generate the grub config file, which means putting them in the 00_header portion of grub.

Then I recreated the configuration file:

grub-mkconfig -o /boot/grub/grub.cfg

The modules were in the header and things worked perfectly on reboot.

If I had to guess: you probably only need the test module to enable if statements.

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52