如何解决从ASP.NET Core 3应用程序执行Linux Shell命令
我的问题从字面上看,我如何从应用程序中执行Shell命令。有一个类似的Post,但是它显示了如何执行脚本文件并需要该文件的路径。
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command,// Path required here
Arguments = args,RedirectStandardOutput = true,RedirectStandardError = true,UseShellExecute = false,CreateNoWindow = true,}
};
process.Start();
为什么要以字符串形式传递命令?
因为我想对它进行插值。否则,我将不得不创建一个带有一些输入参数的脚本。由于我对Shell不太满意,因此我更喜欢简单的方法。
解决方法
假设您要在bash中运行echo hello
,然后
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "bash",RedirectStandardInput = true,RedirectStandardOutput = true,RedirectStandardError = true,UseShellExecute = false
}
};
process.Start();
await process.StandardInput.WriteLineAsync("echo hello");
var output = await process.StandardOutput.ReadLineAsync();
Console.WriteLine(output);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。