7

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.

11
  • Just to be clear, when you say "bash" do you mean a command line program? or do you actually want a unix bash shell to be called? Commented Feb 12, 2015 at 20:30
  • A better question I guess is if you can't just globally install ms git, so that it is callable from the normal command line? in that case, you could just use a normal invoke of a command line program and you wouldn't have to deal with the bash shells that ms git installs by default Commented Feb 12, 2015 at 20:32
  • @lukevp good questions. What lead me to this question was this previous question I asked stackoverflow.com/questions/28485281/…. Based on the answer I marked I would like to run a bash script command of cd $REPO_DIR && git pull origin branchname Commented Feb 12, 2015 at 20:37
  • Unfortunately I probably won't be much help as I use Jenkins to detect changes in our repos. Sorry! Commented Feb 12, 2015 at 20:39
  • XY problem... So you real question is "how to run executable with working folder set to particular location", but for some strange reason you want to do it via bash script (not CMD, not directly)... Please consider what your actual goal is. Commented Feb 12, 2015 at 20:41

3 Answers 3

2

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

processStartInfo.FileName = "git";
processStartInfo.Arguments = "status";
Sign up to request clarification or add additional context in comments.

Comments

0

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]

5 Comments

I am not sure why this was posted as an answer, I think this should be a comment since it is a question?
it is in answer because it fits the question title. you can run a command or a script from c using execve. the question is just because i am worried about the git stuff. please let @RoLYrolls leave a comment here to see if i am totally wrong (and that is why it is designed as a question:)
@EmilKakkau thanks for your answer. is this code that can be run from C#? it looks similar to PHP to me (but i could be wrong). Thanks!
@RoLYroLLs This actually looks very much like C. That was another reason why I am not sure if this could be an answer to this question. The tag indicates to C, I think there may be a confusion related to bash.
If it works with pure C there will definetly a pendent for C# but to figure that out use the search engine of your choice. i just had a short look and it seems that execve is available for C# too.
0

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 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.