Introduction
When you use os.execute() to run external commands using you get numeric return codes which are not very helpful. To overcome this issue we have created a module that you can use to provide more descriptive return codes.
There are two versions of this module one Windows systems, and another for Posix systems (Linux, Unix, Mac etc). The only difference between Windows and Posix is a different mapping for the os.execute()
return codes (because Windows and Posix return different numeric codes from os.execute
).
There are two options:
- Map the
os.execute
numeric return codes to meaningful descriptive text - Return the output from the command/script/program that you are running
Task [top]
How to return descriptive codes for the os.execute()
command.
Implementation [top]
The repository example code below will return more descriptive returns or return the value of command execution.
Note: The screenshots shown are for Windows — the Posix versions are very similar.
- Windows: Import the Run a program under Windows channel from the builtin Iguana Files repository.
- Alternatively you can paste the Run a program under Windows code into the main module (main.lua).
- Posix: Import the Run a program on POSIX channel from the builtin Iguana Files repository.
- Alternatively you can paste the Run a program on POSIX code into the main module (main.lua).
- Review the main.lua and investigate how the Execute() and ExecuteWithOutput() functions are used in the
main()
function.- The
Execute()
function maps theos.execute()
return code numbers to meaningful messages:
Note: These are the Windows code mappings — Posix uses different mappings. - Whereas the
ExecuteWithOutput()
function returns the output from the command:
- The
Note: The two example functions are included in the main module (main.lua) for simplicity of presentation. When you are using these functions in your code you should place them in a local or shared module.
How it works [top]
Running the functions in the module for calling command line scripts will give you more descriptive return code. There are two versions of the module: One for Windows and one for Posix, the only difference is in the mapping of the os.execute()
return codes.
There are two functions:
- Execute(): This function wraps
os.execute()
and maps the return to descriptive messages. - ExecuteWithOutput(): This function wraps
io
and returns the output from the command/script/program that you are running..popen()