title: Installing rbenv through shell script
body: <p>I've created a shell script to install rbenv onto a fresh virtual machine, everyone seems to be working fine, however, once and a while it seems that rbenv fails to install.  I'd like to add some extra error checking throughout the way to ensure that the install happens, and if something failed, re-start the install.  </p>
  
  <p>Some notes:</p>
  
  <ul>
  <li>This is a small part of a larger script</li>
  <li>This is all done through <code>whiptail</code> dialog</li>
  <li>I'm redirecting the output to an events.log file <code>&gt;&amp;$log_fd...</code></li>
  </ul>
  
  <p>This is what I currently have:</p>
  
  <pre><code>set_rbenv(){
    # install rbenv
    RUBY_VERSION="2.1.4"
    RBENV_ROOT="/root/.rbenv"
    PROFILE="/root/.bashrc"
  
    # check if rbenv is installed
    if [[ ! `which rbenv` ]]; then
  
      if [[ ! -d "$RBENV_ROOT" ]]; then
        echo "Cloning rbenv..." &gt;&amp;$log_fd 2&gt;&amp;1
        git clone https://github.com/rbenv/rbenv.git $RBENV_ROOT &gt;&amp;$log_fd 2&gt;&amp;1
      fi
  
      if [[ ! -d "$RBENV_ROOT/plugins/ruby-build" ]]; then
        git clone https://github.com/rbenv/ruby-build.git $RBENV_ROOT/plugins/ruby-build &gt;&amp;$log_fd 2&gt;&amp;1
      fi
  
      if [[ ! `grep -e '/.rbenv\/bin' $PROFILE` ]]; then
        echo "rbenv not in PATH ... adding..." &gt;&amp;$log_fd 2&gt;&amp;1
        echo 'export PATH="$HOME/.rbenv/bin:$PATH"' &gt;&gt; $PROFILE 2&gt;&amp;1
      fi
      if [[ ! `grep -e '^\eval "$(rbenv init -)"' $PROFILE` ]]; then
        echo "rbenv init not in .bashrc... adding..." &gt;&amp;$log_fd 2&gt;&amp;1
        echo 'eval "$(rbenv init -)"' &gt;&gt; $PROFILE 2&gt;&amp;1
      fi
      . $PROFILE
      echo "Finished Installing rbenv..." &gt;&amp;$log_fd 2&gt;&amp;1
    else
      echo "rbenv already installed... skipping!" &gt;&amp;$log_fd 2&gt;&amp;1
    fi
  
    if [ ! -d "$RBENV_ROOT/versions" ]; then
      echo "$RBENV_ROOT/versions doesn't exist ... creating..."
      mkdir $RBENV_ROOT/versions &gt;&amp;$log_fd 2&gt;&amp;1
    fi
  
    if [ ! -d "$RBENV_ROOT/versions/$RUBY_VERSION" ]; then
      echo "$RBENV_ROOT/versions/$RUBY_VERSION doesn't exist ... creating..." &gt;&amp;$log_fd 2&gt;&amp;1
      mkdir $RBENV_ROOT/versions/$RBENV_VERSION &gt;&amp;$log_fd 2&gt;&amp;1
    fi
  
    # check if ruby version is installed
    if [[ ! `rbenv version | grep $RUBY_VERSION` ]]; then
      echo "installing ruby 2.1.4" &gt;&amp;$log_fd 2&gt;&amp;1
      rbenv install $RUBY_VERSION &gt;&amp;$log_fd 2&gt;&amp;1
      rbenv global $RUBY_VERSION &gt;&amp;$log_fd 2&gt;&amp;1
  
      if [ ! -d "$RBENV_ROOT/versions/$RUBY_VERSION/etc" ]; then
        echo "$RBENV_ROOT/versions/$RUBY_VERSION/etc doesn't exist ... creating..." &gt;&amp;$log_fd 2&gt;&amp;1
        mkdir $RBENV_ROOT/versions/$RUBY_VERSION/etc &gt;&amp;$log_fd 2&gt;&amp;1
      fi
  
    else
      echo "ruby $RUBY_VERSION already exists... skipping!" &gt;&amp;$log_fd 2&gt;&amp;1
    fi
    (( STATUS += 35 ))
    echo $STATUS
  }
  </code></pre>
  
  <p>While the install itself can fail at anytime on the new VM, I'm screwed if rbenv installs, but nothing else does (like ruby-build) or the $PATH, etc.</p>
  
  <p>Would it make more sense to just encase every command in it's own <code>if</code> statement outside of the initial <code>if which rbenv</code>, or is there a more robust way to ensure the install completes successfully?</p>