6
$\begingroup$

I'm running a zsh shell:

$SystemShell = "/bin/zsh"

My problem is that some basic system-level commands don't work in RunProcess. For example, echo works but exec, source, and many other build it shell commands don't:

enter image description here

It gives an error:

RunProcess::pnfd: Program source not found.  Check Environment["PATH"]. >>

The docs say that by default, the subprocess will inherit its process directory and environment variables from the Wolfram Engine, so I tried setting the ProcessEnvironment with an association to match my environment vars in the terminal. But that didn't work.

What am I doing wrong here, shouldn't all system level shell commands be accessible?

$\endgroup$
0

2 Answers 2

7
$\begingroup$

I am able to use both, RunProcess and StartProcess, to execute a built-in shell command. At least it works for me on OSX, with zsh or sh shell.

Here is a minimal example with the built-in command source (like in the OP's question).

First, let's choose our shell :

myshell = "/bin/zsh"; (* or "/bin/sh" *)

This is some trivial shell script we'd like to source:

code = "#! " <> myshell <> "\nhi () {echo \"Hello World\";}"
"#! /bin/zsh
hi () {echo \"Hello World\";}"

that we can export directly to an external file (inside for example home directory):

Export["~/file2source", code, "String"];

These are different ways to source the script in the file:

1.

RunProcess[myshell, "StandardOutput", "source ~/file2source ; hi "]

Hello World

2.1

process = StartProcess[myshell];
WriteLine[process, "source ~/file2source  ; hi"];
ReadLine[process]
WriteLine[process, "exit"];

Hello World

2.2

process = StartProcess[myshell];
WriteLine[process, "source ~/file2source ; hi ; exit"];
ReadString[process]

Hello World

Comment: It seems there is a problem with the approach consisting in reading the buffer (like ReadString[process, EndOfBuffer]) when the command is contained in a notebook cell with other commands ... I'll explain that in a different post.

$\endgroup$
6
$\begingroup$

RunProcess can only run commands that are separate binaries, and commands like source and exec are built into the shell, not independent of it. You could execute them by sending them to a running zsh process through a process object returned by StartProcess[$SystemShell].

$\endgroup$
5
  • $\begingroup$ Thanks, but how can I run a process within a process. I'm trying to launch an ipython shell inside a virtualenv within the zsh shell. $\endgroup$
    – M.R.
    Nov 1, 2015 at 23:23
  • $\begingroup$ @M.R. Does that work : RunProcess[$SystemShell, "StandardOutput", "source yourfilepath"] $\endgroup$
    – SquareOne
    Nov 1, 2015 at 23:29
  • $\begingroup$ @SquareOne No, it gives me this: during evaluation of In[126]:= OpenRead::noopen: Cannot open ZSH-ENV... ZSH-RC.... >> Out[127]= $Failed $\endgroup$
    – M.R.
    Nov 1, 2015 at 23:46
  • 1
    $\begingroup$ @SquareOne Try it with StartProcess and then try to run another command using WriteLine, it doesn't work... $\endgroup$
    – M.R.
    Nov 2, 2015 at 0:09
  • 1
    $\begingroup$ @M.R. No, actually I got it work with StartProcess but not with RunProcess ... ?? $\endgroup$
    – SquareOne
    Nov 2, 2015 at 0:21

Your Answer

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

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