Write shell scripts for Linux using C # (next)

Source: Internet
Author: User
Tags fread

In the end of the previous article, "Writing shell scripts for Linux using C #", we left a question about how C # calls Bashshell. After the article was published, I noticed that there was a reader's message recommending the use of "Pash" (something like PowerShell), and after I downloaded and installed the project, I tried to find that it was still not what we wanted. It seems that C # really does not provide this (output redirection) feature, which also forces us to take other ways to implement it. In this article, we will raise the "intimidation" level and incidentally solve this problem, please fasten your seat belts crossing.

In this article, we will introduce:

(1), C # direct call to Bashshell encounter problems

(2), call Bashshell using C's Popen method

(3), by calling C to call Bashshell indirectly

First, C # direct call Bashshell problems arising from

Using C # to tune other applications, there is no doubt that the most straightforward approach is the Process.Start in "System.Diagnostics". But when we use Process.Start, we find that even the simplest commands cannot be invoked, and it is impossible to talk about calls and accept returns.

For one of these errors (of course there will be more problems, not listed here).

Second, using the Popen method called C

Just because Process.Start can't call Bashshell's command directly, we need to take a detour.

We first look at the C language, the C language calls the shell in a variety of ways, we chose the popen function of the way to call, first look at the following demo:

#include <stdio.h>int  main () {    *fp;     Char buffer[255];    FP=popen ("ls/home/le","R");    Fread (buffer,255,255, FP);    Pclose (FP);    printf ("%s", buffer);}

Through the POEPN pipeline and complete the output redirection.

Third, calling the shell indirectly by calling C

Since C has already been able to implement a call to the Bashshell pipeline redirection, then we can then call C in C # Way, and then indirectly complete the call to Bashshell.

We will first transform our C function, and the following code (for readers who have questions about how to do so, see "How to get C for C # functions"):

#include <stdio.h>#include<string.h>void* CONVERTTOCSTR (Char* Input,Char* Res,int*length) {    inti;  for(i=0; i<*length;i++) {Res[i]=* (input+2*i); } Res[i]=' /';}void* Bashhelper (Char* Cmdstr,int* Cmdlength,Char* Output,int*length) {FILE*FP; Charbuffer[*length]; Charcmd[*cmdlength+1];    Converttocstr (cmdstr,cmd,cmdlength); FP=popen (CMD,"R"); Fread (Buffer,*length,*LENGTH,FP);    Pclose (FP);    strcat (Output,buffer); }

The same is true of the C # shell (no IntelliSense is hard to write, I first write it in the console and then copy it).

#!/bin/Env CSharpusingSystem.Diagnostics;usingSystem.IO;usingSystem.Runtime.InteropServices;classclib{ Public Static stringInvokebash (stringcmdstr) {        Char[] Output =New Char[255]; unsafe        {            fixed(Char* C =cmdstr)fixed(Char* op =output) {                intCmdlenth =cmdstr.length; intOutputlength =output.                Length; Clib.bashhelper (c,&cmdlenth, OP, &outputlength); returnMarshal.ptrtostringansi ((IntPtr) op); }}} [DllImport ("/You store so's address/shell.so", CallingConvention =Callingconvention.stdcall)]Static unsafe extern voidBashhelper (Char* Cmdstr,int* Cmdlength,Char* Output,int*length);}varCmdstr ="/bin/ls/";varOutput =Clib.invokebash (CMDSTR); Console.Write (output);

Once we're done, we'll call again in the shell.

Executes the Bashshell command successfully and redirects the return output to C #.

There may be readers who have such a question: "This is not the same as directly written Bashshell?!" "This is bad, C # has the advantage of C #, Bash has the advantage of bash, the combination of the two can be complementary, using Bash can quickly operate Linux, and some of the features bash does not provide, such as writing to the database, calling some services API, Do other things that Bashshell can't do.

OK, this article is written so much, non-C professionals, the text may have unscientific, only provide ideas, do not shoot brick ha. Thank you.

Original address: http://jhonge.net/Home/Single2/1938

Write shell scripts for Linux using C # (next)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.