Path 使用命令在MSYS下的Windows上调用shell脚本
我试图在Windows 7上的Rust(1.0 beta 3)环境中调用命令,但我不知道如何执行 假设您的主文件夹中有一个名为Path 使用命令在MSYS下的Windows上调用shell脚本,path,command,rust,msys,Path,Command,Rust,Msys,我试图在Windows 7上的Rust(1.0 beta 3)环境中调用命令,但我不知道如何执行 假设您的主文件夹中有一个名为myls的非常简单的脚本: #!/bin/bash ls 现在在Rust中创建一个调用脚本的简单程序: use std::process::Command; use std::path::Path; fn main() { let path_linux_style = Path::new("~/myls").to_str().unwrap(); l
myls
的非常简单的脚本:
#!/bin/bash
ls
现在在Rust中创建一个调用脚本的简单程序:
use std::process::Command;
use std::path::Path;
fn main()
{
let path_linux_style = Path::new("~/myls").to_str().unwrap();
let path_win_style = Path::new("C:\\msys64\\home\\yourname\\myls").to_str().unwrap();
let out_linux = Command::new(path_linux_style).output();
let out_win = Command::new(path_win_style).output();
match out_linux
{
Ok(_) => println!("Linux path is working!"),
Err(e) => println!("Linux path is not working: {}", e)
}
match out_win
{
Ok(_) => println!("Win path is working!"),
Err(e) => println!("Win path is not working: {}", e)
}
}
现在,如果您尝试执行它,您将获得以下输出:
Linux path is not working: The system cannot find the file specified.
(os error 2)
Win path is not working: %1 is not a valid Win32 application.
(os error 193)
因此,我无法在MSYS环境中调用任何命令。
我怎样才能解决它
编辑:
我注意到,如果我调用一个可执行文件,问题就不会发生,因此它似乎与bash脚本的调用有关。不管怎么说,这很烦人,因为它使得依赖外部C/C++代码(需要启动
configure
script)编译项目很难开始工作。Windows示例不起作用,因为Windows不是Unix。在类Unix系统上,#脚本开头的code>被识别,并作为解释器调用给定路径上的可执行文件。Windows没有这种行为;即使它这样做了,它也不会识别/bin/bash
的路径名,因为该路径名是msys2模拟的路径名,而不是本机路径名
相反,您可以使用msys2 shell显式执行所需的脚本,方法如下(根据需要更改路径,我安装了msys32
,因为它是32位VM)
let out_win = Command::new("C:\\msys32\\usr\\bin\\sh.exe")
.arg("-c")
.arg(path_win_style)
.output();