Message Boards Message Boards

0
|
2583 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

[?] Pass string to << for executing external command?

Posted 7 years ago

Hi there,

I'm having trouble passing a string containing a valid command using <<, e.g.:

In[170]:= <<"!echo hello world"                                                                            

Out[170]= hello world

In[171]:= myCmdStr = "\"!echo hello world\""                                                               

Out[171]= "!echo hello world"

In[172]:= <<myCmdStr                                                                                       

Get::noopen: Cannot open myCmdStr.

Out[172]= $Failed

Is there a way to make this work? I would like to assemble a string with configurable parameters as input to an external command, execute and then get the return value. Perhaps there is a more elegant solution?

Thanks in advance!

POSTED BY: johnty wang
4 Replies

Ah, got it! (misunderstood the intent of the one example with "All" above, which only returns the exit code :)

Thanks again!

POSTED BY: johnty wang

Use RunProcess:

RunProcess["echo", All, "hello world"]
POSTED BY: Sander Huisman

great, thanks!

p.s. I believe the exact syntax is (for the "all" case):

RunProcess[{"echo","hello world"}]

and

RunProcess[{"echo","hello world"}, "StandardOutput"] 

for just the output itself.

Thanks again! :)

POSTED BY: johnty wang

That was just an example from the documentation. You can also do it like this:

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

giving:

<|"ExitCode" -> 0, "StandardOutput" -> "example line 1
  example line 2
  ", "StandardError" -> ""|>
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract