Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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
share|improve this answer
    
+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. –  Dennis Williamson Jan 25 '11 at 17:31
    
@Dennis Williamson Why not just use nano or some other editor vs redirection? –  Just Jake Jan 25 '11 at 17:48
    
@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. –  Dennis Williamson Jan 25 '11 at 17:53
    
It does need to be automated, indeed. I'll test this in a few minutes (off for dinner now). Thanks! :) –  Matthieu Cartier Jan 25 '11 at 18:06
    
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 Jan 25 '11 at 19:07
show 8 more comments

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.

share|improve this answer
1  
The shebang seems to do nothing (but isn't that expected since this is .bashrc?) –  Matthieu Cartier Jan 25 '11 at 17:09
    
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 Jan 25 '11 at 17:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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