从C#运行bash命令 [英] Running Bash Commands from C#

查看:370
本文介绍了从C#运行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何运行C#bash命令在IIS 7 / .NET 4.5上运行。

我一直在寻找的地方网络和你已经安装了某些事情很多答案presume的/

我已经有的Git 1.9.4.msysgit.2 使用Git Bash和Git的GIU安装。我在寻找一些帮助,以别的什么,我需要安装到运行,即使是最简单的bash命令。以及如何运行它。

我看着像<一个职位href=\"http://stackoverflow.com/questions/15586424/bash-pipes-i-am-trying-to-call-script-from-c-sharp\">bash管道 - 我想打电话从C#的脚本,但使用的cygwin 。我可以这样做没有它,如果是这样,我怎么做呢?

目标

如果我问什么上面没有意义或似乎要问不同的问题,在这里我的终极目标。我试图写我自己的服务器端git的钩子。当开发人员其提交给我们的GitHub库,我想GitHub上调用我们的回调URL。我希望我的回调URL运行 git的拉命令来更新我们的上演什么只是推服务器。

我对这个问题的基础上一个previous问题,我问在<一个href=\"http://stackoverflow.com/questions/28485281/github-setup-auto-deployment-with-remote-server/28485470?noredirect=1#comment45293201_28485470\">GitHub - 设置自动部署与远程服务器。基于答案还有我想运行一个简单的命令,但是无论是硬编码的命令,或把它在一个脚本并运行它,例如: CD $ REPO_DIR&放大器;&安培;混帐拉出身$ branch_name

我知道詹金斯和其他软件的,但我想执行这些命令自己与安装其他软件。

如果需要进一步信息,请随时问。

更新1

因此​​,基于一些答案,下面我来了以下

 使用System.Diagnostics程序;工艺过程=新工艺();的ProcessStartInfo的ProcessStartInfo =新的ProcessStartInfo();
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName = @C:\\ Program Files文件(x86)的\\ Git的\\ BIN \\ bash.exe
processStartInfo.WorkingDirectory = @C:\\ myrepo \\ mysite的;
processStartInfo.Arguments =混帐地位;
processStartInfo.RedirectStandardOutput =真;
processStartInfo.RedirectStandardError = TRUE;
processStartInfo.UseShellExecute = FALSE;process.StartInfo =的ProcessStartInfo;
的Process.Start();字符串错误= process.StandardError.ReadToEnd();
字符串输出= process.StandardOutput.ReadToEnd();ViewBag.Error =错误;
ViewBag.Ouput =输出;

随着code上面我得到C:/ Program Files文件(x86)的/Git/bin/bash.exe:混帐:没有这样的文件或目录。我知道EXE是存在的。什么是我做错了?

更新2

按@SurgeonofDeath评论我跟着这个帖子的 http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/ 并添加Git的的路径,以我的环境变量。不过,我还是我得到了同样的问题。任何想法?

感谢。


解决方案

也许我误解了你的问题,但关于的execve 是什么?
这里是它的一个节选的手册页。


  

名称

 的execve  - 执行程序


  
  

概要

 的#include&LT;&unistd.h中GT;   INT的execve(为const char *文件名,char * const的ARGV [],
              char * const的envp []);


  
  

说明

 的execve()执行程序中通过文件名指向。文件名必须&GT;是
   无论是二进制可执行或脚本开始一个线的制造&gt;该
   形成:       #!除preTER [可选参数]


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.

解决方案

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]

这篇关于从C#运行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