You are not logged in.
Pages: 1
Topic closed
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
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
#!/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
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
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
The original works for me ...
Offline
Offline
Pages: 1
Topic closed