3

Whenever I try to execute shell commands, I get an error saying

/bin/hostname: /bin/hostname: cannot execute binary file
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: Index was outside the bounds of the array.

I currently have

public MachineData FindHostname()
{
        Thread.Sleep(3000);
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "/bin/bash";
        psi.Arguments = "/bin/hostname && /bin/uname";
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;

        Process proc = new Process
        {
            StartInfo = psi
        };

        proc.Start();
        ...
}

Does anyone know of a workaround?

nostrebor
  • 309
  • 1
  • 3
  • 12
  • 1
    If you try it from command line you'll see it doesn't work and has nothing to do with ASP.NET. You need to give bash the `-c` flag to run commands – Sami Kuhmonen Aug 15 '16 at 20:24
  • obvious mistake. thanks, i'll try that out. – nostrebor Aug 15 '16 at 20:59
  • 1
    You can also use the `UseShellExecute = true` setting. It can be used for exactly this kind of scenario; wanting to run a command through the shell... :) – Per Lundberg Jan 21 '17 at 12:48
  • I'm sorry but how will it work. I didn't get it. Any elaborated answer. In my case I'm providing -c but it still gives an error – Haseeb Jadoon Aug 16 '17 at 10:13
  • Hi, Haseeb. What error are you getting? – nostrebor Aug 18 '17 at 04:08
  • [Please check on this page for working code](https://stackoverflow.com/questions/46419222/execute-linux-command-on-centos-using-dotnet-core/46488598#46488598) – Harit Kumar Oct 03 '17 at 06:55
  • I have made a library to deal easily with that: https://medium.com/@equiman/run-a-command-in-external-terminal-with-net-core-cc24e3cc9839 Hope it help! – equiman Aug 27 '18 at 18:31

0 Answers0