bash pipes - I am trying to call script from c#

2019-02-28 20:56发布

问题:

I have a cygwin bash scripts that works:

#!/bin/sh
cd myc
cp Stats.txt Stats.txt.cpy;
cat Stats.txt.cpy | sort -n -k1 | gawk '{sum+=$2; print $0,sum}' > Stats.txt

I want to "call" it from C#:

string cmd="myscript.sh";
System.Diagnostics.Process proc = new System.Diagnostics.Process();

System.Diagnostics.ProcessStartInfo psi =
    new System.Diagnostics.ProcessStartInfo(@"C:\Cygwin\bin\bash.exe");
psi.Arguments = cmd;
psi.WorkingDirectory = "C:\\cygwin\\home\\Moon";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
proc.StartInfo = psi;

proc.Start();
string error = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
this.textBox1.AppendText(error);
this.textBox1.AppendText(output);

It works fine from cygwin terminal BUT from C# I get:

Input file specified two times.

I suspect this is a pipes thing - can anyone help?

回答1:

It was a path issue.

You need to set path in the script - otherwise it uses a different "non-cygwin" path and gets wrong commands.



标签: c# cygwin pipe