测试环境说明
操作系统
[xxxx@localhost lua]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[xxxx@localhost lua]$ uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[xxxx@localhost lua]$
lua
[xxxx@localhost lua]$ lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
shell
[xxxx@localhost lua]$ sh --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
shell调用lua
本测试中使用的lua脚本和shell脚本在同一个目录中。lua脚本名称为test.lua,shell脚本名称为test.sh。同时shell脚本具有执行权限。
[xxxx@localhost lua]$ ls -l
total 8
-rw-rw-r-- 1 zdns zdns 194 Oct 23 11:15 test.lua
-rwxrwxr-x 1 zdns zdns 61 Oct 23 11:39 test.sh
lua脚本
lua脚本文件名称:test.lua
lua脚本文件内容:
exeFileName = arg[0]
param1 = arg[1]
param2 = arg[2]
print("filename:"..exeFileName..", param1:"..param1..", param2:"..param2)
function showAll(args)
for key,val in pairs(args) do
print(string.format("key:%s, val:%s", key, val))
end
end
print("showAll <")
showAll(arg)
print(">")
执行lua脚本: lua test.lua a b c d
执行结果:
[xxxx@localhost lua]$ lua test.lua a b c d
filename:test.lua, param1:a, param2:b
showAll <
key:1, val:a
key:2, val:b
key:3, val:c
key:4, val:d
key:-1, val:lua
key:0, val:test.lua
>
shell脚本
shell脚本名称:test.sh
shell脚本内容:
#!/usr/bin/sh
ret=`lua test.lua a b c d`
echo "ret: [$ret]"
执行shell脚本:./test.sh
执行结果:
[xxxx@localhost lua]$ ./test.sh
ret: [filename:test.lua, param1:a, param2:b
showAll <
key:1, val:a
key:2, val:b
key:3, val:c
key:4, val:d
key:-1, val:lua
key:0, val:test.lua
>]
[xxxx@localhost lua]$
lua调用shell
本测试中使用的shell脚本和lua脚本在同一个目录中。shell脚本名称为test.sh,lua脚本名称为test.lua。
[xxxx@localhost shell]$ ls -l
total 8
-rw-rw-r-- 1 zdns zdns 367 Oct 23 15:11 test.lua
-rw-rw-r-- 1 zdns zdns 41 Oct 23 14:55 test.sh
[xxxx@localhost shell]$
shell脚本
shell脚本名称:test.sh
shell脚本内容:
#!/usr/bin/sh
echo "hello world"
exit 1
执行shell脚本:sh test.sh
执行结果:
[xxxx@localhost shell]$ sh test.sh
hello world
[xxxx@localhost shell]$ echo $?
1
[xxxx@localhost shell]$
lua脚本
lua脚本名称:test.lua
lua脚本内容:
function luaExecute(shellFile)
local ret = os.execute("sh "..shellFile)
print("luaExecute shell("..shellFile.."). ret:"..ret)
end
function luaPipe(shellFile)
local cmd = "sh "..shellFile
local t = io.popen("sh "..shellFile)
local ret = t:read()
print("luaPipe shell("..shellFile.."). ret:"..ret)
end
luaExecute("test.sh")
luaPipe("test.sh")
执行lua脚本:lua test.lua
执行lua脚本结果:
[xxxx@localhost shell]$ lua test.lua
hello world
luaExecute shell(test.sh). ret:256
luaPipe shell(test.sh). ret:hello world
[xxxx@localhost shell]$
lua函数说明
os.execute
原型: os.execute ([command])
解释: 这个函数相当于C语言中的system(),我们可以看到这个函数有一个缺省的参数command,这个函数就是解析command再来通过的系统来调用解析的结果,它会返回一个依赖于操作系统的状态码。当参数缺省时,如果操作系统可以调用解析参数则返回非0的数,否则返回0。
io.popen
原型: io.popen ([prog [, mode]])
解释: 在额外的进程中启动程序prog,并返回用于prog的文件句柄。通俗的来说就是使用这个函数可以调用一个命令(程序),并且返回一个和这个程序相关的文件描述符,一般是这个被调用函数的输出结果,这个文件打开模式由参数mode确定,有取值"r"和"w"两种,分别表示以读、写方式打开,默认是以读的方式。
io.read
原型: io.read(…)
解释: 从文件中读取内容,还有另一种写法就是file:read(),而题目中的写法相当于对标准输入文件的操作,也就是io.input():read()。这个函数会根据所给的格式来读取内容内容,如果读不到所指定格式的内容则会返回nil,如果不指定读取的格式,则函数会选择l做为默认的形式读取。
其中可选的读取格式有:
“n”:读取一个数字,这是唯一返回数字而不是字符串的读取格式。
“a”:从当前位置读取余下的所有内容,如果在文件尾,则返回空串""。
“l”:读取下一个行内容,如果在文件尾部则会返回nil。
number:读取number个字符的字符串,如果在文件尾则会返回nil,如果吧number=0,则这个函数不会读取任何内容而返回一个空串"",在文件尾返回nil。