Use C # To write Shell scripts for Linux,

Source: Internet
Author: User

Use C # To write Shell scripts for Linux,

In this age of determining the personality and the prevalence of the contempt chain, especially in our IT industry, what are the times of your contempt and contempt today? If you do not have the ability to press the box, you have to read it. Today, we also want to improve our style, learn things that are wide open in our minds, and then go to the group to scare other friends ~~!

Speaking of Linux scripts, there is no doubt that Shell scripts are the most used, and other scripts include Python and Ruby. It is estimated that no one will think of C. Speaking of C #, there is still the sword version. It can write application systems under the Operating System (CosmosOS, SharpOS, etc.), desktop programs, website programs, embedded programs, and other programs, write scripts? It is also a successful task.
Okay, go to the official environment of this article. In this article, we will introduce:
(1) Introduction to C # Shell.
(2) compile our first C # Shell script.
(3) introduce the assembly.

I. Introduction to C # Shell
Shell is a translator that provides communication between users and the operating system kernel. Common Shell commands include graphical Shell and command line Shell, it is also the focus of Linux apart from installing the operating system. I can assure that all users who have used Linux will know about it.
C # Shell, just like its name, is a Shell. The command line language used is C #. In fact, its prototype is a Tool provided in the Tool in the Mono Project. Its real name is "C # REPL", so you want to use it, mono must be installed in Linux.. NET environment.
I don't want to talk about how to build a Linux. NET environment. If you still don't understand it, go to the Linux. NET learning notes series. You can run the "/bin/env csharp" command to execute the C # Shell command parser on all machines that have set up the Linux. NET environment. The system console will enter the C # Shell terminal (a little like a Telnet connection ).

Then we can type various commands in it. If you want to exit, execute "quit" to exit and return to the Linux Command terminal.

 

2. Compile the first C # Shell script

For some Linux Shell readers, after the introduction in the previous section, they probably know what is going on. It doesn't matter if you don't understand Shell. This section teaches you how to write a C # Shell. First, we will show you the format of the next "standard" Shell.

Here is a common Bash Shell script, which is used to output the "small butterfly dahong Dafa good" output ten times ". A common Linux Shell script consists of two parts: the first is its parser Declaration, that is, the first line of the header, it is used to specify the Shell parser used by the script for parsing. Since different Shell parsers have their own Shell command lines, it is very important for the computer to select the correct Shell parsers. In addition, it is the main body of the Shel script. Because the Shell script is executed through the parser, its execution method starts from the first line of this article, and parse and execute the statement line by line according to the text. Therefore, in principle, we can write programs like "sequential accounts" (without considering maintainability ).

Below we also write a C # Shell according to this "Standard Format.

First, we need to create a new file. It doesn't matter what the suffix is. in Linux, there is no suffix concept, and then open and fill in the location of the parser, that is, the first line.

Then we can write our code in the same way as C # In normal times (Chinese attention encoding ).

After saving, add the execution permission in Linux and run it (the script file is named demo. cs ).

Sauce ~! An interesting thing happened.

 

3. How to introduce an assembly

Now we have proved the feasibility of the C # Shell script and made readers learn how to compile the C # Shell script, which has gained initial application value, however, because the parser directs the introduction of "System" by default, it can only do something very simple for the time being.

Unlike other "thin" languages, C # has an extremely rich and powerful class library, which is the major force in C #. We need to use them in various occasions at various times. Now let's learn how to call them.

In Linux. NET, the Assembly mainly comes from two sources: the basic library that comes with Mono and the third-party class libraries that we manually introduce. We will explain them separately.

First, explain the basic libraries. The basic libraries that come with Mono can be found in the Mono compilation and installation directory and you want to reference them, you only need to use the "Using namespace" in the script to add them. The effect is as follows:

The Assembly can be automatically introduced and used normally. For third-party class libraries, since they are not added to the reference by default, We need to manually Load the assembly. It is also easy to Load third-party programs. You only need to use "LoadAssembly (" Physical location of the Assembly [do not need dll suffix] ")" and then use the "Using namespace" method.

Clear, you can continue to play.

Okay, the content of this article is almost the same. The purpose of writing this article is only to provide multiple choices of Shell scripts, not to replace the classics such as Bash and Python, you can play on your own machine. If you have any questions. NET interested readers can follow my microblog or go to "OWIN [Open Web Interface. NET) "contact me (Q group number: 373470340 ). Oh, by the way, there are also questions about the interaction between C # Shell and Bash Shell. I have not found any good method yet. Please contact me if you have any inspiration. Thank you.

Personal blog article address: http://jhonge.net/Home/Single2/1802

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.