5

I am trying to make a directory using this code to see if the code is executing but for some reason it executes with no error but the directory is never made. Is there and error in my code somewhere?

var startInfo = new 

var startinfo = new ProcessStartInfo();
startinfo.WorkingDirectory = "/home";

proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();

Console.WriteLine ("Shell has been executed!");
Console.ReadLine();
Brandon Williams
  • 103
  • 1
  • 1
  • 9
  • 1
    what's the working directory? – thumbmunkeys Apr 12 '14 at 10:39
  • My solution i stored on a thumb drive in a folder called projects if thats what you meant. – Brandon Williams Apr 12 '14 at 10:40
  • I assume that you're really trying to do something else (other than create a directory) in the end. Otherwise, it would seem that Directory.CreateDirectory(string) would be the better choice than going through the shell. – KevinS Apr 12 '14 at 11:25
  • Does Desktop exist under the /home directory? If so, why didn't you just set the WorkingDirectory to "/home/Desktop" and only execute the mkdir command? I feel like this is the XY problem: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – KevinS Apr 12 '14 at 11:28
  • I would like to execute a shell script that is saved on my desktop. – Brandon Williams Apr 12 '14 at 11:34
  • use an absolute path for "Desktop" – knocte Apr 12 '14 at 11:35

3 Answers3

16

This works best for me because now I do not have to worry about escaping quotes etc...

using System;
using System.Diagnostics;

class HelloWorld
{
    static void Main()
    {
        // lets say we want to run this command:    
        //  t=$(echo 'this is a test'); echo "$t" | grep -o 'is a'
        var output = ExecuteBashCommand("t=$(echo 'this is a test'); echo \"$t\" | grep -o 'is a'");

        // output the result
        Console.WriteLine(output);
    }

    static string ExecuteBashCommand(string command)
    {
        // according to: https://stackoverflow.com/a/15262019/637142
        // thans to this we will pass everything as one command
        command = command.Replace("\"","\"\"");

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash",
                Arguments = "-c \""+ command + "\"",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };

        proc.Start();
        proc.WaitForExit();

        return proc.StandardOutput.ReadToEnd();
    }
}
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • Thanks, providing a sample that already includes passing back the output helped me a lot! – friday Dec 02 '19 at 16:32
  • I found I needed to use `command = command.Replace("\"", "\\\"");` to get escaped quotes working properly. Besides that, though, this was an exceptionally helpful answer. Thank you kind human. – McHobbes Jan 14 '20 at 23:04
7

This works for me:

Process.Start("/bin/bash", "-c \"echo 'Hello World!'\"");
Alex Erygin
  • 3,161
  • 1
  • 22
  • 22
1

My guess is that your working directory is not where you expect it to be.

See here for more information on the working directory of Process.Start()

also your command seems wrong, use && to execute multiple commands:

  proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";

Thirdly you are setting your working directory wrongly:

 proc.StartInfo.WorkingDirectory = "/home";
Community
  • 1
  • 1
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110