在linux终端上运行c#代码

时间:2021-07-08 00:09:00

How can I execute a C# code on a linux terminal as a shell script.

如何在linux终端上以shell脚本的形式执行c#代码。

I have this sample code:

我有这个示例代码:

public string Check(string _IPaddress,string _Port, int _SmsID)
{
ClassGlobal._client = new TcpClient(_IPaddress, Convert.ToInt32(_Port));
ClassGlobal.SMSID = _SmsID;
string _result = SendToCAS(_IPaddress, _Port, _SmsID );
if (_result != "") return (_result);
string _acoknoledgement = GetFromCAS();
return _acoknoledgement;
}

When I run a shell bash I use #!/bin/bash. There is how to do the same with C#?

当我运行shell bash时,我使用#!/bin/bash。如何用c#做同样的事情?

4 个解决方案

#1


8  

The #! (hashbang) tag is used to tell the shell which interpreter to use so that your perl, php, bash, sh, etc. scripts will run right.

# !(hashbang)标记用于告诉shell使用哪个解释器,以便您的perl、php、bash、sh等脚本可以正常运行。

But C# is not a scripting language, it is intended to be compiled into an executable format. You need to install at least a compiler and runtime if you want to use C#, and preferably an IDE (Integrated Development Environment) to help you develop and debug your applications.

但是c#不是一种脚本语言,它打算被编译成可执行的格式。如果您想使用c#,您需要安装至少一个编译器和运行时,最好安装一个IDE(集成开发环境)来帮助您开发和调试应用程序。

Install Mono for the compiler and runtime, then MonoDevelop for the IDE.

为编译器和运行时安装Mono,然后为IDE安装MonoDevelop。

#2


22  

C# scripts can be run from the bash command line just like Python and Perl scripts, but it takes a small bit of bash magic to make it work. As Corey mentioned above, you must first install Mono on your machine. Then, save the following code in an executable bash script on your Linux machine:

c#脚本可以像Python和Perl脚本一样从bash命令行运行,但是需要一点bash魔法才能使它工作。正如Corey上面提到的,您必须首先在您的机器上安装Mono。然后,在Linux机器上的可执行bash脚本中保存以下代码:

if [ ! -f "$1" ]; then
dmcs_args=$1
shift
else
dmcs_args=""
fi
script=$1
shift
input_cs="$(mktemp)"
output_exe="$(mktemp)"
tail -n +2 $script > $input_cs
dmcs $dmcs_args $input_cs -out:${output_exe} && mono $output_exe $@                                                                          
rm -f $input_cs $output_exe

Assuming you saved the above script as /usr/bin/csexec, an example C# "script" follows:

假设您将上面的脚本保存为/usr/bin/csexec,下面是一个c#“脚本”示例:

#!/usr/bin/csexec -r:System.Windows.Forms.dll -r:System.Drawing.dll                                                                   
using System;                                                                                                                                
using System.Drawing;                                                                                                                        
using System.Windows.Forms;                                                                                                                  
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
        MessageBox.Show("Hello GUI");                                                                                                            
    }                                                                                                                                          
}                                                                                                                                            

Save the above code to a file such as "hello.cs", make it executable, change the first line to point to the previously saved bash script, and then execute it, you should see the following output along with a dialog saying "Hello GUI":

将上面的代码保存到一个文件中,例如“hello”。cs“,使其可执行,将第一行更改为指向先前保存的bash脚本,然后执行它,您应该会看到以下输出以及一个对话框“Hello GUI”:

bash-4.2$ ./hello.cs foo bar baz
Hello Console
Arguments: foo, bar, baz

Note that the GUI requires that you be at run level 5. Here is a simpler C# script that runs at a pure text console:

请注意,GUI要求您处于运行级别5。下面是一个简单的c#脚本,它在纯文本控制台运行:

#!/usr/bin/csexec                                                                                                                     
using System;                                                                                                                                
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
    }                                                                                                                                          
}                                                                                                                                            

Notice that the command line arguments are passed to the C# script, but the shebang arguments (in the first C# script above "-r:System.Windows.Forms.dll -r:System.Drawing.dll") are passed to the C# compiler. Using the latter functionality, you can specify any compiler arguments you require on the first line of your C# script.

注意,命令行参数传递给了c#脚本,但是shebang参数(在上面的第一个c#脚本中是“-r: system . window . forms”)。(dll -r:System.Drawing.dll)被传递给c#编译器。使用后一种功能,您可以在c#脚本的第一行中指定所需的任何编译器参数。

