Friday, July 3, 2009

How to run a simple script Lua script from command line in Windows

originally found here: http://zegarkus.com/2007/02/13/how-to-run-a-simple-script-lua-script-from-command-line-in-windows/

Recently a ‘new to me’ programming language called Lua piqued my attention. Its a lightweight scripting that is commonly embedded in other languages such C and C++ and also used for scripting. There are other applications for it as well, such as Lua CGI and Lua Server Pages, which can be used on webservers. Its also a favoured scripting language for the gaming industry.

Anyways, after downloading the latest standalone of lua (lua5.1.exe), I just wanted to run a very simple “Hello World”esque script - eg HelloWorld.lua from the Lua Standalone. I learned bits an pieces of Python from Idle Python Shell, so I thought I could call up scripts in a simple fashion this way.

The documentation seemed to indicate that I could run scripts the following way in the command line:

lua HelloWorld.lua

However, I kept getting this error:

> lua HelloWorld.lua
stdin:1: ‘=’ expected near ‘HelloWorld’
>

I couldn’t figure it out and I couldn’t find anything how to call up a .lua script through google. Needless to say, I didn’t feel very intelligent. Oh I tried a whole slough of different searches on google and precursory glances at the documention. Here is the progression of google search terms I used:

  • lua tutorials
  • beginners lua tutorial
  • lua for beginners
  • lua programming beginners tutorials run script
  • how to run a .lua files
  • how to run my first lua script
  • lua hello world
  • helloworld.lua
  • hello.lua
  • lua standalone tutorial
  • how to run lua scripts
  • how to run a .lua files from cmd line
  • how to run a .lua files from command line
  • lua environmental variables
  • complete noob looking to run lua from lua5.1.exe
  • going out of my mind looking for a way to run lua through command prompt
  • how the fuck do you run lua from standalone!%@#$@#!!!!

Though I found some great tutorials - I found no answers!
Finally, after 1 hour, I opened up the Caller.lua file which was in the root directory of my luaedit IDE, and there was this interesting line:

dofile(”.\\Function.lua”)

This incidentally pointed to Function.lua file in the root directory. Bingo! I quickly looked up the dofile function in the manual:

dofile (filename)
Opens the named file and executes its contents as a Lua chunk. When called without arguments, dofile executes the contents of the standard input (stdin). Returns any value returned by the chunk. In case of errors, dofile propagates the error to its caller (that is, it does not run in protected mode).

So I ran it, it worked!:
> dofile(”.\\HelloWorld.lua”)
The World has ceased waiting for you. Please be more prompt in find ways to run
Lua from the command line
>

Yep its all documented. My guess is that Lua developers don’t generally run scripts this way? The documentation suggests $ lua -e’a=1′ -e ‘print(a)’ script.lua as a way of running scripts, but I can’t seem to get it to work (yet).

Anyways, I look forward to learning cool stuff about Lua!

Update 18.Feb.2007 ——————–

I have found three of running LUA scripts: LuaEdit, Lua Interpreter (same as above) and windows command line (cmd.exe). You can visit this Lua forum thread to see how its done, or just read here:

To view the output of a lua script with Lua Edit:

  • Go to the view menu
  • Select ‘Debug Windows’ and then “Lua output”
  • You can dock as you prefer

To run it from windows to command line (cmd.exe):

  • go to the directory where lua5.1.exe is located
  • run your script with this command: Lua testfile.lua
  • you can also add the your Lua folder as a windows system environmental variable

2 comments:

  1. Hi, to say you saved my life is about as close as it gets, well documented process. Thanks a bunch.

    ReplyDelete
  2. Hi, I am VERY new to programming and i am not sure that i clearly understand scripts. I was trying to figure out the same thing as you and stumbled across the dofile() function. However, my file does not open. I think it is not located in the correct location (...documents/programs/luatest/Hello.lua). Where am i supposed to put this file?? I see a lot of usr/local/bin mentioned, but i do not know what that is... Also, i can't use the 'Lua' call from the windows command prompt... it says " "Lua" is not recognized as and internal or external command..." I am in the correct directory... Any help would be appreciated.

    ReplyDelete