.net core能调用shell脚本代码示例
时间: 2023-07-08 17:16:56 浏览: 58
是的,.NET Core能够调用Shell脚本代码。可以使用Process类来启动一个Shell进程,并且可以指定需要执行的Shell命令或脚本文件路径。
以下是一个简单的示例代码,它会启动一个Shell进程,并执行一个简单的Shell脚本命令:
```csharp
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "/bin/bash"; // 用于启动Shell进程的可执行文件路径
startInfo.Arguments = "-c \"echo Hello World\""; // 需要执行的Shell命令
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
}
}
```
该示例代码使用/bin/bash可执行文件启动了一个Shell进程,并执行了一个简单的echo命令,输出Hello World字符串。可以根据需要修改Shell命令或脚本文件路径。