Lua os.execute()

作者: AlbertS | 来源:发表于2016-07-11 19:58 被阅读5465次

前言#

今天来看一个短小精悍而又强大的lua函数,这个函数虽然只有短短的几个字母,却涉及到了许多系统级的函数调用,使用方法也相当简单,接下来我们来看看例子。

内容#


os.execute##

  • 原型:os.execute ([command])
  • 解释:这个函数相当于C语言中的system(),我们可以看到这个函数有一个缺省的参数command,这个函数就是解析command再来通过的系统来调用解析的结果,它会返回一个依赖于操作系统的状态码。当参数缺省时,如果操作系统可以调用解析参数则返回非0的数,否则返回0。

Uasge##

  • 我们来新建一个文件命名为executetest.lua然后编写如下代码:
local ret = os.execute();

if ret ~= 0 then
    print("the system shell is available, ret = "..ret.."\n\n")
else
    print("the system shell is not available, ret = "..ret.."\n\n")
end

os.execute("color 02");
print("this is a test for os.execute\n");

local copyret = os.execute("copy " .."luatest.lua".. ",".."luatest.lua.bak")
print("copyret = "..copyret)

os.execute("pause");
  • 运行结果
execute.png execute2.png

总结#

  • 由示例可知,os.execute()使用真的非常方便,例如下面的两者是等价的Lua:os.execute ("pause") <==>c语言:system("pause")
  • 我们也可以使用os.execute("color 02");来将命令提示行的颜色由原来的白色改为绿色,注意这里改变的是所有的显示结果,而不是执行命令行之后的结果。

相关文章

网友评论

    本文标题:Lua os.execute()

    本文链接:https://www.haomeiwen.com/subject/nhljjttx.html