Hello, world!

Let's try to make a Lua script. Let's first create a script that outputs the "Hello, world!" string onto the console.

Direct Execution from the Command Line

Firstly, login to the Yamaha router console and enter Management Mode with the administrator command. All future operations on the console will be performed in Management Mode.

#

Now enter the following commands. The underlined string are the characters that you need to enter.

# lua -e 'print("Hello, world!")'
Hello, world!
#	

So how did that go? Did "Hello, world!" display on the console screen?

Here, the Lua script is directly executed from the command line. The lua command is a command to execute Lua script and the -e option indicates that the string after it is the Lua script. As there are blank spaces and double quotes in the script, the script string is enclosed by single quotes when it is input from the console.

"print("Hello, world!")" is the Lua script. The print function is a function that outputs the given argument to the console as a string. In this case, the string "Hello, world!" is given as the argument, so it is output as is on the console.

Next, let's execute a more script-like example. Enter the following commands.

# lua -e 'for i=1,10 do print(i, "Hello, world!") end'
1     Hello, world!
2     Hello, world!
3     Hello, world!
4     Hello, world!
5     Hello, world!
6     Hello, world!
7     Hello, world!
8     Hello, world!
9     Hello, world!
10    Hello, world!
#

The for statement is a control statement to loop. This for statement means "repeat the statement (in this case, the print function) between do-end while increasing the variable i from 1 to 10 one by one".

Executing a Script Saved in a File

The method to describe a script directly on the command line is very handy when executing simple scripts, but it is not realistic when scripts become long. So let's try executing a script saved in a file.

Create a file on your PC called "hello.lua" that contains the following script and forward it with the file name "/lua/hello.lua" to your router over TFTP.

Download [hello.lua]

for i=1,10 do
  print(i, "Hello, world!")
end

To forward it with a TFTP command (if there is no administrator password configured on the router)

C:\>tftp 192.168.100.1 put hello.lua /lua/hello.lua

To forward it with a TFTP command (if the administrator password configured on the router is doremi)

C:\>tftp 192.168.100.1 put hello.lua /lua/hello.lua/doremi

Next, enter the following command and execute "hello.lua".

# lua /lua/hello.lua
1     Hello, world!
2     Hello, world!
3     Hello, world!
4     Hello, world!
5     Hello, world!
6     Hello, world!
7     Hello, world!
8     Hello, world!
9     Hello, world!
10    Hello, world!
#

If you execute the lua command without the -e option, the lua command presumes that the file name is the argument and executes the script described in that file. In the above case, "/lua/hello.lua" is the file name and executed the script in that file. The content of the file is the same as the loop that used the for statement that you just executed and the result is the same.

There is no special restriction for the file name of the file saved with the Lua script. In this example, the Lua script file is saved in the /lua directory with the extension .lua, but there is no rule that say you must do this. Furthermore, in addition to the FlashROM built-in to the router, the Lua script file can also be executed on microSD or USB memories if the router hardware has an interface.

Return to Top