将 bash 输出分配给 .lua 脚本中的变量
Assign bash output to variable in .lua script
我想将 bash
命令的输出分配给 .lua
脚本中的变量。可能吗?
例如,类似于:
var = `ps uax | grep myprocess`
是的,您需要为此使用 io.popen
。
io.popen (prog [, mode])
Starts program prog in a separated process and returns a file handle that you can use to read data from this program (if mode is "r", the default) or to write data to this program (if mode is "w").
This function is system dependent and is not available on all platforms.
另见 How to execute an external command?。
io.popen calls a command but returns a file object so you can read the output of the command, if the second argument is 'r', but you can also pass input to a command with a second argument of 'w'. Unfortunately, you don't get a io.popen2, and you don't get the return code.
我想将 bash
命令的输出分配给 .lua
脚本中的变量。可能吗?
例如,类似于:
var = `ps uax | grep myprocess`
是的,您需要为此使用 io.popen
。
io.popen (prog [, mode])
Starts program prog in a separated process and returns a file handle that you can use to read data from this program (if mode is "r", the default) or to write data to this program (if mode is "w").
This function is system dependent and is not available on all platforms.
另见 How to execute an external command?。
io.popen calls a command but returns a file object so you can read the output of the command, if the second argument is 'r', but you can also pass input to a command with a second argument of 'w'. Unfortunately, you don't get a io.popen2, and you don't get the return code.