1

I have a txt file called exec.txt, that I have made executable using chmod. Running it from the shell using ./exec.txt works correctly. However, I need to run it from my Mathematica nb file.

Following is what I tried:

Run[NotebookDirectory[] <> "exec.txt"]

This throws me an output of $0$ in the nb (which is supposedly what I should observe if the code worked properly), but I don't get the desired output from the executable. Both the executable and the nb are in the same directory.

However, opening Mathematica kernel from the shell (after entering the directory of nb and executable) and then trying the following command provides the desired result:

Run[Directory[] <> "/exec.txt"]

What should I do to get the desired result from my nb file?

Details about exec.txt:

It contains just the following line:

tr '{}' '[]' <fileABC.dat | tee fileABC.dat

which simply replaces all curly brackets in the dat file with square ones. This is what I mean while saying "desired result".

abstract
  • 203
  • 1
  • 7
  • Where do you expect the output to go? – John Doty Sep 05 '21 at 12:09
  • @JohnDoty, I expect to observe the same behaviour in the nb as observed in the Mathematica kernel from the shell. The kernel output simply displays the content in the dat file with all curly brackets replaced by square ones and then throws an output of 0. The output of the code in the executable is stored back into the dat file. – abstract Sep 05 '21 at 12:19
  • Related: https://mathematica.stackexchange.com/q/18548/4999 – Michael E2 Oct 05 '21 at 13:59

1 Answers1

2

To capture the standard output of an external command, use Get with the command preceded by !:

<< "!whoami|tr a-z A-Z"
(* JPD *)
John Doty
  • 13,712
  • 1
  • 22
  • 42
  • Thanks for the response. I eventually figured out the solution from the discussion in https://mathematica.stackexchange.com/questions/109435/how-to-run-a-shell-script-from-inside-mathematica?rq=1

    Just added SetDirectory[NotebookDirectory[]] at the top of my nb, and it worked properly. Used Run["./exec.txt"] as the command.

    – abstract Sep 05 '21 at 13:04