a1ive/grub

Query: file string search?

steve6375 opened this issue · 8 comments

In Easy2Boot I have a grub4dos code which asks the user to input the computer name and user account name that they want for a Windows Install.
It then uses 'search&replace' code to replace the strings 'PCNAME' and 'UNAME' (and the Product key) with the users input in an XML file.
This means that the user can use the same XML file to install many different computers with Windows.

  1. Boot to grub2
  2. User enters account name and computer name
  3. grub2 changes XML file
  4. Windows Setup runs unattended with new XML file

The user input strings can be longer than 'PCNAME' and 'UNAME'.
Can you suggest a way this can be done in grub2?

grub4dos has an enhanced cat command which can be used to find the position of a string in a file. Can grub2 do this? If I knew the position of a string perhaps I could use dd with seek and skip to copy portions of the input XML file to an output XML file?

a1ive commented

You could write lua scripts to do this.
Lua is much more powerful than grub2 bash-like scripts.

It doesn't seem to have the io module for io.open, etc.
I think there is some file support...

-- globals.lua
-- show all global variables

local seen={}

function dump(t,i)
	seen[t]=true
	local s={}
	local n=0
	for k in pairs(t) do
		n=n+1 s[n]=k
	end
	table.sort(s)
	for k,v in ipairs(s) do
		print(i,v)
		v=t[v]
		if type(v)=="table" and not seen[v] then
			dump(v,i.."\t")
		end
	end
end

dump(_G,"")

image
e.g. but I have no idea how to use it.
I want to open the file
read in a line
use gsub to replace a string
write the line to another file (can be an existing file)
on an NTFS or FAT32 volume.

?? any hints please when you have time ??

a1ive commented

please upload you xml file, I will write an example tomorrow.

infull.zip
That is very kind of you, thank you. I have never used lua and there seems to be no mention of 'file_getline' on Google for lua???

So I want to replace (in this example) all occurences of 'UNAME' with 'steve' and 'PCNAME' with 'ComputerA'

So a function like repl "searchstr" "replacestr" infile outfile
would be ideal
Note that the file has < and > and ! characters!

a1ive commented

abc.zip
Extract it to (hd0,1)/
and run lua (hd0,1)/write.lua

a1ive commented

abcd.zip
Extract it to (hd0,1)/
and run lua (hd0,1)/write.lua
深度截图_选择区域_20200301101832

Thank you so much!
That had the examples I needed and I now have it working. :-) 💯

a1ive commented

That had the examples I needed and I now have it working. :-) 100

I forgot to run grub.file_close.Please add it.

file1 = grub.file_open ("(tar)/test.xml")
file2 = grub.file_open ("(tar)/empty.xml", "w")
uname = read_input ("UNAME")
pcname = read_input ("PCNAME")
search_replace (file1, file2, {"UNAME", "PCNAME"}, {uname, pcname})
grub.file_close (file1)
grub.file_close (file2)