0
I am trying to add some functions to my bashrc, namely:
h () { history | fgrep $1 | sort -u | cut -b 8- }
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar e $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 "I don't know how to extract '$1'." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
using the following command:
echo -e "\
h () { history | fgrep \044\061 | sort -u | cut -b 8- }
extract () {
if [ -f \044\061 ] ; then
case \044\061 in
*.tar.bz2) tar xvjf \044\061 ;;
*.tar.gz) tar xvzf \044\061 ;;
*.bz2) bunzip2 \044\061 ;;
*.rar) rar e \044\061 ;;
*.gz) gunzip \044\061 ;;
*.tar) tar xvf \044\061 ;;
*.tbz2) tar xvjf \044\061 ;;
*.tgz) tar xvzf \044\061 ;;
*.zip) unzip \044\061 ;;
*.Z) uncompress \044\061 ;;
*.7z) 7z x \044\061 ;;
*) echo \042I don't know how to extract '\044\061'.\042 ;;
esac
else
echo \042'\044\061' is not a valid file\041\042
fi
}" >> ~/.bashrc
...but this results in bash: /home/chris/.bashrc: line 123: syntax error: unexpected end of file
when bash starts up. If I remove these functions, this error goes away. Is there some closing statement I must add to the functions section?
+1 That's right, there's no need to do all that octal escaping if you do it this way. But you could do
\$1
which would be more readable than\044\061
. – Paused until further notice. – 2011-01-25T17:31:05.533@Dennis Williamson Why not just use
nano
or some other editor vs redirection? – Just Jake – 2011-01-25T17:48:26.550@Just Jake: Perhaps this needs to be deployed repeatedly, possibly for multiple accounts or on multiple machines. Doing it with a script automates it. However, it should probably be more robust, for example checking that it hasn't already been done. It's possible the OP is doing that and we just don't see that part. – Paused until further notice. – 2011-01-25T17:53:48.040
It does need to be automated, indeed. I'll test this in a few minutes (off for dinner now). Thanks! :) – Matthieu Cartier – 2011-01-25T18:06:47.863
Hm,
$1
still seems to need escaping, or it gets interpreted (how should I even escape it when it gets redirected?). Is there something I'm missing? – Matthieu Cartier – 2011-01-25T19:07:31.883Ah, you're right. I didn't notice that when I tried it earlier. Updated to reflect that. – bahamat – 2011-01-25T19:47:44.300
\$1
still doesn't seem to work, which was my confusion (tried that before posting my reply), it keeps the \ and continues to interpret$1
– Matthieu Cartier – 2011-01-25T22:31:00.877Are you using
cat >> .bashrc << EOF
? It works for me. If you are trying to useecho
you need to use single quotes, not double quotes. – bahamat – 2011-01-25T22:58:45.640Yup, I'm using the exact method you've posted. OS is Ubuntu 10.04. – Matthieu Cartier – 2011-01-26T10:11:40.877
Bump, any ideas? :) – Matthieu Cartier – 2011-01-28T17:44:13.680
I have no idea. It works for me. I've tried everything I can think of to do it wrong and I can't break it. I tried pasting it, putting it in a script with each of
#!/bin/bash
,#!/bin/sh
and#!/bin/zsh
and everything works for me. What version of bash are you using? Runecho $BASH_VERSION
. – bahamat – 2011-01-29T02:55:52.113echo $BASH_VERSION
returns4.1.5(1)-release
. – Matthieu Cartier – 2011-01-30T11:08:52.187In the end I've just
cat
'ed in a file. Thanks anyway, but since this seems to work for some, I'll accept it. :) – Matthieu Cartier – 2011-01-31T13:59:14.883