Register
It is currently Thu Oct 23, 2014 1:51 pm

Check if a program is installed?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Jun 17, 2011 11:55 am   

Joined: Wed Jun 15, 2011 4:05 pm
Posts: 3
Say I wanted to put in a bash script a check to install a program that's not already installed, what would be the exit code of an installed program? With .rpms I could do
Code:
SVN="subversion"
rpm -q $SVN >/dev/null 2>&1
# 'rpm' exits with a 0 if the package is in the system
if [ "$?" -ne "0" ]; then
        yum -y install $SVN
fi
I tried in Ubuntu with
Code:
    if [ "subversion" != "0" ]; then
        echo "No suitable SVN package installed. Grabbing one now . . ."; sleep 1
        apt-get install -y subversion >/dev/null && echo "Got one."
    else
        echo -e "Dependencies met. Continuing on . . .\n"; sleep 1
    fi
But it always does the 'then' action and never the 'else' action - regardless of if subversion is installed already. So this doesn't seem to be right? What's the exit status in apt-get if not 0? Or if you can tell me a better solution then I'm game for that too. :)


Top
 Profile  
 PostPosted: Sat Jun 18, 2011 6:00 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
On debian based systems you can use 'dpkg' to check for installed packages.
In your example you could replace your rpm -q with dpkg -s (status) and your check is done.


Top
 Profile  
 PostPosted: Sat Jun 18, 2011 9:53 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
The best way is to use type(0). ( « help type » )

Code:
if ! type -p subversion > /dev/null; then
    # subversion(1) is not installed on the system
    exit 1
fi


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP