Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: How do execute a bash script with C# .NET

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    How do execute a bash script with C# .NET

    Hi all.

    I hope someone can help me. On a Linux debian server i got, i have installed mono, with the purpose of being able to make a C# .NET program, that can execute a bash script, and somehow import the output of the bash script into C# for processing.

    But how do you do this?

    Also, how can you from C# send a argument or value to a bash script?

    Hope someone can help me, or lead me in the right direction.

    Thanks on advance.
    Kind regards.

  2. #2
    Join Date
    Jul 2008
    Location
    England
    Beans
    860

    Re: How do execute a bash script with C# .NET

    What script are you trying to run?

    Have you checked that libraries don't already exist for what you are after?

    Paul

  3. #3
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do execute a bash script with C# .NET

    Lets say i want to run a script containing ls commands.
    Just so i can get an example of this process.

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: How do execute a bash script with C# .NET

    see:
    http://msdn.microsoft.com/en-us/libr...s.process.aspx
    http://msdn.microsoft.com/en-us/libr...startinfo.aspx
    http://msdn.microsoft.com/en-us/libr...ardoutput.aspx

    example:
    Code:
    /*$ cat /tmp/bash.sh 
     * #!/bin/bash
     * echo "bla $1"
     */
    using System;
    using System.Diagnostics;
    
    class Runshell
    {
      static void Main()
      {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "/tmp/bash.sh";
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
    
        psi.Arguments = "test";
        Process p = Process.Start(psi);
        string strOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        Console.WriteLine(strOutput);
      }
    }

  5. #5
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do execute a bash script with C# .NET

    Quote Originally Posted by MadCow108 View Post
    see:
    http://msdn.microsoft.com/en-us/libr...s.process.aspx
    http://msdn.microsoft.com/en-us/libr...startinfo.aspx
    http://msdn.microsoft.com/en-us/libr...ardoutput.aspx

    example:
    Code:
    /*$ cat /tmp/bash.sh 
     * #!/bin/bash
     * echo "bla $1"
     */
    using System;
    using System.Diagnostics;
    
    class Runshell
    {
      static void Main()
      {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "/tmp/bash.sh";
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
    
        psi.Arguments = "test";
        Process p = Process.Start(psi);
        string strOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        Console.WriteLine(strOutput);
      }
    }
    Thanks, this gives me a lot of information i can look through and make searches from. Thumps up. If anyone has more to give i wont say NO!

  6. #6
    Join Date
    Jul 2008
    Location
    England
    Beans
    860

    Re: How do execute a bash script with C# .NET

    Quote Originally Posted by Drenriza View Post
    Lets say i want to run a script containing ls commands.
    This is exactly the reason I didn't post the solution in my original post. What do you really want to do? Finding information about files and folders is something you should be doing with the .NET API, not something you should be running bash scripts on.

    You should try to avoid running scripts from C# unless you have no other alternative.

    Paul

  7. #7
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do execute a bash script with C# .NET

    Quote Originally Posted by PaulM1985 View Post
    This is exactly the reason I didn't post the solution in my original post. What do you really want to do? Finding information about files and folders is something you should be doing with the .NET API, not something you should be running bash scripts on.

    You should try to avoid running scripts from C# unless you have no other alternative.

    Paul
    I work in a medium size corporation, where i from time to time write scripts to do a specific task. Over R&D department has at sometime written a command in the C language or bash scripting (i don't know what it is 100% or how to check it, asking is like puting a sign on ur back saying "i'm THAT dumb"). But it can be executed from a bash script. And what i want to do it run that command (right now) and take its output. But it wodent make sense (i think) to tell about a custom command you don't know. So just to get an example to work from. I said ls. Since thats what comes the closest in function, and what has most similarities in its output form.

    I'm interested in learning the foundation in getting C# working on Linux.
    Taking output from bash, C and C++ and being able to work with it in C#. But i need to start somewhere, to try and get this understanding.

    Finding information about files and folders is something you should be doing with the .NET API, not something you should be running bash scripts on.
    Your right, but i dont know how.
    Last edited by Drenriza; July 15th, 2011 at 12:53 PM.

  8. #8
    Join Date
    Jul 2008
    Location
    England
    Beans
    860

    Re: How do execute a bash script with C# .NET

    Ok, so you have a function in C that you want to call. Have you seen anything like this:

    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

    Say you have a function in C which looks like:
    int GetNumPlusOne(int num)
    in a lib called "Test.dll"

    You can do:
    [DllImport("Test.dll")]
    public static extern int GetNumPlusOne(int num);

    And now you can use GetNumPlusOne in your C# code. Saves you having to use scripts

    Paul

  9. #9
    Join Date
    Jul 2008
    Location
    England
    Beans
    860

    Re: How do execute a bash script with C# .NET

    File and directory info can be found using the file and directory info classes:

    http://msdn.microsoft.com/en-us/libr....fileinfo.aspx
    http://msdn.microsoft.com/en-us/libr...ctoryinfo.aspx

    Paul

  10. #10
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do execute a bash script with C# .NET

    Quote Originally Posted by PaulM1985 View Post
    Ok, so you have a function in C that you want to call. Have you seen anything like this:

    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

    Say you have a function in C which looks like:
    int GetNumPlusOne(int num)
    in a lib called "Test.dll"

    You can do:
    [DllImport("Test.dll")]
    public static extern int GetNumPlusOne(int num);

    And now you can use GetNumPlusOne in your C# code. Saves you having to use scripts

    Paul
    As i understand it a DLL is a dynamic link library. Lets say this custom C program lies in /path/bin/commandName

    How can i decompile this, and search for this function, that you describe.
    Or have i misunderstood something?

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •