I am used to using open3 to run commands in Ruby. Since there doesn't seem to be an equivalent lib in crystal-lang, I kludged up this:
def run_cmd(cmd, args)
stdout_str = IO::Memory.new
stderr_str = IO::Memory.new
result = [] of Int32 | String
status = Process.run(cmd, args: args, output: stdout_str, error: stderr_str)
if status.success?
result = [status.exit_code, "#{stdout_str}"]
else
result = [status.exit_code, "#{stderr_str}"]
end
stdout_str.close
stderr_str.close
result
end
cmd = "ping"
hostname = "my_host"
args = ["-c 2", "#{hostname}"]
result = run_cmd(cmd, args)
puts "ping: #{hostname}: Name or service not known" if result[0] != 0
Is there a better way to do this? Says the retired network specialist who is not a software developer exploring crystal-lang.
Thanks in advance for all advice.