This is a quite basic but essential operation: Launch a command, and get its output in a string.
There are various solutions.
- One is to redirect the output to a temporary file, and then read its content. But this is not convenient, since it require disk access, and you can not always hide the process.
- The most advanced (and still simple) solution is to use the Eiffel Process library.
- Warning: you have to enable the multithreaded option to fully use the EiffelProcess pipe redirection.
- Warning: most of the time, you should precise absolute path to executable (such as "/bin/ls" instead of just "ls")
output_of_command (a_cmd, a_dir: STRING): STRING is -- Output of command `a_cmd' launched in directory `a_dir'. require cmd_attached: a_cmd /= Void dir_attached: a_dir /= Void local pf: PROCESS_FACTORY p: PROCESS retried: BOOLEAN do if not retried then last_error := 0 create Result.make (10) create pf p := pf.process_launcher_with_command_line (a_cmd, a_dir) p.set_hidden (True) p.set_separate_console (False) p.redirect_input_to_stream p.redirect_output_to_agent (agent (res: STRING; s: STRING) do if s /= Void then res.append_string (s) end end (Result, ?) ) p.launch p.wait_for_exit else last_error := 1 end rescue retried := True retry end