Conditions

Conditions in GRUB scripting language is almost the same as POSIX shells. It has if else condition statements and it is usable with while and until loop.

If else statements

GRUB scripting languages has such feature. Example:

if [ condition ]; then
        ... # do something
elif [ condition ]; then
        ... # do something
elif [ condition ]; then
        ... # do something
else
        ... # do something        
fi

Conditions

Conditions are complying to GRUB's test command. They are as follows:


Filesystem Flags

Since everything in Unix is a file, there is definitely a set of filesystem flags. You can use these flags to check for file, directory, block device, character device, missing file, status, permission, group, etc.

  • [ -d "/path/to/directory" ] - check /path/to/directory is a directory. False is no.
  • [ -e "/path/to/object" ] - check /path/to/object exists. False is no.
  • [ -f "/path/to/file" ] - check /path/to/file is a file. False is no.


File Flags

  • [ -s "/path/to/file" ] - check /path/to/file contain byte data. False is 0 byte or missing file.
  • [ -r "/path/to/file" ] - check /path/to/file has read permission. False means no read permission or missing file.
  • [ -w "/path/to/file" ] - check /path/to/file has write permission. False means no write permission or missing file.
  • [ -x "/path/to/file" ] - check /path/to/file has execute permission. False means no execute permission or missing file.
  • [ -u "/path/to/file" ] - check /path/to/file has user-ID description. False means no user-ID description is set or missing file.
  • [ -t "/path/to/file" ] - check /path/to/file has file descriptor. False means no or missing file.


String Flags

  • [ -z "" ] - check string is empty (length is zero). False otherwise.
  • [ -n "" ] - check string is not empty (length is not zero). False otherwise.
  • [ "string" ] - check string exists (not null). False otherwise.
  • [ "one" = "one" ] - check strings are the same. False otherwise.
  • [ "one" != "two" ] - check strings are not the same. False otherwise.


Arithmetic Flags

  • [ 1 -eq 1 ] - check both integers are equal. False otherwise.
  • [ 1 -ne 2 ] - check both integers are not equal. False otherwise.
  • [ 1 -gt 0 ] - check the former integer is greater than the latter. False otherwise.
  • [ 1 -ge 0 ] - check the former integer is greater than or equal to the latter. False otherwise.
  • [ 0 -lt 1 ] - check the former integer is less than the latter. False otherwise.
  • [ 0 -le 1 ] - check the former integer is less than or equal to the latter. False otherwise.

Compound conditions

GRUB scripting language does not support && and || syntax. Hence you need to use the bracket expression () with -a and -o conditions instead.

Example:

# AND case
if [ ("one" = "one") -a ("two" = "two") ]; then
        ... # do something when both expressions are true
fi

# OR case
if [ ("one" = "false") -o ("two" = "two") ]; then
        ... # do something when either one is true
fi

That's all about conditions in GRUB scripting language.