If you are interested in the details of how the bash script works, shebang (#!) lumps together all arguments passed to it on the first line of the C# script, followed by the script name, followed by command line arguments passed to the script itself. In the first C# example above, the following 5 arguments would be passed into the bash script (delineated by quotes):

如果您对bash脚本如何工作的细节感兴趣,那么shebang(#!)将在c#脚本的第一行传递给它的所有参数聚合在一起,然后是脚本名称,然后是传递给脚本本身的命令行参数。在上面的第一个c#示例中,下面的5个参数将被传递到bash脚本中(用引号描述):

"-r:System.Windows.Forms.dll -r:System.Drawing.dll" "hello.cs" "foo" "bar" "baz"

The script determines that the first argument is not a filename and assumes it contains arguments for the C# compiler. It then strips off the first line of the C# script using 'tail' and saves the result to a temporary file (since the C# compiler does not read from stdin). Finally, the output of the compiler is saved to another temporary file and executed in mono with the original arguments passed to the script. The 'shift' operator is used to eliminate the compiler arguments and the script name, leaving behind only the script arguments.

脚本确定第一个参数不是文件名,并假定它包含c#编译器的参数。然后使用“tail”删除c#脚本的第一行,并将结果保存到临时文件中(因为c#编译器不从stdin中读取)。最后,将编译器的输出保存到另一个临时文件中,并使用mono执行,并将原始参数传递给脚本。“shift”操作符用于消除编译器参数和脚本名称,只留下脚本参数。

Compilation errors will be dumped to the command line when the C# script is executed.

当执行c#脚本时,编译错误将被转储到命令行。

#3


14  

Of course it can be done and the process is extremely simple.

当然,这是可以做到的,而且过程极其简单。

Here I am explaining the steps for Ubuntu Linux.

这里我将解释Ubuntu Linux的步骤。

Open terminal:

打开终端:

Ctrl + Alt + T

Ctrl + Alt + T

Type

类型

gedit hello.cs

In the gedit window that opens paste the following example code:

在打开的gedit窗口中,粘贴以下示例代码:

using System;
class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello World!");
  }
}

Save and close gedit.

保存并关闭中。

Back in terminal type:

在终端类型:

sudo apt update
sudo apt install mono-complete
mcs -out:hello.exe hello.cs
mono hello.exe

Output:

输出:

Hello World!

#4


5  

You can't execute C# like a script, you have to compile it first. For that, you could install mono.

你不能像脚本一样执行c#,你必须先编译它。为此,您可以安装mono。

You can then compile your program with mcs and execute it with mono.

然后,您可以使用mcs编译程序并使用mono执行它。

#1


8  

The #! (hashbang) tag is used to tell the shell which interpreter to use so that your perl, php, bash, sh, etc. scripts will run right.

# !(hashbang)标记用于告诉shell使用哪个解释器,以便您的perl、php、bash、sh等脚本可以正常运行。

But C# is not a scripting language, it is intended to be compiled into an executable format. You need to install at least a compiler and runtime if you want to use C#, and preferably an IDE (Integrated Development Environment) to help you develop and debug your applications.

但是c#不是一种脚本语言,它打算被编译成可执行的格式。如果您想使用c#,您需要安装至少一个编译器和运行时,最好安装一个IDE(集成开发环境)来帮助您开发和调试应用程序。

Install Mono for the compiler and runtime, then MonoDevelop for the IDE.

为编译器和运行时安装Mono,然后为IDE安装MonoDevelop。

#2


22  

C# scripts can be run from the bash command line just like Python and Perl scripts, but it takes a small bit of bash magic to make it work. As Corey mentioned above, you must first install Mono on your machine. Then, save the following code in an executable bash script on your Linux machine:

c#脚本可以像Python和Perl脚本一样从bash命令行运行,但是需要一点bash魔法才能使它工作。正如Corey上面提到的,您必须首先在您的机器上安装Mono。然后,在Linux机器上的可执行bash脚本中保存以下代码:

if [ ! -f "$1" ]; then
dmcs_args=$1
shift
else
dmcs_args=""
fi
script=$1
shift
input_cs="$(mktemp)"
output_exe="$(mktemp)"
tail -n +2 $script > $input_cs
dmcs $dmcs_args $input_cs -out:${output_exe} && mono $output_exe $@                                                                          
rm -f $input_cs $output_exe

Assuming you saved the above script as /usr/bin/csexec, an example C# "script" follows:

