简体   繁体   English

如何在 C# 中使用 awk 命令

[英]How to use Awk command in C#

public class CmdHelper
{
    public static string StartCmd(string commandLine)
    {
    commandLine = commandLine.Trim().TrimStart('&') + "&exit";
    string outputMsg = "";
    Process pro = new Process();
    pro.StartInfo.FileName = "cmd.exe";
    pro.StartInfo.UseShellExecute = false;
    pro.StartInfo.RedirectStandardError = true;
    pro.StartInfo.RedirectStandardInput = true;
    pro.StartInfo.RedirectStandardOutput = true;
    pro.StartInfo.CreateNoWindow = false;
    pro.Start();
    pro.StandardInput.WriteLine(commandLine);
    pro.StandardInput.AutoFlush = true;
    pro.StartInfo.StandardErrorEncoding = Encoding.UTF8;
    pro.StartInfo.StandardOutputEncoding = Encoding.UTF8;
    outputMsg += pro.StandardOutput.ReadToEnd();

    pro.WaitForExit();
    pro.Close();

    return outputMsg;
}

public static void CommitBundleToSvn()
{
    string folderPath = EditorConst.BundlesPath + VEngine.Editor.Builds.Settings.GetPlatformName();
    string command = "svn status " + folderPath;
    string regaxPattern = ".*&exit";
    string output = "";
    string[] regexRes = {};
    command = "svn st " + folderPath+ " | awk \"{if ($1 == \\\"?\\\") {print $2} }\""; 
    output = CmdHelper.StartCmd(command);
    UnityEngine.Debug.LogError(command);
    UnityEngine.Debug.LogError(output);
}

Execute command:执行命令:

("svn st " + folderPath+ " | awk \"{if ($1 == \\\"?\\\") {print $2} }\"") 

In windows cmd , cmd could get return message.windows cmd中, cmd 可以得到返回消息。

Now I call the CommitBundleToSvn().现在我调用 CommitBundleToSvn()。 but the cmd will not exit.但 cmd 不会退出。

If the command is "svn status " + folderPath, it is expected.如果命令是“svn status” + folderPath,那么它是预期的。

Another option is to not use awk in this case, and do:另一种选择是在这种情况下不使用awk ,并执行以下操作:

command = "svn st " + folderPath+ ";
output = CmdHelper.StartCmd(command);
output = string.Join("\r\n", output.Split("\r\n",StringSplitOptions.RemoveEmptyEntries)
    .Where(x => x.Split(' ',StringSplitOptions.RemoveEmptyEntries)[0]
    .Contains("?"))
    .Select(s => s.Split(' ',StringSplitOptions.RemoveEmptyEntries)[1])
    .ToList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM