-1

I'm receiving a random line break in the following C# string and can't identify why the output is on two separate lines.

I've really only tried to define the variable in two different manners. Both produce the same results.

string cmdStr = "docker images | grep -E '^prabhasgupte/webmon.*latest' | head -1 | awk '{print $3}'";
Console.Write(cmdStr);
string imageId = commands.Bash(cmdStr);
Console.Write(imageId);
string x = $"docker tag {imageId} scan_target:{imageId}";
Console.Write(x);

This is the output I'm receiving:

docker images | grep -E '^prabhasgupte/webmon.*latest' | head -1 | awk '{print $3}'

0043699b906f

docker tag 0043699b906f

scan_target:0043699b906f

public static string Bash(this string cmd)
{
    var escapedArgs = cmd.Replace("\"", "\\\"");

    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "/bin/bash",
            Arguments = $"-c \"{escapedArgs}\"",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        }
    };
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    return result;
}
Community
  • 1
  • 1
Ryan Brown
  • 23
  • 1
  • 7
  • Have you attached a debugger to this? could one of the two strings contain an extraneous line-break? – Sam Axe Oct 17 '19 at 18:57
  • You'd probably want to take a closer look at that `Bash()` implementation – Mathias R. Jessen Oct 17 '19 at 18:57
  • 1
    The string that `commands.Bash(...)` returns contains a newline character. –  Oct 17 '19 at 18:58
  • 1
    I am [not able](https://dotnetfiddle.net/BYhjJW) to reproduce when replacing the `commands.Bash()` call with a fixed string. I imagine its something to do with that implementation. See what the debugger returns – maccettura Oct 17 '19 at 18:58
  • `string imageId = commands.Bash(cmdStr) + " some test";`, try that and run it... Does it seperate the `some test`? Or try this `Regex.Replace(commands.Bash(cmdStr), @"\t|\n|\r", "");` – Trevor Oct 17 '19 at 19:05
  • @Çöđěxěŕ In that particular instance it does separate the " some test" on a new line but if I attached that to the end of the same function with other commands, it does not. Here is the Bash function: – Ryan Brown Oct 17 '19 at 19:14
  • `process.StandardOutput.ReadToEnd()` trim the end of this; this is the actual root cause of the later issue. – Trevor Oct 17 '19 at 19:17

2 Answers2

0

Seems like your issue might be awk inserting a newline, which is its default behavior. I copied this from an awk-related SO question:

The ORS (output record separator) variable in AWK defaults to "\n" and is printed after every line. You can change it to " " in the BEGIN section if you want everything printed consecutively.

Daniel Crha
  • 675
  • 5
  • 13
-1

the reason this is happening is that imageId has a line break in it from the bash command. try trimming the result

   string imageId = commands.Bash(cmdStr).TrimEnd( '\r', '\n', Environment.NewLine.ToCharArray() );
Mr Heelis
  • 2,370
  • 4
  • 24
  • 34
  • 1
    Sure, it is correct that the newline is from the bash command, but why put on a band-aid in C# when you can solve the problem in the place where it's really originating from (awk)? – Daniel Crha Oct 17 '19 at 19:25
  • @DanielCrha I believe you can pass `cat` as a command line arg when calling the script, that may actually remove it, haven't tried it myself, but would be interesting. – Trevor Oct 17 '19 at 19:28
  • @Çöđěxěŕ cat means "stream pipe" (usually it covers files) so yes, it does "pump" the output in bash as it were.. I am fairly sure it doesn't insert new line characters because it's fairly common to `grep` its result - incidentally I disagree that it is a **repeat of the same question** as you marked .. he is asking **why** the "random" break appeared.. I am (the only one) telling him why the "random break" is there – Mr Heelis Oct 17 '19 at 19:36