为什么我无法在Linux终端执行命令?

问题描述 投票:0回答:1

我使用的框架是.Net 8。

我尝试遵循如何在 C# Mono 中执行 Linux 命令行

中接受的答案

这是我的代码:

ProcessStartInfo _processStartInfo = new ProcessStartInfo();
_processStartInfo.FileName = "dmesg | grep tty";
_processStartInfo.UseShellExecute = false;
_processStartInfo.RedirectStandardOutput = true;
Process p = Process.Start(_processStartInfo );
var _result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();

我运行代码后,它报告此错误:

Unhandled exception. System.ComponentModel.Win32Exception (2): An error occurred           trying to start process 'dmesg | grep tty' with working directory '/home/admin'          . No such file or directory
   at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo,           String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCr          edentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32          & stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

在putty终端中,命令运行良好,如下:

有什么问题吗?

asp.net-core .net-core
1个回答
0
投票

尝试使用

FileName
Arguments
,如下所示,它在我这边有效。

ProcessStartInfo startInfo = new ProcessStartInfo
{
    FileName = "/bin/bash",
    Arguments = "-c \"dmesg | grep tty\"",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

这是我的azure应用服务linux平台上的测试结果。

© www.soinside.com 2019 - 2024. All rights reserved.