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:
#!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.
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/.* //' )".