Skip to main content
Post Undeleted by SquareOne
added 1040 characters in body
Source Link
SquareOne
  • 7.7k
  • 1
  • 17
  • 36

The form I am able to use both, RunProcess[$SystemShell, prop, input]RunProcess and StartProcess, to execute a built-in shell command. At least it works for me on OSX, with zsh or sh shell.

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

Let's create this file in your home directory and name it for example "myalias"First, let's choose our shell :

#!myshell = "/bin/bashzsh"; (* or "/bin/sh" *) 

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

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

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

Export["~/file2source", myls='ls'code, "String"]; 

Then in MMAThese are different ways to source the script in the file:

1.

RunProcess[$SystemShellRunProcess[myshell, "StandardOutput", "source ~/myaliasfile2source ; alias"]hi "] 

Hello World

returns2.1

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

myls='ls'Hello World

which shows2.2

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

Hello World

Comment: It seems there is a problem with the file has been sourcedapproach 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.

The form RunProcess[$SystemShell, prop, input] works for me.

See this example:

Let's create this file in your home directory and name it for example "myalias":

#! /bin/bash alias myls='ls' 

Then in MMA :

RunProcess[$SystemShell, "StandardOutput", "source ~/myalias ; alias"] 

returns

myls='ls'

which shows the file has been sourced !

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.

Post Deleted by SquareOne
Source Link
SquareOne
  • 7.7k
  • 1
  • 17
  • 36

The form RunProcess[$SystemShell, prop, input] works for me.

See this example:

Let's create this file in your home directory and name it for example "myalias":

#! /bin/bash alias myls='ls' 

Then in MMA :

RunProcess[$SystemShell, "StandardOutput", "source ~/myalias ; alias"] 

returns

myls='ls'

which shows the file has been sourced !