Mohammad Fadin
Mohammad Fadin

Reputation: 579

How to import lua file to execute it?

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.

Upvotes: 1

Views: 4375

Answers (3)

Skylight
Skylight

Reputation: 21

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...:)

Upvotes: 0

jpjacobs
jpjacobs

Reputation: 9549

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!'

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249153

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).

Upvotes: 1

Related Questions