.bashrc reports unexpected EOF

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?

Matthieu Cartier

Posted 2011-01-25T16:56:30.373

Reputation: 3 422

Answers

2

The first line is the problem.

h () { history | fgrep $1 | sort -u | cut -b 8- }

Change it to this:

h () { history | fgrep $1 | sort -u | cut -b 8- ; }

Or:

h () {
    history | fgrep $1 | sort -u | cut -b 8-
}

Also, an easier/cleaner way to get it into your .bashrc is with redirection (although you do still need to escape $).

cat >> .bashrc << EOF
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
}
EOF

bahamat

Posted 2011-01-25T16:56:30.373

Reputation: 5 130

+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.883

Ah, 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.877

Are you using cat >> .bashrc << EOF? It works for me. If you are trying to use echo you need to use single quotes, not double quotes. – bahamat – 2011-01-25T22:58:45.640

Yup, 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? Run echo $BASH_VERSION. – bahamat – 2011-01-29T02:55:52.113

echo $BASH_VERSION returns 4.1.5(1)-release. – Matthieu Cartier – 2011-01-30T11:08:52.187

In 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

0

Change your shebang to

#!/bin/bash -x

and run the program again, to get a line by line trace. Usually unexpected end of file is an unclosed quote or bracket.

Satanicpuppy

Posted 2011-01-25T16:56:30.373

Reputation: 6 169

1The shebang seems to do nothing (but isn't that expected since this is .bashrc?) – Matthieu Cartier – 2011-01-25T17:09:09.567

Just tried running it with ./.bashrc... I see nothing of use other than at the end ./.bashrc: line 124: syntax error: unexpected end of file. What should I be looking for? – Matthieu Cartier – 2011-01-25T17:12:10.767