10
$\begingroup$

I have a very basic question. I have a shell file test.sh that prints let's say Yeah to a file called output.txt:

#!/bin/sh

echo "Yeah" > output.txt

I am running the Run command from the notebook:

Run["sh test.sh"]

But it is not creating the file output.txt. Can anyone tell me please what is going wrong?

$\endgroup$
7
  • 1
    $\begingroup$ It works for me, are you looking in the right directory? When I run it Run["sh test.sh"] I get an output 0, and the output.txt shows up in the same directory. $\endgroup$
    – Jason B.
    Commented Mar 8, 2016 at 16:21
  • $\begingroup$ Thank you Jason but it is a little strange. I am getting an output 0 only when I specify the entire path to test.sh (though both the notebook and test.sh are in the same directory). But even then I do not see output.txt in the same directory. So Run["sh /Users/sxm016/Documents/Alignment/Alignment_ras_sos/March_\ 2016_ras_Gauss_multivariate/Bistable_traj/test.sh"] gives me an output 0 but no file named output.txt in the same directory $\endgroup$
    – Sayak
    Commented Mar 8, 2016 at 16:28
  • $\begingroup$ That is strange. What is the output from Directory[]? Perhaps the output.txt file is going somewhere in Mathematica's path? $\endgroup$
    – Jason B.
    Commented Mar 8, 2016 at 16:32
  • $\begingroup$ Thank you so much. That is exactly what is happening. When I typed Directory[] it showed me the path to my home not the current directory. Thank you. Can you please tell me how to change the mathematica path ? $\endgroup$
    – Sayak
    Commented Mar 8, 2016 at 16:37
  • $\begingroup$ you want SetDirectory[]. I actually have a line in my /home/jason/.Mathematica/Kernel/init.m file to say SetDirectory["~/Documents/Mathematica"]; so that I always start in that directory $\endgroup$
    – Jason B.
    Commented Mar 8, 2016 at 16:39

1 Answer 1

12
$\begingroup$

The issue here is that when Mathematica executes an external system command, at least on Linux, it does so from the current directory. You can verify this via

Directory[]
Run["! echo $PWD > file.txt"];
FilePrint["file.txt"]

enter image description here

which is my default working directory. I actually have a line in my

/home/jason/.Mathematica/Kernel/init.m

file that says SetDirectory["~/Documents/Mathematica"]; so that I always start in that directory.

$\endgroup$
4
  • $\begingroup$ Well done, thank you! $\endgroup$
    – user9660
    Commented Mar 9, 2016 at 15:40
  • $\begingroup$ @Louis Absolutely, it's always an open question what to do when a question is answered in the comments but it doesn't get closed. $\endgroup$
    – Jason B.
    Commented Mar 9, 2016 at 19:18
  • $\begingroup$ Yeah, I did that and it works. Thanks Jason. You saved me time $\endgroup$
    – Sayak
    Commented Mar 10, 2016 at 16:15
  • $\begingroup$ @Sayak - Glad to help. If that answers the question you could mark it as solved, then it won't be in the list of unanswered questions. $\endgroup$
    – Jason B.
    Commented Mar 10, 2016 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.