假设您将上面的脚本保存为/usr/bin/csexec,下面是一个c#“脚本”示例:

#!/usr/bin/csexec -r:System.Windows.Forms.dll -r:System.Drawing.dll                                                                   
using System;                                                                                                                                
using System.Drawing;                                                                                                                        
using System.Windows.Forms;                                                                                                                  
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
        MessageBox.Show("Hello GUI");                                                                                                            
    }                                                                                                                                          
}                                                                                                                                            

Save the above code to a file such as "hello.cs", make it executable, change the first line to point to the previously saved bash script, and then execute it, you should see the following output along with a dialog saying "Hello GUI":

将上面的代码保存到一个文件中,例如“hello”。cs“,使其可执行,将第一行更改为指向先前保存的bash脚本,然后执行它,您应该会看到以下输出以及一个对话框“Hello GUI”:

bash-4.2$ ./hello.cs foo bar baz
Hello Console
Arguments: foo, bar, baz

Note that the GUI requires that you be at run level 5. Here is a simpler C# script that runs at a pure text console:

请注意,GUI要求您处于运行级别5。下面是一个简单的c#脚本,它在纯文本控制台运行:

#!/usr/bin/csexec                                                                                                                     
using System;                                                                                                                                
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
    }                                                                                                                                          
}                                                                                                                                            

Notice that the command line arguments are passed to the C# script, but the shebang arguments (in the first C# script above "-r:System.Windows.Forms.dll -r:System.Drawing.dll") are passed to the C# compiler. Using the latter functionality, you can specify any compiler arguments you require on the first line of your C# script.

注意,命令行参数传递给了c#脚本,但是shebang参数(在上面的第一个c#脚本中是“-r: system . window . forms”)。(dll -r:System.Drawing.dll)被传递给c#编译器。使用后一种功能,您可以在c#脚本的第一行中指定所需的任何编译器参数。

If you are interested in the details of how the bash script works, shebang (#!) lumps together all arguments passed to it on the first line of the C# script, followed by the script name, followed by command line arguments passed to the script itself. In the first C# example above, the following 5 arguments would be passed into the bash script (delineated by quotes):

如果您对bash脚本如何工作的细节感兴趣,那么shebang(#!)将在c#脚本的第一行传递给它的所有参数聚合在一起,然后是脚本名称,然后是传递给脚本本身的命令行参数。在上面的第一个c#示例中,下面的5个参数将被传递到bash脚本中(用引号描述):

"-r:System.Windows.Forms.dll -r:System.Drawing.dll" "hello.cs" "foo" "bar" "baz"

The script determines that the first argument is not a filename and assumes it contains arguments for the C# compiler. It then strips off the first line of the C# script using 'tail' and saves the result to a temporary file (since the C# compiler does not read from stdin). Finally, the output of the compiler is saved to another temporary file and executed in mono with the original arguments passed to the script. The 'shift' operator is used to eliminate the compiler arguments and the script name, leaving behind only the script arguments.

脚本确定第一个参数不是文件名,并假定它包含c#编译器的参数。然后使用“tail”删除c#脚本的第一行,并将结果保存到临时文件中(因为c#编译器不从stdin中读取)。最后,将编译器的输出保存到另一个临时文件中,并使用mono执行,并将原始参数传递给脚本。“shift”操作符用于消除编译器参数和脚本名称,只留下脚本参数。

Compilation errors will be dumped to the command line when the C# script is executed.

当执行c#脚本时,编译错误将被转储到命令行。

#3


14  

Of course it can be done and the process is extremely simple.

当然,这是可以做到的,而且过程极其简单。

Here I am explaining the steps for Ubuntu Linux.

这里我将解释Ubuntu Linux的步骤。

Open terminal:

打开终端:

Ctrl + Alt + T

Ctrl + Alt + T

Type

类型

gedit hello.cs

In the gedit window that opens paste the following example code:

在打开的gedit窗口中,粘贴以下示例代码:

using System;
class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello World!");
  }
}

Save and close gedit.

保存并关闭中。

Back in terminal type:

在终端类型:

sudo apt update
sudo apt install mono-complete
mcs -out:hello.exe hello.cs
mono hello.exe

Output:

输出:

Hello World!

#4


5  

You can't execute C# like a script, you have to compile it first. For that, you could install mono.

你不能像脚本一样执行c#,你必须先编译它。为此,您可以安装mono。

You can then compile your program with mcs and execute it with mono.

然后,您可以使用mcs编译程序并使用mono执行它。