0

I have a fairly simple bash script which I need to make a bit more complicated, but I'm not certain of how to do it. The script, so far, is like so:

    #!bin/bash

    if rails -v | grep -q "3.2"
     then
    echo "Rails 3.2 installed. Uninstalling and adding Rails 3.1.4."
    gem uninstall rails -v=$version
    gem install rails -v=3.1.4
     else
    echo "Rails 3.2 not installed. Exiting."
    exit 1
 fi

As you can see, it's not complete and pretty basic. The overall goal of this script is to see if said server has rails 3.2.x installed and, if so, to then uninstall it and install rails 3.1.4. I've got everything covered but the actual insertion of the required version into the "gem uninstall" portion. The $version part should be replaced with the version number output by rails -v on the server.

Any assistance with this is appreciated.

Thanks.

3 Answers 3

1

Something like

gem uninstall rails -v=$(rails -v | grep -o "3.2.*")

Tailor your regular expression to match just the version number, and grep will output just that.

Sign up to request clarification or add additional context in comments.

1 Comment

Or, more generally, grep -o '[0-9]\+\..*'
0
 gem uninstall rails -v=$( rails -v )

5 Comments

What is the output of rails -v?
@Striketh: Try gem uninstall rails -v=$( rails -v | sed 's/.* //' ) (or other ways to extract only the version number).
@William - It's always going to be "Rails x.x.x" with the x's representing the version number.
@DennisWilliamson: This works, but it's not 100%. When the command is executed it goes into rails manual prompt requiring a Y or N about uninstalling rails, and I don't want any manual input. Is there a way to set the variable earlier in with something like version=( rails -v | sed 's/.* //' ) and then simply have the $version variable inserted into the gem uninstall line to bypass this prompt? When entered directly as gem uninstall rails -v=3.2.3 for example, this prompt is bypassed.
Disregard - just needed the -I flag in the rails command. Works perfectly with that.
0

Try surrounding it with quotes, like this: gem uninstall rails -v="$( rails -v | sed 's/.* //' )".

Comments

Your Answer

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.