0

The shell script below runs without error but when it finishes , I discovered that it only ran the first part of the script that installs rbenv but doesn't call all the lines from echo "========= Cloning ruby build =========" till the end. I am using Ubuntu 13.10 the Lubuntu variant.

If I type each command in the script directly into the terminal everything works and ruby 2.1.2 is installed but if run the script instead, ruby_build and ruby 2.1.2 are not installed though rbenv is.

Any suggestion how to resolve this.

rbenv_ruby_installer.sh

   rbenv_install.sh

   sudo apt-get -y  update && apt-get upgrade 

   echo "======================================="
   echo "========= Cloning Rbenv ========="
   echo "======================================="

   cd
   git clone git://github.com/sstephenson/rbenv.git .rbenv
   echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
   echo 'eval "$(rbenv init -)"' >> ~/.bashrc
   exec $SHELL

   echo "======================================="
   echo "========= Cloning ruby build ========="
   echo "======================================="

   git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
   echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
   exec $SHELL

   echo "======================================="
   echo "========= Installing Ruby with Rbenv ========="
   echo "======================================="
   rbenv install 2.1.2
   rbenv global 2.1.2
   rbenv rehash
   ruby -v
brg
  • 3,915
  • 8
  • 37
  • 66
  • "exec $SHELL" is your problem. That causes the shell to replace itself with no arguments, no script, nothing to do. It exits. – Bruce K May 30 '14 at 16:41
  • Thanks for the comment, so should I use **source ~/.bashrc** – brg May 30 '14 at 16:43
  • No. You should not be modifying ~/.bashrc and just append the directories to your current value for PATH: PATH+=:/path/to/whereever – Bruce K May 30 '14 at 16:44
  • Ok thanks, but one last question. If the path is not added to ~/.bashrc, how do I ensure each time I exit the terminal and start a new terminal instance, this path is not lost? Something along that lines was discussed here: http://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux – brg May 30 '14 at 17:37
  • Yeah, you just don't want to append to .bashrc every time you run a script. Typically, my bashrc has a loop looking for various directories and adds them to PATH (and MANPATH, etc.) as they are located. – Bruce K May 30 '14 at 17:41

1 Answers1

1

Don't use:

exec $SHELL

instead add logic to your ~/.bashrc to look for these directories. In scripts that install (clone) hierarchies from somewhere, they may want new directories in their PATH, but do not auto-edit your ~/.bashrc file every time you run a script. Add search logic a la:

for d in $HOME/.rbenv/bin $HOME/.rbenv/plugins/ruby-build/bin
do test -d $d && PATH+=:$d ; done
Bruce K
  • 749
  • 5
  • 15