Murga-Projects Forums
"murgaLua -e string" in Windows and Mac - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: "murgaLua -e string" in Windows and Mac (/showthread.php?tid=289)


"murgaLua -e string" in Windows and Mac - mikshaw - 02-22-2008 05:09 AM

I wonder if someone on Windows and Mac could do a test for me.
The -e option of murgaLua should allow you to run a string that can be applied to a given script.

In Linux I can pass a variable to a script using the following:

Code:
murgaLua -e 'variable=\"this is a string\"' scriptname.lua

This apparently doesn't work in Windows, and I'm not sure about Mac (I think it probably works).

If someone could do a test or two to see if it might work with 'variable=[[this is a string]]' or something similar, could you please post the solution here?

Thanks.


RE: "murgaLua -e string" in Windows and Mac - iGame3D - 02-22-2008 07:17 AM

Looks like it works on Mac.

Code:
if a~= nil then
if a==1 then print("ok") end
end


Code:
murgaLua -e 'a=1' /Volumes/iGame3D/test.lua

Returned "ok"


RE: "murgaLua -e string" in Windows and Mac - Juergen - 02-22-2008 08:05 AM

mikshaw Wrote:
In Linux I can pass a variable to a script using the following:

Code:
murgaLua -e 'variable=\"this is a string\"' scriptname.lua

This apparently doesn't work in Windows, and I'm not sure about Mac (I think it probably works).


Why do you think that this doesn't work in Windows?
You don't have to escape the double quotes when you use the single quotes. lua -e 'variable="string"' works perfectly. You only have to escape the double quotes when you use double quotes to quote ;-) (-e "variable=\"string\"")

On Windows you can only use double quotes, but the escape mechanism is the same as on Linux or nearly every other OS.

Juergen


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-22-2008 12:06 PM

Quote:
Why do you think that this doesn't work in Windows?

Because the last time i had access to a windows machine I tested it and it failed.

Quote:
You don't have to escape the double quotes when you use the single quotes. lua -e 'variable="string"' works perfectly.

I wasn't aware of this. Thanks for the lesson.

Quote:
On Windows you can only use double quotes

I'm guessing it was probably the backspace that was causing the error then? Or do I need to always use only double quotes if I want it to work in Windows? If that's the case how would you nest quotes?


RE: "murgaLua -e string" in Windows and Mac - Juergen - 02-22-2008 08:12 PM

Quote:
On Windows you can only use double quotes

I'm guessing it was probably the backspace that was causing the error then? Or do I need to always use only double quotes if I want it to work in Windows? If that's the case how would you nest quotes?
[/quote]
The same as on Linux:

Code:
lua -e var="string" --doesn' work on Windows or Linux
lua -e var='string' -- does work on Windows but not on Linux
lua -e var=\"string\" --does work on Windows and Linux
lua -e "var=\"string\"" --does work on both
lua -e 'var="string"' -- works on Linux but not on Windows
lua -e "var='string'" -- works on both
lua -e "var=\'string\'" -- works on Linux but not on Windows
lua -e var1=\"string\"\ var2=\"string\" -- works on Linux but not on Windows
...

This applies to Bourne/Korn compatible shells on Linux and CMD on Windows. Other shells might have a different quoting behavior.

Juergen


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-22-2008 08:55 PM

Ok, if it works on Windows there is something else causing the problem.
Specifically what I'm trying to do is run a separate murgaLua process that runs a new script using a variable created in the current script. This is what I have working in Linux:

Code:
io.popen(MURGALUA.." -e 'images=\""..images.."\"' "..tempfile)

MURGALUA is the murgaLua executable
tempfile is a dynamically created script file
images is a Lua variable being passed, which is a directory that tempfile wouldn't be able to find otherwise.
If I recall correctly, I got a "bad symbol" error in Windows.

This also works in Linux, but I haven't yet tested in Windows:

Code:
io.popen(MURGALUA..[[ -e 'images="]]..images..[["' ]]..tempfile)


Maybe there is a problem with the string being interpreted first for io.popen and then being misinterpreted when it's run. It might end up being sent in this format:

Quote:
lua -e 'var="string"' -- works on Linux but not on Windows

If that's the case, I think it might be a problem with using a backslash escape after all.
It looks like one option could be something like this:

Code:
io.popen(MURGALUA..' -e "images=\"'..images..'\"" '..tempfile)

which I hope would run this command:

Code:
murgaLua -e "images=\"<images path>\"" <new script>

However, I could see it doing this instead:

Code:
murgaLua -e "images="<images path>"" <new script>

which I guess would probably also fail.

Perhaps the best solution would be

Code:
io.popen(MURGALUA..[[ -e "images=']]..images..[['" ]]..tempfile)

which is essentially the same as the second one above but with the single and double quotes swapped, hopefully resulting in this:

Code:
murgaLua -e "images='<images path>'" <new script>

That one looks like it should work in Windows.


RE: "murgaLua -e string" in Windows and Mac - Juergen - 02-22-2008 09:55 PM

Hi Mike,

first you don't need the outer quotes. lua -e var=\"string\" works. The outer quotes are only necessary if there is a space in the "-e" argument, because then it would be treated as two separate arguments and interpreted as a filename. For example lua -e var1=\"string\" var2=\"string\" would not work. But lua -e var1=\"string\" -e var2=\"string\" or lua -e "var1=\"string\" var2=\"string\"". On Linux there is also the possibility to escape/quote the space charecter ("\ ") so that the Lua interpreter doesn't see two arguments.

There is one logical error in your examples. If you want to the shell to see something like "var=\"string\"" then you have to write it the following way:

Code:
io.popen(MURGALUA.." -e \"images=\\\""..images.."\\\" "..tempfile)

You forgot to escape/quote the backslash so that it could be send literally to the shell. In your examples you quoted only the double quotes (in the Lua string) to appear in the shell. If you also quote the backslash it should work without problems.

Juergen


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-23-2008 01:10 AM

Quote:
The outer quotes are only necessary if there is a space in the "-e" argument

That's a strong possibility, though. The script checks the location of itself and uses that directory as the value of images. In windows it is almost guaranteed that there will be a space in there if the user has the script somewhere in his home directory ("C:\Documents and Settings\username\", for example). I've always hated this aspect of Windows, but it's there so I have to deal with it. Perhaps just having quotes around the value of the variable is enough to tell the shell that images="C:\Documents and Settings\username\" is a single parameter? In other words, would this be passed correctly:
murgaLua -e images="C:\Documents and Settings\username\" scriptname
EDIT: no, that doesn't even work in Linux. The entire string after -e must be passed as a single string regardless of whether or not there are spaces. murgaLua -e variable="a_string_without_spaces" didn't even work.

Quote:
You forgot to escape/quote the backslash so that it could be send literally to the shell.

I'd actually thought about this briefly, but it seemed that if it works in Linux then maybe it's not an issue. After seeing your example of the way double quotes nested within single quotes don't work in Windows, I believe you're correct.
Unfortunately escaping escapes is something that has always bothered me as well....mostly just because it looks messy and is sometimes hard for me to follow.

I'm hoping that the double brackets will work instead of needing to escape quotes, mainly because it is the cleaner method in my opinion.


RE: "murgaLua -e string" in Windows and Mac - Juergen - 02-23-2008 03:58 AM

If you wan't to set a filename in the script you wan't to call, it might be a little bit harder. Although there might be a few solutions. I think the easiest solution would be to replace the "\" with "/" and replace them in the script you call again (string.gsub(var,"(\\)","/")). There could be also be another problem, because Windows user put all kind of weird characters in their filenames, because they can.

If you want to be really on the save side you can do the following (although it might be a little bit overkill):

Code:
io.popen(MURGALUA.." -e images=\""..(mime.b64(file,nil)).."\" "..scriptname,"r")
--in the script you can get the directory with:
images=(mime.unb64(images,nil))

While this might be a little bit overkill, it is a method that wil definitely work.

Juergen


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-23-2008 11:21 AM

I think you might be reading more into this than is necessary. Yes, I understand that there could be weird characters in a Windows path, but that's something I have no interest in supporting. All I'm trying to do here is to pass the lua command images="some path". If that path has ridiculous characters in it and the script fails, I don't care...I see that as a moronic thing that the end user will have to deal with. File paths should be alphanumeric, and anything else beyond spaces (which are sadly much too common to ignore) is not worth my time.

I will look into this last suggestion, though, since it doesn't seem to be too complicated as far as filesize and processing goes.

Thank you for all your help. I'm not sure when I'll next be testing on windows, but you've given me a lot of options that I hadn't seen before.

edit: alphanumeric is probably a bit more restrictive than I intended. There are a few characters, underscores and hyphens (and tilde for backups), that are acceptible. Just about anything else has the potential to cause problems for numerous applications.


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-28-2008 12:06 AM

Still haven't gotten back on a Windows machine to find a definite solution. I may just release the next widgets_demo beta as-is and hope someone else can fix it if it's broken in Windows =o)


RE: "murgaLua -e string" in Windows and Mac - JohnMurga - 02-28-2008 01:17 AM

mikshaw Wrote:
Still haven't gotten back on a Windows machine to find a definite solution. I may just release the next widgets_demo beta as-is and hope someone else can fix it if it's broken in Windows =o)

I'll volunteer to do that :-)

Cheers
JohnM


RE: "murgaLua -e string" in Windows and Mac - iGame3D - 02-28-2008 07:28 AM

While we are on the topic, whats the " -e " for anyway.
Is there somewhere that explains those arguments?


RE: "murgaLua -e string" in Windows and Mac - mikshaw - 02-28-2008 08:16 AM

It's a standard Lua commandline option, documented in section 6 of the Lua manual.

Quote:
-e stat: executes string stat

So for example, the following command would set scriptname.lua to use the plastic scheme even if Fl:scheme(NULL) is not in the script:

Code:
murgaLua -e "Fl:scheme('plastic')" scriptname.lua


In the new widgets_demo layout I'm using this to set the images variable so individual scripts can find the images. The scripts themselves cannot find this directory because they are run from temp files.