未能在 Fish 中构建通用压缩函数

Art*_*ang 2 linux compression shell fish

我在Linux上使用fish shell,我想编写一个适用于大多数压缩文件格式的函数,我写的是:

function ex
    if test -f $argv[1]
        switch (file $argv[1])
            case '*.tar.bz2'
                tar xjf $argv[1]
            case '*.tar.gz' '*.tgz'
                tar xvf $argv[1]
            case '*.bz2'
                bunzip2 $argv[1]
            case '*.rar'
                unrar x $argv[1]
            case '*.gz'
                gunzip $argv[1]
            case '*.tar'
                tar xf $argv[1]
            case '*.tbz2'
                tar xjf $argv[1]
            case '*.zip'
                unzip $argv[1]
            case '*.Z'
                uncompress $argv[1]
            case '*.7z'
                7z x $argv[1]
        end
    else
        echo "'$argv[1]' is not a valid file"
    end
end
Run Code Online (Sandbox Code Playgroud)

但是,当我将其应用于 Fish shell 中的 tar.gz 文件时,没有给出错误,但也没有提取它。然后当我尝试tar xjf该文件时,它被提取。

这让我困惑了一段时间。

Mar*_*ler 5

您正在切换命令的结果file,而不是名称。