在Cygwin上使用`std :: process :: Command`执行`find`不起作用 [英] Executing `find` using `std::process::Command` on cygwin does not work

查看:61
本文介绍了在Cygwin上使用`std :: process :: Command`执行`find`不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从Rust程序调用 find 命令时,我得到 FIND:无效开关 FIND:参数格式不正确错误.

When I try to call the find command from a Rust program, either I get a FIND: Invalid switch or a FIND: Parameter format incorrect error.

查找在命令行中可以正常工作.

find works fine from command line.

echo $PATH

/usr/local/bin:/usr/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:.....

我正在搜索的文件( main.rs )存在.

The file I am searching for (main.rs) exists.

use std::process::{Stdio,Command};
use std::io::{Write};

fn main() {
    let mut cmd_find = Command::new("/cygdrive/c/cygwin64/bin/find.exe")
        .arg("/cygdrive/c/cygwin64/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_cmdd.exe

./find_cmdd.exe

线程'< main>'惊恐于无法执行进程:系统找不到指定的文件.(操作系统错误2)',find_cmdd.rs:12

我也尝试了以下选项,

let mut cmd_find = Command::new("find").....

我收到 FIND:无效开关错误的

.

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

I do not have the luxury of renaming/copying the find.exe to another location.

推荐答案

通过 Command 运行程序时,Cygwin基本上不存在.执行进程使用操作系统的本机功能.对于Windows

Cygwin basically doesn't exist when you are running a program via Command. Executing a process uses the operating system's native functionality; in the case of Windows that's CreateProcessW.

这意味着:

  1. 您的cygwin shell设置的 PATH 变量在启动进程时可能没有任何意义.
  2. 带有/cygdrive/...的目录结构在Windows中实际上不存在;那是一件神器.
  1. The PATH variable set by your cygwin shell may or may not mean anything when starting a process.
  2. The directory structure with /cygdrive/... doesn't actually exist in Windows; that's an artifact.

所有这些,您必须使用Windows本地路径:

All that said, you have to use Windows-native paths:

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上似乎没有任何影响...

As a side note, I have no idea what piping standard input to find does; it doesn't seem to have any effect for me on Msys2 or on OS X...

这篇关于在Cygwin上使用`std :: process :: Command`执行`find`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