在cygwin上使用`std::process::Command`执行`find`无效

在cygwin上使用`std::process::Command`执行`find`无效,cygwin,rust,Cygwin,Rust,当我试图从Rust程序调用find命令时,要么得到find:Invalid开关,要么得到find:Parameter format incorrect错误 find可以在命令行中正常工作 echo$PATH /usr/local/bin:/usr/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:。。。。。 我正在搜索的文件(main.rs)存在 ./find_cmdd.exe 线程“”在“”处惊慌失措,无法执行进程:系统找不到指定的文件

当我试图从Rust程序调用
find
命令时,要么得到
find:Invalid开关
,要么得到
find:Parameter format incorrect
错误

find
可以在命令行中正常工作

echo$PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:。。。。。
我正在搜索的文件(
main.rs
)存在

./find_cmdd.exe

线程“”在“”处惊慌失措,无法执行进程:系统找不到指定的文件。(操作系统错误2)”,查找cmdd.rs:12

我还尝试了以下选项

let mut cmd_find = Command::new("find").....
我得到的
FIND:Invalid开关
错误

我无法将
find.exe重命名/复制到另一个位置。

“find:Invalid switch error”表示这不是cygwin
find
,但您正在调用Windows。要再次检查:

$find-k
查找:未知谓词“%k”
$/cygdrive/c/windows/system32/find-k
查找:参数格式不正确

当您通过
命令运行程序时,Cygwin基本上不存在。执行进程使用操作系统的本机功能;就窗户而言

这意味着:

  • cygwin shell设置的
    PATH
    变量在启动进程时可能有任何意义,也可能没有任何意义
  • Windows中实际上不存在带有
    /cygdrive/…
    的目录结构;那是一件艺术品
  • 综上所述,您必须使用Windows本机路径:

    use std::process::{Stdio, Command};
    use std::io::Write;
    
    fn main() {
        let mut cmd_find = Command::new(r#"\msys32\usr\bin\find.exe"#)
            .args(&[r#"\msys32\home"#])
            .stdin(Stdio::piped())
            .spawn()
            .unwrap_or_else(|e| panic!("failed to execute process:  {}", e));
    
        if let Some(ref mut stdin) = cmd_find.stdin {
            stdin.write_all(b"main.rs").unwrap();
        }
    
        let res = cmd_find.wait_with_output().unwrap().stdout;
        println!("{}", String::from_utf8_lossy(&res));
    }
    

    作为旁注,我不知道
    find
    的管道标准输入有什么作用;它似乎对我在Msys2或OS X上没有任何影响。

    Hi Matzeri,我同意正在调用windows find。正如我所指出的,PATH变量在windows路径之前保存着Cygwin bin目录,因此我不确定windows find是如何被调用的,但是我如何在我的程序中调用或使用Cygwin find命令呢?正如我所指出的,我已经尝试使用,(“/cygdrive/c/cygwin64/bin/find)…这似乎没用。谢谢谢普马斯特!
    use std::process::{Stdio, Command};
    use std::io::Write;
    
    fn main() {
        let mut cmd_find = Command::new(r#"\msys32\usr\bin\find.exe"#)
            .args(&[r#"\msys32\home"#])
            .stdin(Stdio::piped())
            .spawn()
            .unwrap_or_else(|e| panic!("failed to execute process:  {}", e));
    
        if let Some(ref mut stdin) = cmd_find.stdin {
            stdin.write_all(b"main.rs").unwrap();
        }
    
        let res = cmd_find.wait_with_output().unwrap().stdout;
        println!("{}", String::from_utf8_lossy(&res));
    }