Tuesday, 12 November 2019

E2B v1.B8b Beta available + grub4dos coding tips

Changes from v1.B7 are are:
  1. Improve E2B scripts to allow the user to use a greater number of grub4dos environment variables in the MyE2B.cfg file (grub4dos max is 60).
  2. Add .vhdx file extension (was removed in last few versions).

About grub4dos environment variables

If the user defines too many grub4dos variables using 'set' commands in the MyE2B.cfg file or in their menus and their .g4b scripts, then E2B may fail with various grub4dos errors being reported at various odd places!

Whilst 60 environment variables may sound like more than enough for the grub4dos environment, in practise it is not a lot. Consider that E2B uses approximately 20 variables just for a normal menu and that the grub4dos scripts also need to define temporary variables inside each script. This leaves about 20 variables free for the user to define in the MyE2B.cfg file. Sometimes this is not enough so I have tried to improve the E2B scripts so that more variables can be defined in the MyE2B.cfg file without causing weird grub4dos errors. One side affect of the new change is that the Windows ISO/IMGPTN files are now re-enumerated when you enter the main Windows Menu. When in the Main Menu, the individual Windows file counters such as CXP, C7, C8, etc. are now no longer present, only the CALL variable (for total count of files under \_ISO\WINDOWS) is available.

Writing grub4dos code

If you write any grub4dos code\scripts, be careful to remove any temporary variables that you have created if they are no longer needed. This avoids eating up the number of free variables!

Tips:

Use
set x=
to remove the variable x (make sure there is no space after the = sign.

To remove several variables you can use:
set x= ;; set y= ;; set z=
Note that in this case the single space after = is ignored (but you should not put a space after 'set z=').

You can type
varsleft
at the grub4dos command prompt to see how many free grub4dos environment variables are left to define. This runs the varsleft.g4b script in E2B.

Difference between && and &;

grub4dos 0.4.6a allows you to put more than one command on a single line.

&& means (if true) and || means (if false), e.g.

!BAT
set x=1
# This will display Hello and then Sailor! on the next line
if %x%==1 echo Hello && echo Sailor!
# This will display Bad boy!
if %x%==2 echo Hello || echo Bad boy!
# Remove the variable as we have finished with it
set x=

Note that x must be defined or else the line will be interpreted as 'if ==1' and grub4dos will report 'Error 30: Invalid argument'! If you are not sure if x is defined you can use a string comparison using double-quotes:

if "%x%"=="1" echo Hello && echo Goodbye

You can test for the existence of a variable using if exist:

if exist x echo x exists and has the value %x%

Note that there may be one or more spaces at the end of the string and they would not be visible in this case, but by using * symbols at either end we can see if x contains spaces at the start or end of the string. If we just used %x% then we wouldn't see any spaces. For instance,

# define a string with a space at the end (make sure you type a space at the end of the next line)
set x=fred 
if "%x%"=="fred" echo XXX || echo -e x=*%x%\*
# define a string with a space at the beginning
set "x= fred"
if "%x%"=="fred" echo XXX || echo -e x=*%x%\*

This will not print XXX.

Note that because %* is a special operator meaning 'all command line parameters' in grub4dos-speak, if we want to use the * character and it follows a % sign, we need to escape the * using \* instead, but the escape character only works with echo if we specify the -e parameter. If we just use a different character instead of * it would be simpler - e.g.

# define a string with a space at the end (make sure you type a space at the end of the next line)
set x=fred 
if "%x%"=="fred" echo XXX || echo x=[%x%]
# define a string with a space at the beginning
set "x= fred"
if "%x%"=="fred" echo XXX || echo x=[%x%]



Note that when using && or ||, the environment is not updated until after the whole line has been executed. This can trap the inexperienced user. For example:

!BAT
set x=1
if %x%==1 echo x is %x% && set x=2 && echo x is %x%

Not what you were expecting perhaps?

Later versions of 0.4.6a also support the  &; ,   |;  and  ;; operators.

;; allows you to combine multiple commands on one line - it has the same affect as if you wrote the commands on separate lines.

&; and |;  are the same as && and || except that the environment is updated immediately, so now we can have...

!BAT
set x=1
if %x%==1 echo x is %x% &; set x=2 &; echo x is %x%

That's better!
and we get the expected result.

setlocal\endlocal

You can and should use setlocal and endlocal in your scripts. For instance, if your script only requires two variables when it is called, say x and y, you can use setlocal and endlocal to ensure you have plenty of environment variable space by deleting any variables that you don't need:

comparexy.g4b

!BAT
# first copy all environment variables into a temporary area of memory 
setlocal
# now delete all the variables in the new temporary area using set * except x and y
set * && set x=%x% && set y=%y%
# set result to 0
set result=0
# if x is the same as y then set result to 1
if "%x%"=="%y%" set result=1
# now restore all the previous environment variables but also set result as a new variable
endlocal && set result=%result%
# exit
goto :eof

Note that in this case the && command is useful because even though we have used  set * to clear all the variables, we can still use set x=%x% on the same line because the variable x will still be present until the whole line has executed.

.VHDX

I removed the .vhdx file extension in a few of the previous versions of E2B and prompted the user to rename it to .vhd, however it seems that Windows looks at the file extension to determine how to boot it and so the file needs to end in .vhdx to avoid a BSOD! So the .vhdx file extension is back.

Early Betas are available from the Alternate Download areas as usual.

No comments:

Post a Comment