They put such a problem: you need to write a script that will check if apache2, mysql and php are installed and write installed or not installed next to each item. Please do not write the code! And better to chew on how to do it.

    2 answers 2

    Distribution kit? Checking packages in different distributions goes in different ways, it works on my ubuntu, I do not know how correctly

     dpkg -s sqlite3 | grep Status 

    UPD Quickly ditched such a script, basically plowing, but there is a problem with the message that the package will not find this error and it remains in the output, most likely I am using the wrong command, look for how to determine the state of the package using dpkg, apt-get or aptitude, all of them in Debian are by default, the principle of the script most likely will not change

     #!/bin/bash #$1 - поданая на вход скрипта переменная I=`dpkg -s $1 | grep "Status" ` #проверяем состояние пакета (dpkg) и ищем в выводе его статус (grep) if [ -n "$I" ] #проверяем что нашли строку со статусом (что строка не пуста) then echo $1" installed" #выводим результат else echo $1" not installed" fi 
    • +1, you need to know the distribution. Maybe it’s necessary to use rpm :-) - user6550
    • Well, in principle, you can try to grope the package files themselves, and for the Apache with the muscle, you can also check the demons, but this is somewhat wrong IMHO, and the question was about the packages :) - aknew
    • they can be installed but not started :-) - user6550
    • Distribution - debian - Syaskaa
    • @klopp, that's why I did not write about it in response. @Syaqa For the same debian, ubuntu builds on it - aknew 6:43 pm

    In addition to the above dpkg -s you can use apt-cache policy :

     $ apt-cache policy jenkins jenkins: Installed: 1.542 Candidate: 1.542 Version table: *** 1.542 0 100 /var/lib/dpkg/status 

    And this is the case if the package is not installed:

     $ apt-cache policy sl sl: Installed: (none) Candidate: 3.03-17 Version table: 3.03-17 0 500 http://someserver.domain/ubuntu/ trusty/universe amd64 Packages 

    Thus, you can start with apt-cache policy php | grep Installed apt-cache policy php | grep Installed

    The script that will check

    If suddenly you need to check it on numerous hosts, look towards ansible:

     ansible hostgroup -i production -m shell -a 'apt-cache policy mypackage | grep Installed' 


     ansible hostgroup -i production -m shell -a 'dpkg -s mypackage | grep Status'