You are not logged in.

#1 2009-03-03 05:55:11

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

How to check if a particular package is installed?

I am writing a script where I need to check the existence of ruby-gtk2., If it is installed, I start the gui version of the program and if not, then I want to start the CLI version of the program.

How would I check this in a script?


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#2 2009-03-03 06:22:55

evr
Arch Linux f@h Team Member
Registered: 2009-01-23
Posts: 554

Re: How to check if a particular package is installed?

maybe something like this?

#!/bin/bash
x=`pacman -Qs ruby-gtk2`
if [ -n "$x" ]
 then echo "Installed"
 else echo "Not Installed"
fi

Offline

#3 2009-03-03 06:27:40

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: How to check if a particular package is installed?

if (pacman -Q ruby-gtk2 >/dev/null) ; then echo "ruby-gtk2 is installed"; fi

But I'd probably simply check for the existance of a file:

if [ -f /usr/lib/ruby/site_ruby/1.8/gtk2.rb ]; then...

1000

Offline

#4 2009-03-03 06:39:58

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to check if a particular package is installed?

Thanks guys. evr your answer seems obvious -- now why didn't I think of that, but of course it only applies to Arch.

byte, your 2nd option of checking for the existence of the file seems logical, but what if ruby upgrades to 1.9, I would have to refactor the script, correct?

I think using pacman would be the best bet here.

Last edited by Inxsible (2009-03-03 06:43:21)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#5 2009-03-03 18:44:51

winch
Member
Registered: 2008-04-13
Posts: 43

Re: How to check if a particular package is installed?

begin
    require 'gtk2'
    rescue LoadError
        puts 'ruby-gtk2 not installed'
end

It's simple enough to use ruby to test if ruby-gtk2 is installed or not.

Another option would be to check if the gui app returns a specific value or piece or text when ruby-gtk2 is missing that you could check for.

Offline

#6 2009-03-03 18:59:12

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Re: How to check if a particular package is installed?

if you know exactly how the binaries should be named, you don't need to even know where they are , you don't need pacman , and you're not bound to Arch.

which $PROGRAM_NAME >/dev/null && echo "program installed"

< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#7 2009-03-03 19:43:57

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to check if a particular package is installed?

winch wrote:
begin
    require 'gtk2'
    rescue LoadError
        puts 'ruby-gtk2 not installed'
end

It's simple enough to use ruby to test if ruby-gtk2 is installed or not.

Another option would be to check if the gui app returns a specific value or piece or text when ruby-gtk2 is missing that you could check for.

yes. I know how to do that with ruby. The code actually does check for this. But I wanted to put it in a shell script, so that I could use the same PKGBUILD for the CLI version and the gtk version.

The author's code simply spits out an error message when you start up the gtk version without having ruby-gtk installed. So I just created a script which checks this beforehand and accordingly starts either the command line app or the gtk app.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#8 2009-03-03 19:46:41

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to check if a particular package is installed?

Dieter@be wrote:

if you know exactly how the binaries should be named, you don't need to even know where they are , you don't need pacman , and you're not bound to Arch.

which $PROGRAM_NAME >/dev/null && echo "program installed"

That seems even better. However, the PKGBUILD is for Arch and on top of that, the program name also differs according to the distro. For eg. Debian has that package as libgtk2-ruby. So when I search for ruby-gtk2 using the 'which' command, I am pretty much restricting to Arch again (or any other distro which uses the ruby-gtk2 as the package name)

But yeah, using which is better I guess, since that is a built in function.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#9 2009-03-06 12:01:02

Xilon
Member
Registered: 2007-01-01
Posts: 243

Re: How to check if a particular package is installed?

Inxsible wrote:
winch wrote:
begin
    require 'gtk2'
    rescue LoadError
        puts 'ruby-gtk2 not installed'
end

It's simple enough to use ruby to test if ruby-gtk2 is installed or not.

Another option would be to check if the gui app returns a specific value or piece or text when ruby-gtk2 is missing that you could check for.

yes. I know how to do that with ruby. The code actually does check for this. But I wanted to put it in a shell script, so that I could use the same PKGBUILD for the CLI version and the gtk version.

The author's code simply spits out an error message when you start up the gtk version without having ruby-gtk installed. So I just created a script which checks this beforehand and accordingly starts either the command line app or the gtk app.

Perhaps it would be better to explain what you're trying to do. It seems that what you really want is to simply specify ruby-gtk2 in optdepends in the PKGBUILD. If you really need the script solution, and this is for an Archlinux package, then the lack of portability is a non-issue, so the pacman solution is fine.

Offline

#10 2009-03-06 16:18:54

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to check if a particular package is installed?

Xilon wrote:

Perhaps it would be better to explain what you're trying to do. It seems that what you really want is to simply specify ruby-gtk2 in optdepends in the PKGBUILD. If you really need the script solution, and this is for an Archlinux package, then the lack of portability is a non-issue, so the pacman solution is fine.

I just got this software which can be used via CLI or GTK. The author checks for ruby-gtk2 and then puts an error message saying that you do not have ruby-gtk2 installed.

However, I wanted to build just 1 PKGBUILD for the CLI version as well as the GTK version, that's why I decided to build an sh script which will check for ruby-gtk2 and then if available start the GUI version or else start the CLI version. Of course if you do have ruby-gtk2 installed and still want to start the CLI version, you will have to create a different script. Maybe later down the line, I will put in two PKGBUILDS - one for CLI and the other for GTK

But its all done, Here's the app that I submitted to AUR.

Last edited by Inxsible (2009-03-06 16:20:53)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#11 2009-03-08 14:00:21

winch
Member
Registered: 2008-04-13
Posts: 43

Re: How to check if a particular package is installed?

Looking at the pkgbuild I don't understand why this script  is needed. Upstream provides two separate programs so why try to merge them into one? There isn't any reason why a package can't put more than one file in /usr/bin.

Offline

#12 2009-03-08 23:08:22

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to check if a particular package is installed?

winch wrote:

Looking at the pkgbuild I don't understand why this script  is needed. Upstream provides two separate programs so why try to merge them into one? There isn't any reason why a package can't put more than one file in /usr/bin.

you are absolutely right. There is no need for it. I did that because I didn't want to put 2 PKGBUILDs for the same app and still allow users who did not want to install ruby-gtk2, to be able to use the package. As of now ruby-gtk2 is in optdepends.

But I guess I have come around that and as I mentioned in my earlier post, I will be changing it and submitting two PKGBUILDS to AUR, so users can either download the CLI version or the gtk version.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

Board footer

Powered by FluxBB