How to import lua file to execute it? - lua

I'm using Lua Mac 5.1.4 Compiler.
I'm trying to import lua file and run it.
I tried using this code:
% lua hello.lua
But I'm getting this error: stdin:1: unexpected symbol near '%'
Am I doing something wrong? This is my first day using lua so be easy on me.
Thank you.

The problem is probably that you saw this verbatim text in a tutorial:
% lua hello.lua
The '%' at the beginning of the line is not something you are supposed to type into your terminal, but is rather a generic prompt indicator. Sometimes you might see it written as '$' instead:
$ lua hello.lua
In either case, the first character is not something you type, but rather is a typographical convention to suggest that what follows is to be typed at a prompt. Your actual prompt might look something like this:
mo#macbook$
So you would type lua hello.lua but your screen would look like this:
mo#macbook$ lua hello.lua
So, try just entering lua hello.lua and see what happens.
Note that the error message you got regarding stdin:1 is likely from your shell (e.g. bash), and not from Lua (which never even started running due to the malformed command in the shell).

The error stdin:1: unexpected symbol near '%' suggests that you typed in % lua hello.lua while in an interactive lua session (or you executed a script containing it). Now that's something that you should type in in the commandline window.
Instead try something like print'Hello World!'

Lua provides two ways to call a file. One is the loadfile() and the other is the dofile() commands. Try using dofile("hello.lua").That should work.If it doesn't type the absolute path...:)

Related

SciTE can not find user defined function. The editor executes "/bin/sh" for some reason

So, this is my first attempt to make my source code editing more comfortable. I follow the SciTE scripting guide.
ext.lua.startup.script=$(SciteDefaultHome)/startup.lua
command.name.1.*= Programmer's Manual (selected text)
command.subsystem.1.*= 3
command.1.*=man_select
command.shortcut.1.*=F1
Above is the user properties file. It binds F1 key to user defined Lua function.
The problem is, that insteed of starting the function, man_select in my case, the SciTE editor output gives me
>man_select
/bin/sh: man_select: not found
>Exit code: 127
So, the editor runs a shell with my function name as a argument. I can't find no SciTE logs, or console, to see what the cause is.
I define man_select as a simple Lua function, without arguments:
function man_select()
local sel = editor:GetSelText()
...
end
What should I do to make my own Lua functions visible to SciTE editor? Is there a way to ask SciTE for more information on its scripting system flow?

Basic Erlang module troubles

So I straight up copy and pasted the code from the Erlang docs: https://erlang.org/doc/reference_manual/modules.html#module-syntax
Please help!
The module didn't compiled. To fix it try this in the Erlang shell (eshell):
1> c(m).
ok
2> m:fact(1).
1
See documentation on how code loading works.
exception error: undefined shell command
almost always means that the shell environment does not have the intended function, which can also mean that the module which contains its definition has not been loaded for all the code that we write by our hand and execute. You can deliberately try to misspell some auto loaded BIF(s) for example "length1" and you will still see same message showing up.

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.

An error with io.read() in lua

When I use io.read() I get input from the user but when I do so it wont count backspaces, so if I type:
blah blah blaht
when my program wants input then if I delete the t:
blah blah blah
It still reads it as if the t is there, help?
EDIT: Only does this when debugging, im using SciTE, thx
I have been trying to figure this out for a long time and I came across this:
You have two programs here: SciTE and cmd. SciTE sends each visible
character like 'r' through to cmd as soon as it is typed. ScITE
interprets the backspace key internally to change the buffer but does
not send the key through to cmd. You can not edit a command line as
you want. Simplest to type Enter and try again. (source)
Unfortunately it doesn't seem to be possible to fix this.
Your best options are:
Execute the Lua script outside of SciTE and then you wont have this issue
Use another editor

Why won't applications in Program Files run using os.execute in lua?

I'm trying to run an executable using Lua's os.execute() function. If I do something like the following it does not work:
os.execute("C:\\\Program Files\\\Movie Maker\\\moviemk.exe")
However, if I put my lua file in the same path that moviemk.exe is in then it can call it.
Any ideas why this may be?
P.S. I'm using Windows XP SP3
This is a classic problem with the command shell. It isn't really a Windows specific problem, except that on *nix, people never really got into the habit of putting spaces in file names, and Windows puts spaces in several default system places such as C:\Program Files.
What is happening is that os.execute(str) is implemented in terms of the ANSI C function system(str), which on Windows tries to duplicate the effect of typing "cmd /C "..str to the command prompt. (On *nix, it uses /bin/sh -c instead of cmd /C.)
The classic problem is that this has to split the complete command string at whitespace to decide what program to run, and what its arguments are.
Your original example: os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe") effectively became cmd /c c:\program files\movie maker\moviemk.exe, which after splitting it up on whitespace, CMD tried to find a program named c:\program to execute with arguments named files\movie and maker\moviemk.exe. This isn't what you intended.
The solution is to be much more defensive about quoting.
I would write this as:
os.execute [["C:\Program Files\Movie Maker\Moviemk.exe"]]
If there were additional command line arguments to be supplied, I would use double-quotes around each, and a single space between arguments. Using the long string syntax [[...]] has the advantage that backslash isn't a special character, so you don't need extra leaning toothpicks making it harder to read the string literal.
Using double quotes around each argument should work on both Windows and *nix, although it is harder to find commands that are the same on both platforms, of course.
One other detail to be aware of is that \Programs Files might not be on C:. There might not even be a disk named C:. (My work PC boots from E: and I find more buggy programs that way.) The easiest way to learn the correct pathname is to just use the environment variable ProgramFiles. There are many other ways.
Try:
os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe")
or:
os.execute("C:/Program Files/Movie Maker/moviemk.exe")
The '\' character is used for escape characters in Lua.

Resources