• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Launch External Script CMD/BAT in lua module (1 Viewer)

tecno214

Newcomer
Joined
Apr 10, 2021
Messages
3
Reaction score
0
Good evening..

One question..
There is the possibility to run an external script inside a lua module?
I view the os.execute command but i don't know the perfect usage of this function....

Thank You in advance for your response at my question..
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
93
Reaction score
32
Location
Victoria BC
Website
winlua.net
Lua:
local sf = string.format --~I hate typing string.format

local function run_cmd(cmd, param_string)
    command_str = sf("%s %s", cmd, param_string or "")
    print("Running:",command_str)
    local result = os.execute(command_str)

    return result
end


print("Result: ", run_cmd("echo", "Hello World"))
print("Result: ",run_cmd("dir", "c:\temp"))
print("Result: ",run_cmd("powershell", '"ls c:/temp"'))

os.execute does not give you access to the output of the command, it only give you a success or failure. If you want to manipulate the output of the command (e.g. store it in a string for later), you need to use io.popen:


Lua:
local function mangle_output(cmd, param_string)
    command_str = sf("%s %s", cmd, param_string or "")
    print("Running:",command_str) 
    
    local handle = io.popen(command_str)
    local match = handle:read("*a")
    match = match:gsub("0", "X")
    return match
end


print("Result: ",mangle_output("dir", "c:\\temp"))
print("Result: ",mangle_output("powershell", '"ls c:/temp"'))

This is a simplistic use of io.popen. You can read it line by line as well. Check out the lua reference on file:read.

In Windows, you can't interact with commands in Lua (e.g. read, write, read, write). In Linux or BSD there are a couple of tools, but it's not straightforward.

Here is a script I wrote a while ago that uses powershell functions to run commands and fetch/install luarocks. It doesn't work correctly anymore (WTH???) but it does run. Check out the function return_shell_output, it gets the shell output and then matches a pattern in the output (e.g. looks for a result). Return_shell_output2 does the same thing but with CMD. [Note: The code is a little gnarly.]

Code:
 lua .\fetch-luarocks.lua -u

Finally, I would recommend you grab a copy of the Programming In Lua book. It's really good and it's very easy to read. I found it fills in the holes that learning Lua on the internet leaves.

Helps?
 

Attachments

  • fetch-luarocks.lua
    10.3 KB · Views: 0

tecno214

Newcomer
Joined
Apr 10, 2021
Messages
3
Reaction score
0
Thank you for the explanation.
Therefore it is better to use the io.popen command.

Question: what is the script patch to run in the example above ..
For example if I have to run this script

c: \ temp \ test.cmd

How do I apply it to the lua language?
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
93
Reaction score
32
Location
Victoria BC
Website
winlua.net
Lua:
local sf = string.format --~I hate typing string.format

--This function isn't used, it's here for reference
local function run_cmd(cmd, param_string)
    command_str = sf("%s %s", cmd, param_string or "")
    print("Running with os.execute:",command_str)
    local result = os.execute(command_str)

    return result
end

local function mangle_output(cmd, param_string)
    command_str = sf("%s %s", cmd, param_string or "")
    print("Running:",command_str)
   
    local handle = io.popen(command_str)
    local match = handle:read("*a")
    match = match:gsub("0", "X")
    return match
end

--Check if there is anything to run
if #arg > 0 then
    -- concatinate the arg table containing all the arguments to our script,
    -- then run it through our mangler.
    local result = mangle_output(table.concat(arg, " "))
    if result then
        print(result)
    else
        print("no result?")
    end
else

    print("please provide at least one command.")
end

Save this as mangler.lua. Then call
Code:
lua mangler.lua c:\\temp\\test.cmd param1 param2

If you use single backslash in windows, you may wind up with unexpected behavior if you manipulate the strings in lua. Use double backslash or a single forward(CMD understand forward slash, some programs don't).

This lua script can also be wrapped in a bat or ps1 script for "ease of use".

If you need a lua installation, there are two options at winlua.net (lua only or complete toolchain including luarocks)
 
Top