Running Bash Commands from C#

2019-07-19 15:53发布

I am trying to figure out how to run a bash command from C# running on IIS 7/.Net 4.5.

I've been searching the web and a lot of answers presume you have certain things installed/in place.

I already have Git 1.9.4.msysgit.2 installed with Git Bash and Git Giu. I'm looking for some help as to what else I need installed to run even the simplest of bash commands. And how to run it.

I've looked at posts like bash pipes - I am trying to call script from c# but that uses cygwin. Can I do the same without it and if so, how do I go about it?

Goal

If what I'm asking above doesn't make sense or seems to ask separate questions, here my ultimate goal. I'm trying to write my own server-side git hook. When a developer pushes their commits to our GitHub repo, I want GitHub to call our callback url. I want my callback url to run a git pull command to update our staging server with what was just pushed.

I got to this question based on a previous question I asked at GitHub - setup auto deployment with remote server. based on answers there I'm trying to run a simple command, either but hard coding the command, or putting it in a script and running it, e.g.: cd $REPO_DIR && git pull origin $branch_name.

I am aware of Jenkins and other software, but I want to perform these commands myself vs. installing another software.

If further information is needed please feel free to ask.

Update 1

So based on a few answers below I've come up with the following

using System.Diagnostics;

Process process = new Process();

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName = @"C:\Program Files (x86)\Git\bin\bash.exe";
processStartInfo.WorkingDirectory = @"C:\myrepo\mysite";
processStartInfo.Arguments = "git status";
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;

process.StartInfo = processStartInfo;
process.Start();

String error = process.StandardError.ReadToEnd();
String output = process.StandardOutput.ReadToEnd();

ViewBag.Error = error;
ViewBag.Ouput = output;

With the code above I am getting "C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory. I know the exe is there. What's am I doing wrong?

Update 2

As per @SurgeonofDeath comment I followed this post http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/ and added the paths of Git to my environmental variables. However I still am getting the same issues. Any ideas?

Thanks.

标签: c# git bash shell
3条回答
霸刀☆藐视天下
2楼-- · 2019-07-19 16:27

Instead of calling the bash.exe, simply call git and pass the status as argument:

processStartInfo.FileName = "git";
processStartInfo.Arguments = "status";
查看更多
干净又极端
3楼-- · 2019-07-19 16:36

perhaps i misunderstood your question but what about execve? here is an excerpt of it's man page.

NAME

   execve - execute program

SYNOPSIS

   #include <unistd.h>

   int execve(const char *filename, char *const argv[],
              char *const envp[]);

DESCRIPTION

   execve() executes the program pointed to by filename.  filename must > be
   either a binary executable, or a script starting with  a  line  of  > the
   form:

       #! interpreter [optional-arg]
查看更多
forever°为你锁心
4楼-- · 2019-07-19 16:44

Check your PATH environment variable and update it

C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory

means that it's git which is not found by bash.

1. Check the PATH environment variable in bash (which is and should remain different from Windows one)

Adjust this

processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

to make the terminal visible.

In the terminal you will create with the Process.Start()

Type:

echo ${PATH}

2. Update your path

  • You could update the global path of windows (which requires a restart)
  • You could update the user path of windows (which should require a logoff, but I'm not sure).
  • You just set Path to what you like with System.Environment.SetEnvironmentVariable before starting the Process

Additional note:

If you have like me several versions of bash, command interpreters, git and so on, it could be really messy if you try to concatenate all the paths and hope to find the ideal order. You could see some weird behavior of you beloved commands until you realize it's not the one you intend to run ... think of FIND.exe... And I didn't even think of the user-friendly interface of windows to edit environment variables ...

查看更多
登录 后发表回答