Running External Program - getting the programs results not just the stdout

Hi,

I have managed to confuse myself but I am sure there is a simple answer.
I have a c++ script I can call this in the terminal with:

./script $InputFile > $OutputFilePathAndName

With the terminal printing the stdout but also saving the c++ programs results to the file described in OutputFilePathAndName
In Julia if I write something like:

TerminalLine="./script $InputFile > $OutputFilePathAndName"
TerminalLine=split("$TerminalLine")
run(`$TerminalLine`);

The file OutputFilePathAndName never writes? In a simple terminal this writes correctly but not using Julia ā€˜run’ or Julias shell.
In windows I got around this using:

TerminalLine="./script $InputFile > $OutputFilePathAndName"
TerminalLine=split("powershell $TerminalLine")
run(`$TerminalLine`);

i.e. calling powershell as a subprocess.
Any ideas what I need to do in Linux?

Cheers,
Tim

I think this might be right for you:

pipeline(`do_work`, stdout=pipeline(`sort`, "out.txt"))

That way it should be possible to pipe the output of the program where you want it.

No, I thought this might be too and tried this earlier. This just returns the terminal output in the text file, not the output from the program. I run this as so:

run(pipeline(`./script`,stdout=pipeline(`sort`,"$OutputFilePathAndName"))  );

I think you don’t need the sort or the second pipeline here, that is just from the example in the documentation. I also assume that OutputFilePathAndName is a string so "$OutputFilePathAndName" == OutputFilePathAndName

But I’m a little confused. If you call just the program from the shell, isn’t all output shown there? If so, the stdout parameter should catch that. If not, where is it going instead? What is the difference between ā€œterminal outputā€ and ā€œoutput from the programā€?

I have spotted a mistake in my code.Your answer made me go back and double check this (and it works).
Cheers for the advice.
Tim

1 Like