![](/img/trans.png)
[英]When I execute docker command from C# app I get "docker:invalid reference format" error
[英]I get a can't execute binary file error when running a bash command from C#
我創建了一個 Discord Bot,您可以與之交互(在具有默認 Raspberry Pi 操作系統的 Raspberry Pi 上運行)。 我還添加了一個命令來執行 shell 命令。 我的代碼:
ProcessStartInfo inf = new()
{
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
FileName = "/bin/bash",
Arguments = command,
};
if (Process.Start(inf) is Process cmd)
{
string ret = "Output: " + await cmd.StandardOutput.ReadToEndAsync();
ret += "\nError Output: " + await cmd.StandardError.ReadToEndAsync();
await Program.Log(ret);
return ret;
}
當我將命令設置為“回顯你好,世界!” 我只是得到錯誤
/usr/bin/echo: /usr/bin/echo: 無法執行二進制文件
來自標准輸出。 我究竟做錯了什么?
如果要通過它執行某些操作,則必須將 -c 參數傳遞給 bash
ProcessStartInfo inf = new()
{
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
FileName = "/bin/bash",
Arguments = $"-c \"{command}\""
};
if (Process.Start(inf) is Process cmd)
{
string ret = "Output: " + await cmd.StandardOutput.ReadToEndAsync();
ret += "\nError Output: " + await cmd.StandardError.ReadToEndAsync();
await Program.Log(ret);
return ret;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.