Just to put this out there, I have added the following to some of my personal modules:
--- hs._asm.extras.exec(command[, with_user_env]) -> output, status, type, rc
--- Function
--- Runs a shell command and returns stdout as a string (may include a trailing newline), followed by true or nil indicating if the command completed successfully, the exit type ("exit" or "signal"), and the result code.
---
--- If `with_user_env` is `true`, then invoke the user's default shell as an interactive login shell in which to execute the provided command in order to make sure their setup files are properly evaluated so extra path and environment variables can be set. This is not done, if `with_user_env` is `false` or not provided, as it does add some overhead and is not always strictly necessary.
module.exec = function(command, user_env)
local f
if user_env then
f = io.popen(os.getenv("SHELL").." -l -i -c \""..command.."\"", 'r')
else
f = io.popen(command, 'r')
end
local s = f:read('*a')
local status, exit_type, rc = f:close()
return s, status, exit_type, rc
end
This more closely resembles os.execute() because it returns status, exit_type, and rc (result code) like exec does, but also the output of the invocation... this:
result = module.exec("which ls") will return the string "/bin/ls" and store it in result, or
result, status, exit_type, rc = module.exec("which ls") returns "/bin/ls", true, exit, 0 and stores each in the corresponding variable.
As an added bonus, you could do module.exec("which blueutil", true) which causes this version to wrap the command with your shell and the appropriate args to invoke your login scripts, thus gaining environment variable changes like to PATH. Eg:
> _asm.extras.exec("which blueutil")
nil exit 1
> _asm.extras.exec("which blueutil", true)
/usr/local/bin/blueutil
true exit 0
And I forget where it was asked, but applescript returns a table because at the time I ported it from Hydra, it either worked or it didn't and the results and/or error weren't always returned to Lua. I mean to go back and clean up the return values so it's more lua friendly at some time, but haven't gotten around to it yet... someone else is more than welcome, as this isn't currently a high priority on my list since it works well enough to troubleshoot and use for basic applescripting.