6
$\begingroup$

System: Windows 10 professional. Mathematica Version: 10.3.0 English.

RunProcess doesn't work in my computer as described in tutorial.

enter image description here

The right result the function should give: enter image description here

I tried different RunProcess examples given in the documentation and it seems that most of them work on Windows not as expected. For instance:

RunProcess[$SystemShell, All, "echo example line 1
 echo example line 2
 exit
 "]

Outcome as follows:

<|"ExitCode" -> 0, 
 "StandardOutput" -> 
  "Microsoft Windows [\[Degree]æ\[PlusMinus]¾ 10.0.10586]

  (c) 2015 Microsoft Corporation¡£\[PlusMinus]Some text...

  C:\Users\Veya\Documents>echo example line 1
  example line 1

  C:\Users\Veya\Documents>echo example line 2
  example line 2

  C:\Users\Veya\Documents>exit
  ", "StandardError" -> ""|>

And it should be shorter:

<|"ExitCode" -> 0, "StandardOutput" -> "example line 1
  example line 2
  ", "StandardError" -> ""|>

Question: What am I doing wrong? Are problems due to my Windows settings? How can I get results looking as described in the documentation?


Why I need RunProcess

I want to use MaTex in Mathematica; it is based on RunProcess.


Addition:

I've also directly tried Environment command from documentation but get $Failed.

 Environment["HOME"]
> $Failed
$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Nov 27 '15 at 13:00
  • $\begingroup$ Thank you for your advice. I will keep it in mind. $\endgroup$ – Veya.Zhang Nov 28 '15 at 16:09
  • $\begingroup$ @Veya, did you see possible issues in documentation? i.e. optionC = If[StringFreeQ[$System, "Windows"], "-c", "/c"]; RunProcess[{$SystemShell, optionC, "echo example line"}] $\endgroup$ – garej Nov 28 '15 at 20:50
  • $\begingroup$ @garej I have tried your code in my computer and get a result like this:<|"ExitCode" -> 0, "StandardOutput" -> "example line ", "StandardError" -> ""|> It seems like a right result. But I don't know what it means? $\endgroup$ – Veya.Zhang Nov 29 '15 at 2:40
  • $\begingroup$ @garel I have tracked clew into function named Environment. This code didn't work either.(Input Environment["HOME"] Output $Failed) Maybe it is the source of problem. But I also don't know how to fix it. $\endgroup$ – Veya.Zhang Nov 29 '15 at 10:09
7
$\begingroup$

Your RunProcess works properly in all examples and System settings are fine. As @Oleksandr R. has admitted, date is a shell builtin on Windows. So there are commands date /t and time /t in cmd Windows shell that can be used instead.

In Mathematica for Windows:

$SystemShell == "cmd"
(* True *)

So something like:

RunProcess[{"cmd", "/c", "date /t&&time /t"}]

gives

<|"ExitCode" -> 0, "StandardOutput" -> "30.11.2015 

23:17

", "StandardError" -> ""|>

Compare:

enter image description here


Your code from documentation

RunProcess[$SystemShell, All, "echo example line 1
 echo example line 2
 exit
 "]

also gave you the right output (as it looks in Windows cmd shell). Just compare your output with the screen:

enter image description here

(I've used Win+R and cmd /k cls to avoid information about OS.)

If you want to avoid intermediate lines from shell that seem puzzled you, try something similar to this:

RunProcess[{"cmd", "/k", 
  "cls&&echo example line 1&&echo example line 2&&exit"}]

or this (note, that key /k needs exit to stop RunProcess):

RunProcess[{"cmd", "/c", 
  "cls&&echo example line 1&&echo example line 2"}]
<|"ExitCode" -> 0, "StandardOutput" -> " 

example line 1

example line 2

", "StandardError" -> ""|>

Concerning addition (most likely):

Environment["HOMEPATH"]
\Users\Acer

Note that RunProcess[{'*command*', ...}] in the command place expects binary file which is visible (by explicit path or Environment). To see all Environment variables one may use

First /@ GetEnvironment[]
$\endgroup$
  • 2
    $\begingroup$ Good answer; +1. However, I just wanted to make it explicit: OP's first example didn't work because date is a shell builtin on Windows. I'm sure you know this, but it is not mentioned anywhere in the comments or answer, so I include it here for completeness. $\endgroup$ – Oleksandr R. Nov 30 '15 at 23:28
  • $\begingroup$ @OleksandrR., thank you, I've added your suggestion and refined the answer. $\endgroup$ – garej Dec 1 '15 at 6:06
  • $\begingroup$ @garej and OleksandrR .Thank you.I have solved my problem totally! $\endgroup$ – Veya.Zhang Dec 1 '15 at 14:49

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