3
\$\begingroup\$

I want to write an automated bash script for installing Ruby and Rails using rbenv, but I am getting session reload issues in the terminal.

#!/bin/bash
echo "Installing rbenv.."
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
reset
echo "Finished installing rbenv"
#!/bin/bash
rbenv -v
echo "Installing ruby.."
sudo rbenv install 2.2.3
rbenv global 2.2.3
echo "Finished installing ruby"
ruby -v
echo "gem: --no-document" > ~/.gemrc
echo "Installing rails.."
sudo apt-get install ruby-dev
sudo gem install bundler
gem install rails -v 4.2.3
rbenv rehash
echo "Finished installing rails"
rails -v
rbenv rehash
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 26, 2016 at 6:07
\$\endgroup\$
1
  • 1
    \$\begingroup\$ What do you mean by this: "But I am getting issue of session reload in terminal." \$\endgroup\$ Commented Jun 4, 2016 at 18:39

1 Answer 1

1
\$\begingroup\$

Not a lot can be improved on a simple installer script like this. I have a few tips but nothing really significant.

The script doesn't check if it has already been executed. If you run it again by mistake, it will append to ~/.bashrc lines that were already there.

As a simple workaround, you could add -e flag to the shebang:

#!/bin/bash -e

This will cause the script to exit on the first error. Which will happen on the first git clone command, which will fail because the local clone already exists. The script will stop there and not reach the lines that append again to .bashrc.

Speaking of shebang, you have a second #!/bin/bash in the middle of the script, which was probably a copy-paste mistake and has no purpose there, so should be removed.

answered Jun 4, 2016 at 18:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.