You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将bash解压函数迁移至Nushell时遇执行错误求助

问题:Nushell自定义解压函数执行报错

从Bash迁移至Nushell后,将原Bash的ex解压函数改写为Nushell版本,执行ex openssl-1.1.1.tar.gz时触发nu::shell::external_command错误,提示“No such file or directory (os error 2)”。

原Bash函数

ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       unrar x $1     ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

改写的Nushell函数

def ex [$file?: string] {
        if $file == null {"No file defined"} else {
                if $file == *.tar.bz2 {
                        tar xjf $file;
                }
                else if $file == *.tar.gz {
                        tar xzf $file;
                }
                else if $file == *.bz2 {
                        bunzip2 $file;
                }
                else if $file == *.rar {
                        unzip $file;
                }
                else if $file == *.gz {
                        gunzip $file;
                }
                else if $file == *.tar {
                        tar xf $file;
                }
                else if $file == *.tbz2 {
                        tar xjf $file;
                }
                else if $file == *.tgz {
                        tar xzf $file;
                }
                else if $file == *.zip {
                        unzip $file;
                }
                else if $file == *.Z {
                        uncompress $file;
                }
                else if $file == *.7z {
                        7z x $file;
                }
        }
}

报错信息

ex openssl-1.1.1.tar.gz                                                                                          
Error: nu::shell::external_command (link)

  × External command failed
     ╭─[/home/ysyltbya/.config/nushell/config.nu:523:1]
 523 │          }
 524 │          else if $file == *.tar.gz {
     ·   ──┬─
     ·     ╰── did you mean 'ls'?
 525 │                  tar xzf $file;
     ╰────
  help: No such file or directory (os error 2)

问题原因及解决方法

核心错误原因

Nushell中不能直接用$file == *.tar.gz这种语法——这里的*.tar.gz会被解析为尝试执行名为*.tar.gz的外部命令,而非通配符匹配,系统找不到这个命令,所以抛出“找不到文件或目录”的错误。

另外原函数还缺少两个关键逻辑:

  1. 未判断输入的$file是否为存在的文件(对应Bash里的[ -f $1 ])
  2. 未处理不支持的文件格式的默认情况

修正后的Nushell函数

推荐使用Nushell的match表达式(更接近Bash的case语法),同时补全缺失的逻辑:

def ex [$file?: string] {
    if $file == null {
        print "No file defined"
        return
    }

    if not (($file | path exists) and ($file | path is-file)) {
        print "'($file)' is not a valid file"
        return
    }

    match $file {
        *.tar.bz2 | *.tbz2 => tar xjf $file
        *.tar.gz | *.tgz => tar xzf $file
        *.bz2 => bunzip2 $file
        *.rar => unrar x $file  # 原函数此处误写为unzip,已修正
        *.gz => gunzip $file
        *.tar => tar xf $file
        *.zip => unzip $file
        *.Z => uncompress $file
        *.7z => 7z x $file
        _ => print "'($file)' cannot be extracted via ex()"
    }
}

关键修正点说明

  1. 使用match+通配符匹配:Nushell的match支持*.xxx这种通配符语法,正确实现格式匹配
  2. 补全文件有效性判断:用path exists和path is-file检查输入是否为存在的文件
  3. 修正原函数的错误:原Nushell函数中*.rar分支错误调用了unzip,改为正确的unrar x $file
  4. 添加默认分支:处理不支持的文件格式,对应Bash里的*分支

内容的提问来源于stack exchange,提问作者YaSyelTebya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:50:33