I need to, from a bash script, check to see if certain Ruby gems are installed .
I thought I could do something like
if ! gem list <name>; then do_stuff; fi
but testing on the command line using echo $?
shows that gem list <name>
returns 0 regardless of if name is actually found.
Does this mean I have to use grep to filter the output of gem list, or is there a better way I can check to see if a gem is installed?
gem list <name> -i
will return the stringtrue
if the gem is installed andfalse
otherwise. Also, the return codes are what you would expect.For more informations, see
gem help list
.Edit: @Riateche correctly observed that this might give false positives if you search for a gem name that is a substring of an otherwise installed gem. To avoid this, use a regex syntax:
(Example:
gem list '^mini$' -i
).It looks as if the
gem spec
command will fail with an error if the named gem is not installed. So:I don't know if this is the canonical way of solving this, but it works.
You can also use the
usage excerptquery
sub-command to thegem
command.Will also look for specific versions as well, using the
-v
switch.