【问题标题】:Command not found when using system process使用系统进程时找不到命令
【发布时间】:2016-06-06 10:46:46
【问题描述】:

当我使用终端/Bash 调用 Image Magick 命令(例如“convert”)时,我的命令成功了。如果我使用 C# 脚本中的系统进程使用相同的命令“convert”将参数传递给 Bash 控制台,它将返回一个未通过 StandardRedirectError 找到的错误命令。

为什么使用系统进程时找不到命令?例如

    ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
    startInfo.WorkingDirectory = installFolder;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;


    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start(); 

    if (process != null) {

        process.StandardInput.WriteLine ("convert"); //error: command not found
        process.StandardInput.WriteLine ("echo \"hello world\""); //output: "hello world"

谢谢

【问题讨论】:

    标签: c# bash process


    【解决方案1】:

    请发布更完整的代码示例,您需要告诉 Process 要运行什么可执行文件并传入参数。 See the MSDN docs and Sample.

    有点像

       Process process = new Process();
       startInfo.Arguments = "image_[0-9].gif  image_[1-9][0-9].gif  animation.gif" 
       process.StartInfo = startInfo;
       process.Start("convert.exe");
    

    【讨论】:

    • 这是更简单的方法,您可以使用参数调用应用程序或脚本,我已经通过了这个障碍。我正在使用 StandardInput.WriteLine() 方法将参数传递给 bash 控制台,直到进程退出/退出。无论如何,我都会更新我的代码以使其更清晰。 ;)
    • 查看您更新的问题 - 是否转换为所需的路径?如果你完全限定它 @"C:\myfolder\convert.exe" 怎么办?
    • "convert" 行是一个 Image Magick 命令行命令,它自己会显示帮助信息。据我所知,Image Magick 被设置为环境路径,因为我可以通过终端或 /bin/bash 手动调用它。正是这一点让我感到困惑,因为在带有或不带有完整参数的命令中传递失败,但简单的回显成功。我会尝试在我的系统上找到它的确切路径,看看是否有帮助。它可以安装在系统上的许多地方,并且使用绝对路径对于我正在创建的工具来说并不理想。谢谢,感谢您的意见。
    • 看来我用 Mac Ports 安装了这个包,它在我的 .bash_profile 中有一个环境路径。这可能会通过系统进程破坏命令。 export PATH="/opt/local/bin:/opt/local/sbin:$PATH" - 无论如何都要继续。
    • 也可以在这里查看:askubuntu.com/questions/121073/…
    【解决方案2】:

    正如@MurrayFoxcroft 所指的正确方向,这是由于非登录shell 模式意味着我的.bash_profile 没有加载。这样做:

            process.StandardInput.WriteLine (". ~/.bash_profile");
            process.StandardInput.WriteLine ("convert");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2014-05-21
      • 2019-05-17
      • 2020-03-30
      相关资源
      最近更新 更多