Andrii K.
Andrii K.

Reputation: 17

Call lua function from bash

I have a lua-script, named "program.lua". I start this script as 'lua /path/to/program.lua' and I get specific CLI provided by the script:

bash $ /usr/bin/lua /path/to/program.lua
Usage: 
  help   - help
  quit   - quit
  do_get - get data
script> _

In this script I have function 'do_get', and if I call this function from the program.lua's CLI, I've got the correct output:

bash $ /usr/bin/lua /path/to/program.lua
Usage: 
  help   - help
  quit   - quit
  do_get - get data
script> do_get
string1
string2
param1
param2

script> _

The question. How I can call function 'do_get' from bash and get the output I need without entering to script's CLI?

The script edit is disallowed.

Thank you

Upvotes: 0

Views: 1654

Answers (1)

pce
pce

Reputation: 5931

You can capture the output by calling the Lua Program in a shell-script and pipe the output to a variable:

lua_output=$(printf "do_get\n\n" | /usr/bin/lua /path/to/program.lua 2>&1)
 
echo $lua_output

If that does not work try a tool that can handle interactive prompts like yes (https://man7.org/linux/man-pages/man1/yes.1.html) yes do_get | /usr/bin/lua /path/to/program.lua or expect (https://man7.org/linux/man-pages/man1/expect.1.html)

Upvotes: 1

Related Questions