How Can I Execute a Non-Lua File From a Lua Script? - lua

I have a Lua script and I want to execute a batch file from within this file; how can I do this?
I've seen examples as follows that I thought could do this but in retrospect I am probably misunderstanding their purpose:
os.execute('C:\\tmp\\MyFile.bat')
or
local handle = io.open('C:\\tmp\\MyFile.bat')
handle:close()
I believe this question has been asked and answered before: How do I run an executable using Lua?
However my code is failing; I'm not sure why. Is there a library I need to load for 'os' for example?
When I try and run this code I get the following error:
"attempt to index a nil value"

You have to use os.execute[[yourFile.bat]] when opening a batch file.
More information for this here

Related

lua custom terminal not having command outputs

Im trying to make a terminal but im stuck on one thing. In the doer program command do. I want docom to be the output of of the loadstring. input = io.read() its a lua terminal inside my program but nothing displays any output. Here is the code that is relevant:
docom = loadstring(input)
print(docom)
How do i make the output display? Because currently its like this:
welcome to the terminal!
loaded
do
do:
print("hello")
function: 0x809b60
do:
The third and fifth line are user inputs. how do i fix this so it shows the hello string instead of the function name. i want this to be able to manage it as i have everything else in the same lua script. please help.
You probably want print(docom()).
loadstring compile a script into a function. That's what you see function: 0x809b60.
loadstring does not run the function. Hence the call docom().
You may want to add error handling by checking whether docom is nil and by calling docom via pcall.

File not working in Lua

I am doing a small program for school in Lua, but I need help. I open a file, but when I write, nothing appears in the .txt file. Can anyone help? This is the snippet of code I am trying to fix:
file=io.open('var.txt',"w+")
io.output(file)
io.write('hi!')
Edit: I tried file:close() and io.flush(), but I haven't managed to make it work.
Try the following:
io.output('var.txt')
io.write('hi!')
io.close()
The function io.output allows you to specify a current file for output by its filename. In your example, you were passing a file handler that you created with io.open instead of a filename. This implicitly creates a bad file handler that io.write cannot use.
For more info, check out the chapter on "The Simple I/O Model" from "Programming in Lua".

Read data before executing lua file

I want to read a table inside a Lua file before executing it. Is there a way to do this with loadfile. It returns only a function and I can't seem to be able to read what is inside (what is declared but not executed).
The other option I tried is to check if the environment changed, but yet again I couldn't read inside the function returned by loadfile().
Is there a way to do this without opening the file as text and searching the table?
Here is an example of the table I try to retrieve:
--file to be loaded
local library = require("library") --random requires...
table = { author = "myself", dependencies = "library > 1.0"} --table to get before execution
What you want is not possible.
There are no declarations in Lua, only executable statements.
You need to execute a script to see what it does.
However, you could read the file as text and try to extract the info you need using pattern matching. This won't be foolproof but it'll probably work in most cases if the files are written in the same way.

Loading lua script files in redis

Could someone give an example of how to load and execute .lua script files in windows. I am using ServiceStack redis to loadluascript. It works to certain scripts which don't have module(...) like things.
I am getting this error
Error compiling script (new function): user_script:5: cannot use '...' outside a
vararg function near '...' , sPort: 61688, LastCommand:
Any help by giving an example would be highly appreciated.
Thanks in advance
It would help if you posted the Lua script you are trying to load or execute.
Three dots don't have anything to do with modules:
Vararg expressions, denoted by three dots ('...'), can only be used
when directly inside a vararg function
I guess this answers your question: your Lua code is simply invalid.
Speaking of modules: you can't load your own modules in Redis Lua, which you might already know. See http://redis.io Scripting.
The solution for the above kind Lua script is to prepend local before a function or all variables. I took out the module thing and tweaked the Lua script to make it work. Later I realized the script will not be any use to me :). Thanks for looking into this post.

Lua: require fails to find submodule, but searchpath succeeds?

usually when I have a question about something remotely software related I find that someone else has already asked the very same thing, and gotten good answers that works for me too.
This time, though, I've failed to find an answer to my predicament.
Here we go:
I'm currently trying to move up my Lua-programming a notch or three and want to use modules. So, I've got a structure like this:
main.lua
foo/bar.lua
Now, in main.lua I do
require("foo.bar")
which fails,
main.lua:1 module 'foo.bar' not found:
no field package.preload['foo.bar']
no file 'foo.bar.lua'
no file 'foo.bar.lua'
no file 'foo.lua'
Ok, something might be wrong with my package.path so I use package.searchpath("foo.bar", package.path) to see what I', doing wrong.
The problem is that package.searchpath resolves foo.bar to foo/bar.lua which is exactly right.
As I've understood it, package.searchpath tries to find the module in the same way as require, but there seems to be som glitch in my case.
What strikes me as odd is the repetition of the no file 'foo.bar.lua' in the error output
Have I misunderstood the use of require?
I'm using LuaJIT-2.0.0 to run my chunks
Update:
I'm using LuaJIT-2.0.0 to run my chunks <- This was the reason for my problem, stock Lua-5.2.2 behaves as expected
package.path = debug.getinfo(1,"S").source:match[[^#?(.*[\/])[^\/]-$]] .."?.lua;".. package.path
require("foo.bar")
This line causes require to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory, insert the
relative path between " and ?.lua
Here is part of require description:
[...] Otherwise require searches for a Lua loader using the path stored in
package.path. If that also fails, it searches for a C loader using the
path stored in package.cpath. If that also fails, it tries an
all-in-one loader (see package.loaders).
Default path for package.path is always the .exe that executes specified script.

Resources