How to run a shell script from C # on OSX?

I would like to use C # to execute a shell script. Based on such questions, I came up with a solution that looks like this.

System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app","sunflow/sunflow.sh"); 

At the moment, Terminal is opening, then the shell file with the default application is opening (Xcode in my case). Changing the default application is not an option, as this application will need to be installed for other users.

Ideally, the solution will allow arguments for the shell file.

+4
source share
1 answer

I cannot test a Mac right away, but the following code works on Linux and should work on a Mac because Mono is very close to the main Microsoft.NET interfaces:

 ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "foo/bar.sh", Arguments = "arg1 arg2 arg3", }; Process proc = new Process() { StartInfo = startInfo, }; proc.Start(); 

A few notes about my environment:

  • I created a test directory specifically to test this code.
  • I created the bar.sh file in the foo subdirectory with the following code:

     #!/bin/sh for arg in $* do echo $arg done 
  • I wrapped the Main method around the C # code above in Test.cs and compiled with dmcs Test.cs and executed using mono Test.exe .

  • The end result is "arg1 arg2 arg3", with the three tokens separated by newlines
+7
source

Source: https://habr.com/ru/post/1432751/


All Articles