首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能从bash重写某个函数到nushell

不能从bash重写某个函数到nushell
EN

Stack Overflow用户
提问于 2022-11-25 16:16:29
回答 1查看 30关注 0票数 1

我要从巴什搬到努希尔。我的一个步骤是移动这个函数:

代码语言:javascript
复制
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
}

我在“努谢尔”中写道:

代码语言:javascript
复制
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;
                }
        }
}

但是当我用这个命令测试它时(我在执行命令的目录中有一个openssl源代码存档):ex openssl-1.1.1.tar.gz,我得到了以下错误:

代码语言:javascript
复制
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)

我不明白问题出在哪里。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-25 20:11:24

主要问题是您仍然试图使用Bash模式进行字符串匹配。您可以在Nushell中使用以下两种方法来完成这一任务:

  • ends-with字符串比较操作符:

如果$file以“.tbz2”结尾.

  • 或正则表达式比较:

如果$file =~ '.*.tbz2‘.

但是,您可能会考虑一种更实用的/数据驱动的/Nushell方式:

代码语言:javascript
复制
def ex [$file?: string] {
    let archivers = [
        [ pattern         , command              , options       ];
        [ ".tar.bz2"      , "tar"                , "xjf"         ]
        [ ".tar.gz"       , "tar"                , "xzf"         ]
        [ ".bz2"          , "unzip2"             , ""            ]
        [ ".tar.bz2"      , "tar"                , "xjf"         ]
        [ ".rar"          , "unrar"              , ""            ]
        [ ".gz"           , "gunzip"             , ""            ]
        [ ".tar"          , "tar"                , "xf"          ]
        [ ".tbz2"         , "tar"                , "xjf"         ]
        [ ".tgz"          , "tar"                , "xzf"         ]
        [ ".zip"          , "unzip"              , ""            ]
        [ ".Z"            , "uncompress"         , ""            ]
        [ ".7z"           , "7z"                 , "x"           ]
    ]

    if $file == null {
        print -e "No file defined"
    } else if not ($file | path exists) {
        print -e $"Can't find ($file)"
    } else {
        let matchingArchivers = ($archivers | where { |archiver| $file ends-with $archiver.pattern })
        if ($matchingArchivers | length) > 0 {
            let archiver = $matchingArchivers.0
            run-external $archiver.command $archiver.options $file
        } else {
            print -e $"($file) cannot be extracted via ex\(\)"
        }
    }
}

备注:

使用这种形式的

  • ,进行更改和添加要容易得多;在这个意义上更像Bash的case
  • I添加了另一个丢失的逻辑,比如(a)检查文件是否存在,以及(b)检查是否有匹配的应用
  • 修复了rar文件上使用的unzip命令。
  • 是的,它是基于每一个可能的匹配,而不是caseif/else的正常“短路”行为,但它的影响很小。
  • 使用第一个匹配来模仿case的行为。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74575468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档