You are not logged in.

#1 2011-03-08 02:39:31

Haptic
Member
Registered: 2009-09-03
Posts: 149

Bash Function Error

I am having problems with an extract function copied from both the wiki and the thread 'post your alias.'

The Error:

bash: syntax error near unexpected token `newline'

The Extract function (from wiki)

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
}

The Extract function (from the thread)

#!/bin/bash
#sourced by ~/.bashrc

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

It seems that its specific to me since other people are using these functions fine. I originally had the same error in zsh so I switch back to bash, but the error still occurred. What is causing this?

Last edited by Haptic (2011-03-08 02:39:43)

Offline

#2 2011-03-08 03:53:47

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash Function Error

Works for me if I copy/paste directly from this thread here. I'm guessing you've got some carriage returns mixed in. Either pipe the containing file to 'cat -A' or view it in an editor like vim that will show unprintable characters -- you're looking for ^M escapes. Delete them.

Offline

#3 2011-03-10 03:05:31

Haptic
Member
Registered: 2009-09-03
Posts: 149

Re: Bash Function Error

#!/bin/bash$
#sourced by ~/.bashrc$
$
# uncompress depending on extension...$
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$
}$

Here's the 'cat -A' output. I don't see any ^M's or anything weird.

Offline

#4 2011-03-15 02:12:58

Haptic
Member
Registered: 2009-09-03
Posts: 149

Re: Bash Function Error

I'm back on zsh and I get pretty much the same error:

zsh: parse error near `\n'

Why won't these scripts work for me?

Offline

#5 2013-07-17 07:50:51

AirOnSkin
Member
From: Switzerland
Registered: 2008-12-09
Posts: 11

Re: Bash Function Error

I was fighting with this a bit as well.

Replace this line

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

with this one.

*.tgz|*.tlz|*.txz|*.tb2|*.tbz|*.tbz2|*.taz|*.tar|*.tar.Z|*.tar.bz|*.tar.bz2|*.tar.gz|*.tar.lzma|*.tar.xz)

This basically just expands the wildcards.

Have fun,
Air

Offline

#6 2013-07-17 08:40:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash Function Error

The original works for me ...

Offline

#7 2013-07-17 08:43:11

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Bash Function Error


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB