Grabbing the output of a command and inserting it into a bash script

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:


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

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.



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.

 gem uninstall rails -v=$( rails -v )

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

Need Your Help

How do I fill the value of an input element with Mechanize?

ruby nokogiri mechanize

How do I change fill the value of an input element with Mechanize?