在 linux 终端上运行 C# 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20392243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run C# code on linux terminal
提问by Hedron Dantas
How can I execute a C# code on a linux terminal as a shell script.
如何在 Linux 终端上将 C# 代码作为 shell 脚本执行。
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#做同样的事情?
采纳答案by Corey
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)标签是用来告诉使用哪种解释让你的Perl,PHP,庆典,SH等脚本将正确运行shell。
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 Monofor the compiler and runtime, then MonoDevelopfor the IDE.
为编译器和运行时安装Mono,然后为 IDE安装MonoDevelop。
回答by user703016
回答by michael
NOTE: @adabru's answer below makes my solution obsolete unless you are using an older mono platform.
注意:除非您使用的是较旧的单声道平台,否则@adabru 下面的回答会使我的解决方案过时。
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 Monoon 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 "" ]; then
dmcs_args=
shift
else
dmcs_args=""
fi
script=
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图形界面”:
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.Windows.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 读取)。最后,编译器的输出被保存到另一个临时文件中,并以传递给脚本的原始参数在单声道中执行。'shift' 运算符用于消除编译器参数和脚本名称,只留下脚本参数。
Compilation errors will be dumped to the command line when the C# script is executed.
执行 C# 脚本时,编译错误将转储到命令行。
回答by Suraj
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.
保存并关闭 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!
回答by adabru
After installing mono you can use csharp hello.cs
. Starting with Mono 2.10, you can also use the shebang like this:
安装单声道后,您可以使用csharp hello.cs
. 从 Mono 2.10 开始,您还可以像这样使用 shebang:
#!/usr/bin/csharp
Console.WriteLine ("Hello, World");
If you need assemblies, you can load them e.g. with the line LoadAssembly("System.IO.Compression")
inside your script.
如果您需要程序集,您可以加载它们,例如使用LoadAssembly("System.IO.Compression")
脚本中的一行。
Reference: man csharp
.
参考:man csharp
。