如何在C#中使用awk命令

发布于 2025-02-14 01:37:36 字数 1592 浏览 4 评论 0原文

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命令:

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

Windows CMD中,CMD可以获取返回消息。

现在,我称为commitbundletosvn()。但是CMD不会退出。

如果命令是“ SVN状态” + FolderPath,则可以预期。

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.

Now I call the CommitBundleToSvn(). but the cmd will not exit.

If the command is "svn status " + folderPath, it is expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2025-02-21 01:37:36

另一个选项是在这种情况下不使用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());

Another option is to not use awk in this case, and do:

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文