Rust Command无法执行*拓展解决办法

rust 复制代码
async fn run_cmd_async_out<I, S>(cmd: &str, args: I, timeout_s: u64, with_http_proxy: bool) -> Result<String>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let mut cmd = tokio::process::Command::new(cmd);
    
    // 让 sh 来运行命令,使得通配符能够被 shell 解析
    let cmd = cmd.arg("-c").arg(cmd).args(args);

    if with_http_proxy {
        // 设置 HTTP 代理
        if let Ok(c) = fs_read_line(CONF_HTTP_PROXY) {
            cmd.env("http_proxy", c);
        }

        if let Ok(c) = fs_read_line(CONF_HTTPS_PROXY) {
            cmd.env("https_proxy", c);
        }
    }
    
    let mut child = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().context("wait stdout/stderr failed")?;

    match timeout(Duration::from_secs(timeout_s), child.wait()).await {
        Ok(Ok(_)) => {
            let output = child.wait_with_output().await?;
            if !output.status.success() {
                Err(anyhow!("run cmd failed, {}", String::from_utf8_lossy(output.stderr.as_slice())))
            } else {
                let status = String::from_utf8_lossy(output.stdout.as_slice()).to_string();
                Ok(status)
            }
        }
        Ok(Err(e)) => Err(anyhow!("running cmd error, {e}")),
        Err(e) => {
            let _ = child.start_kill();
            let _ = child.wait().await;
            Err(anyhow!("running cmd timeout, {e}"))
        }
    }
}

调用的示例 。

rust 复制代码
system_async_run("sh", &vec!["-c", "rm -f /var/backups/*.pcap"]).await?;

解释:

在 Linux 中,当你使用命令如 rm -rf /var/volatile/*.pcap 时,* 是由 shell 进行扩展的,它将通配符替换为实际的文件名列表。在使用 tokio::process::Command 时,这种通配符扩展不会自动发生,因为 Command 直接调用的是操作系统的命令,而没有通过 shell 来进行扩展。

要解决这个问题,你可以让 tokio::process::Command 在运行时通过 shell 来执行该命令,从而使得 * 符号能够得到正确的扩展。你可以通过设置 sh 来实现这一点。

注: 妈的。好坑。折腾我半天。以为删除不了文件。

相关推荐
GetcharZp1 天前
告别 jq 噩梦!这款 JSON 神器 fx 让你在终端体验“丝滑”的数据操作
后端
2401_873479401 天前
如何利用IP查询定位识别电商刷单?4个关键指标+工具配置方案
开发语言·tcp/ip·php
我爱cope1 天前
【从0开始学设计模式-10| 装饰模式】
java·开发语言·设计模式
菜鸟学Python1 天前
Python生态在悄悄改变:FastAPI全面反超,Django和Flask还行吗?
开发语言·python·django·flask·fastapi
小码哥_常1 天前
告别臃肿!Elasticsearch平替Manticore登场
后端
浪浪小洋1 天前
c++ qt课设定制
开发语言·c++
charlie1145141911 天前
嵌入式C++工程实践第16篇:第四次重构 —— LED模板,从通用GPIO到专用抽象
c语言·开发语言·c++·驱动开发·嵌入式硬件·重构
故事和你911 天前
洛谷-数据结构1-4-图的基本应用1
开发语言·数据结构·算法·深度优先·动态规划·图论
苍何1 天前
万字保姆级教程:Hermes+Kimi K2.6 打造7x24h Agent军团
后端
我叫黑大帅1 天前
为什么map查找时间复杂度是O(1)?
后端·算法·面试