16

I would like to write an installation bash script, where I would like to install MySQL server.

On Linux Mint I had followed code:

apt-get -y --force-yes install mysql-server-5.6

but I installed the new Debian 8 and there is no mysql-server - instead there is mariadb.

How can I find out if package exists?

I just know that there is dpkg -s which should tell whether a package is installed.

4
  • 3
    --force-yes deactivates all security, so you really could break your system to the point of no repair. Also: --force-yes overrides (deactivates) -y. where the latter could be the option of choice: A long form of -y is --assume-yes, and that's what it does; Saying yes until it could get really ugly, with risking havoc among core elements, fundamentals, etc. --force overrides breaks security (learned it the hard way)
    – erch
    Commented Apr 29, 2015 at 22:44
  • Addendum: You might be interested in the --simulateoption for apt-get, to do a --dry-run, just in case
    – erch
    Commented Apr 29, 2015 at 23:02
  • 1
    just to clarify: you want to find out if a package is in any of the repositories that are defined in /etc/apt/sources.list, right?
    – wullxz
    Commented Apr 29, 2015 at 23:18
  • Note that there actually is a mysql-server package in Debian 8, alongside mariadb: packages.debian.org/jessie/mysql-server
    – Dan Getz
    Commented Apr 29, 2015 at 23:59

4 Answers 4

17

(the below is from Ubuntu, but the same technique obviously works on Debian as well)

$ apt-cache show screen
Package: screen
Priority: optional
Section: misc
Installed-Size: 950
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Axel Beckert <[email protected]>
Architecture: amd64
Version: 4.1.0~20120320gitdb59704-9
Depends: libc6 (>= 2.15), libpam0g (>= 0.99.7.1), libtinfo5
Suggests: iselect (>= 1.4.0-1) | screenie | byobu
Filename: pool/main/s/screen/screen_4.1.0~20120320gitdb59704-9_amd64.deb
Size: 645730
...

If the package exists, information will be displayed. If not, you'll see something like:

$ apt-cache show foobar
N: Unable to locate package foobar
E: No packages found

Additionally, the exit code of apt-cache will be non-zero if no matching packages are found.

Additional note: If you're using apt-cache show package where package is a virtual one (one that doesn't exist, but is, for example, referenced by other packages), you'll get:

N: Can't select versions from package 'package' as it is purely virtual
N: No packages found

The exit code of this is zero (which is a bit misleading in my opinion.)

2
  • 3
    just to complete this answer: you can also search the defined repositories from the sources.list with this command: apt-cache search *searchstring*. That command will also search the description of packages and could help you find packages where you don't know the exact package name.
    – wullxz
    Commented Apr 29, 2015 at 23:22
  • 1
    @wullxz, only the apt-cache search ... returns any kind of matches and the app. returns 0 even if nothing is found, which is not practical in a bash script. Commented Feb 20, 2017 at 4:05
5

I would use dpkg -l mysql-server &> /dev/null && echo "mysql-server is installed".

It will check if the mysql-server package is installed and if so, it prints this fact to the screen. A more sophisticated solution would be, in bash (untested):

function package_exists() {
    return dpkg -l "$1" &> /dev/null
}

So one can do in a script:

if ! package_exists mysql-server ; then
    echo ”Please install mysql-server!"
fi
1
  • 8
    I think he wants to know if the package exists at all and not if it is already installed on the local machine...
    – wullxz
    Commented Apr 29, 2015 at 23:16
2

In a one-liner:

apt-cache pkgnames | grep -q "\<$your_package_name\>"

exits with 0 if it's present, 1 if not.

Bonus: efficient check for multiple packages, listed in file "packages_we_want", one per line, sorted with sort:

apt-cache --no-generate pkgnames | sort | comm -13 - packages_we_want lists all packages that you want, but do not exist in the repositories.

apt-cache --no-generate pkgnames | sort | comm -12 - packages_we_want lists all packages that you want and exist in the repositories.

1

My solution 😎

function package_exists() {
    dpkg -s "$1" &> /dev/null
    return $?
}
if package_exists abra; then
    CLIENT=abra
elif package_exists cadabra; then
    CLIENT=cadabra
else
     echo 'Client not found!'
     exit 1
fi
1
  • (1) We prefer answers to have a bit more English text.  Your answer is approximately 8% text and 92% code; that's a bad ratio.  (2) If you had explained your answer, you would have said "Use dpkg -s."  But the OP already knows about dpkg -s, and seems to believe that it's not what they want.  Are you saying that the OP is wrong, and dpkg -s really does do what they want?  Please explain. … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. Commented Jul 6, 2020 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .