Write a sample 'hello world' script:
$ cat << eof > hello.lua
-- hello world!
systm.print("hello world!\n")
eof
$ luactl load s1 ./hello.lua
./hello.lua loaded into s1
$ dmesg | tail -1
[ 2769.687618] hello world!
Notice that the absolute path is required for luactl to read the script
more testing
1) parsing simple functions-- sum func
function add(x, y)
return x + y
end
systm.print("the sum of 16 and 32 is:\n")
systm.print (add(16, 32))
systm.print ("\n")
-- factorial func
function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
systm.print("the factorial of 7 is:\n")
systm.print (fact(7))
$ luactl load s1 ./sample.lua
./sample.lua loaded into s1
$ dmesg | grep -A 5 sample
[ 4283.160428] lua0: loading ./sample.lua into state s1
[ 4283.170432] the sum of 16 and 32 is:
[ 4283.170432] 48
[ 4283.170432] the factorial of 7 is:
[ 4283.180433] 5040
2) displaying OS info
-- print system info
function uname()
tbl = {"copyright", "cpu_model", "machine", "machine_arch", "osrelease", "os
type", "kernel_ident", "version"}
for i = 1, #tbl do
systm.print(systm[tbl[i]] .. "\n")
end
end
uname()
Which produces:
[ 5519.710421] lua0: loading ./sys.lua into state s1
[ 5519.720425] Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
[ 5519.730424] 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
[ 5519.730424] 2018, 2019, 2020 The NetBSD Foundation, Inc. All rights reserved.
[ 5519.740424] Copyright (c) 1982, 1986, 1989, 1991, 1993
[ 5519.750425] The Regents of the University of California. All rights reserved.
[ 5519.760427] raspberrypi,3-model-b
[ 5519.760427] evbarm
[ 5519.760427] aarch64
[ 5519.760427] 9.2_STABLE
[ 5519.770428] NetBSD
[ 5519.770428] GENERIC64
[ 5519.770428] NetBSD 9.2_STABLE (GENERIC64) #0: Fri Aug 20 19:33:44 UTC 2021
[ 5519.780428] mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/GENERIC64
3) display hostname
function hostname()
local f = io.popen ("/bin/hostname")
local host = f:read("*a") or ""
f:close()
host =string.gsub(host, "\n$", "")
systm.print (host)
end
systm.print("Welcome to:\n")
hostname()
This is not going to work:
$ luactl load s1 ./host.lua
luactl: LUALOAD: Invalid argument