2

I have the following function in my .bashrc file:

extract() {
    local c e i

    (($#)) || return

    for i; do
        c=''
        e=1

        if [[ ! -r $i ]]; then
            echo "$0: file is unreadable: \`$i'" >&2
            continue
        fi

        case $i in
        *.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
               c='bsdtar xvf';;
        *.7z)  c='7z x';;
        *.Z)   c='uncompress';;
        *.bz2) c='bunzip2';;
        *.exe) c='cabextract';;
        *.gz)  c='gunzip';;
        *.rar) c='unrar x';;
        *.xz)  c='unxz';;
        *.zip) c='unzip';;
        *)     echo "$0: unrecognized file extension: \`$i'" >&2
               continue;;
        esac

        command $c "$i"
        e=$?
    done

    return $e
}

now this has been working perfectly for me on my current Arch Linux system. Recently, I installed the new, void-linux distro and tried to use my old .bashrc on it.

However, on Void-Linux, this functions throws an error:

syntax error near unexpected token '('

and points to this line:

*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))

Some investigation returned that the Bash on that distro refuses to read the @() pattern and hence returns an error. I remember using the same function on a Debian Stable system a couple of months ago too.

Can anyone point out why this code doesn't seem to be portable? And where the error is?

  • I was counting the number of parenthesis you have at the end statement. It may be possible you have too many. – ryekayo Sep 16 '14 at 17:25
  • No, I checked that multiple times. I think you found 1 extra paren which is a part of the switch-case syntax. This function currently works for me on Arch Linux, too. – darnir Sep 16 '14 at 17:26
  • 1
    have you enabled extglob before trying to define that function? – llua Sep 16 '14 at 17:33
  • Damn! I'm enabling extglob, but after the function is defined. Once I fixed the order, it works! I never came across this issue in the past, maybe all the other systems were setting extglob globally. Thanks a bunch! – darnir Sep 16 '14 at 17:38
6

As mentioned in my comment, that is a common error people see when trying to use extended globs(shopt -s extglob) before enabling the option. This includes function definitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.