1

Ok, I'm wanting to know if there's a way of running scripts from an external source with Lua. Be it another file in .txt format or from pastebin, what have you, and run the code and wait for said code to finish, and then continue on with the rest of the function. I'm not quite sure at all about how it'd work, but this is basically the idea I'm going by and isn't actual code.

function runStuff()
print("checking for stuff")
run.script(derp.txt)
wait for script
   if run.script == finished
   continue program
      elseif nil
   print("looks like this script sucks.")
   end
end
runStuff()

And for example what "derp.txt" contains is:

function lookFile()
   if herp.txt exists then
      rename herp.txt to herp.lua
   else
      nil
   end
end
lookFile()

I'm still new to Lua so I'm coding like a moron here, but you get my picture hopefully. I'm working on a project that'll act like an installer package for repositories based from pastebin or anywhere else that'll supply raw format outputs of lua scripts and I'm going to use that idea for it to call on scripts to run externally. So when I supply a "First Time Run" version of the program, it'll call out to a lua script that'll call to another lua script and install that script, then close.

This is for minecraft, mind you. ComputerCraft made me take interest in Lua, but anyway, hopefully you got the gist of what I'm trying to figure out. Hopefully that's doable and if not, I'll just have to figure something else out.

Ryan Stein
  • 7,930
  • 3
  • 24
  • 38
BluZero
  • 39
  • 7
  • take a look at `loadfile` or `dofile`. And try to at least skim through the documentations before posting on SO. – Bartek Banachewicz Feb 04 '13 at 12:28
  • So would loadfile work from a pastebin raw link? What I'm wanting to do here is run a script from an external source, not load a file in particular but open up said document and read the script inside and run that script. So for example running a script that's on pastebin, rather than just grabbing the script and saving it as a file and running it on your machine, loading up the link that the script is on and running the instructions given. – BluZero Feb 04 '13 at 12:37
  • Please, RTFM before you start asking more questions that doesn't make sense whatsoever. You have to supply a file with lua code in it, what's hard to understand here? If you want to grab the code *directly* from pastebin, you will need `luasocket` and a http query for the file contents. – Bartek Banachewicz Feb 04 '13 at 12:39
  • luasocket, I'll look into that. Thanks, and sorry if I irritated you or something. What I'm working with does have an http.request function in it so it's atleast got that ability. I can pull up the instructions and output it into the window and see it if I want to, just taking those instructions and rather than viewing them, just running them from there is what I'm trying to achieve, I'll look more into it and see if there's more functions I'm missing here. – BluZero Feb 04 '13 at 12:42
  • So if you have the contents loaded, you can transform the string version into something callable by `load` function. As in `f = load([[ print "foo" ]]); f();` – Bartek Banachewicz Feb 04 '13 at 12:49
  • So with what I'm going to be doing, if I do something like say: `f = load(webOutput)` `webOutput = http.get("pastebin.com/rawlink=[pastelink]")` something like that would work? Or would I need something a bit more indepth? – BluZero Feb 04 '13 at 12:55
  • I'd like to help you with this, but unfortunately you don't have enough reputation for SO chat :( – Bartek Banachewicz Feb 04 '13 at 13:18
  • No worries good sir. I learn to code a weird way by instead of using books I look at other code, and work from that making my own stuff. I don't know who else does this or can relate to that but there certainly is random stuff I miss that's pretty easy. Thanks for the help though, you've definitely shed some good light on this. Maybe now I can continue on with my project without any more problems... Until I come up with more ideas that I don't know how to do yet. – BluZero Feb 04 '13 at 13:37

1 Answers1

1

To load and execute a Lua code fragment you can use something like this:

local http = require("socket.http")
local response, err = http.request("url to some Lua code")
if not response then error(err) end
local f, err = (loadstring or load)(response)
if not f then error(err) end
print("done with "..response)
-- make sure you read on (in)security implications of running remote code
-- f() -- call the function based on the remote code

You probably need to use sandboxing.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • I did something that seemed to work with a bit of help from a couple friends: http://pastebin.com/raw.php?i=tzGm3aLn It seemed to do the trick, now I'm just trying to figure out how to record variables from the external program. – BluZero Feb 06 '13 at 08:16