Skip to content

Need help using shebang to alter program before executing it #1852

Answered by SquidDev
Zirunis asked this question in Q&A
Discussion options

You must be logged in to vote

For custom shebang programs, you don't probably don't need some of the fancier things shell does (like fancy error reporting), so can probably get away with something like this:

local function execute(path, contents)
  -- Shift arg left one, so replacing { [0] = "shebang-program", "program", "arg1" }
  -- with { [-1] = "shebang-program", [0] = "program", "arg1" }.
  local new_arg = {}
  local i = #arg while arg[i] do new_arg[i - 1] = arg[i] i = i - 1 end

  -- Create a new environment and require instance.
  -- You could probably overwrite _ENV.{arg,require,package}, rather than creating a new env,
  -- but this feels cleaner.
  local env = setmetatable({ arg = new_arg, shell = shell, mul…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Zirunis
Comment options

Answer selected by Zirunis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants