Checking for installed package for bash - linux

I need a script that will check whether packages are installed for apache2, mysql and php.
Example output:
apache2 .... ok
mysql .... ok
php ... not installed

Packages are not necessarily named the same on different distributions, and querying for their presence depends on the package manager in use.
Debian (dpkg):
dpkg-query -W -f='${Package}\n' apache2 mysql-server php5 2>/dev/null
Fedora (RPM):
rpm -q --qf '%{NAME}\n' httpd mysql-server php 2>/dev/null
Gentoo (Portage):
equery --quiet list www-servers/apache:2 dev-lang/php dev-db/mysql

Assuming APT:
dpkg -l | grep -i apache2
etc.

For CentOS (will only show the ones that are installed):
yum list installed | egrep -i 'apache|mysql|php'

Related

How to view all installed packages in terminal (Ubuntu)

Need to view all the packages installed on my system through terminal.
I am using ubuntu 16.10
# dpkg -l
From dpkg manual:
dpkg-query actions
See dpkg-query(1) for more information about the following actions.
-l, --list package-name-pattern...
List packages matching given pattern.
-s, --status package-name...
Report status of specified package.
-L, --listfiles package-name...
List files installed to your system from package-name.
-S, --search filename-search-pattern...
Search for a filename from installed packages.
-p, --print-avail package-name...
Display details about package-name, as found in
/var/lib/dpkg/available. Users of APT-based frontends
should use apt-cache show package-name instead.
To list packages installed only by you:
gunzip -c /var/log/apt/history.log.*.gz | grep 'apt-get install' | cut -f4- -d" " | tr ' ' $'\n' | sort -u
Solution: In order to view all installed packages in linux Ubuntu, run on terminal apt --installed list,
Use apt flags and would be able to see available upgrades to some packages ( --upgradeable ) / current installed packages ( --installed ) / all available versions ( --all-versions ).
From Documentation:
DESCRIPTION
apt provides a high-level commandline interface for the package management system.
It is intended as an end user interface and enables some options better suited for interactive usage by
default compared to more specialized APT tools like apt-get(8) and apt-cache(8).
Much like apt itself, its manpage is intended as an end user interface and as such only mentions the most
used commands and options partly to not duplicate information in multiple places and
partly to avoid overwhelming readers with a cornucopia of options and details.
the list flag offers 3 options:
list (work-in-progress)
list is somewhat similar to dpkg-query --list in that it can display a
list of packages satisfying certain criteria. It supports glob(7)
patterns for matching package names as well as
options to list installed (--installed), upgradeable (--upgradeable) or
all available (--all-versions) versions.
command to list all installed packages
sudo apt list --installed
you can add 'grep' to find your service as below:
sudo apt list --installed | grep <my_service_name>
I used the following Three Cmd Syntaxes & tested them to List installed packages on My ubuntu Subsystem machine from VB6 Shell() function and 2 of them Worked fine:
1- Syntax-#1:[ Worked ]
sudo apt list --installed
2- Syntax-#2:[ Worked ]
sudo dpkg -l
Syntax-#3: [ Do not Worked .. for me ]
sudo dpkg -l | grep -i apache
And Here is MY VB6 - Code List:
Private Sub Command1_Click()
Dim Id3 As Variant ' 1- Syntax-#1: Worked fine:
Id3 = Shell(App.Path & "\bash.exe | sudo apt list --installed", vbNormalFocus)
' 2- Syntax-#2: Worked Also: ' Id3 = Shell(App.Path & "\bash.exe
| sudo dpkg -l", vbNormalFocus)
' 3- Syntax-#3: Does not show output ... Why .. Dont know now: '
Id3 = Shell(App.Path & "\bash.exe | sudo dpkg -l | grep -i apache",
vbNormalFocus) End Sub**

Explanation of this docker file?

I am trying to understand this docker file
FROM ubuntu:trusty
MAINTAINER Wurstmeister
RUN apt-get update; apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64/
RUN echo 'root:wurstmeister' | chpasswd
RUN mkdir /var/run/sshd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
I understood most of the lines but I don't understand what these below lines means?
apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
echo 'root:wurstmeister' | chpasswd
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Can anyone help me understand? I am not sure what is the purpose of wget supervisor docker.io openssh-server in that line. Also what does echo mean there? And then also last sed line.
apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
Installs a bunch of packages using the ubuntu package manager (the base image is Ubuntu Trusty 14.04).
The -y option is used to prevent apt from asking user confirmation about installing the packages and their dependencies: the installation just proceeds without need for any input. This is needed to avoid hanging the process of building the docker image. Packages installed:
openssh-server: so that the container can act as an ssh server and process requests from ssh clients
wget, unzip: utilities
java: eventually this container is used to allow users to access it via ssh, maybe those users need java
supervisor, docker.io: I don't see a direct usage of it in the Dockerfile itself
then
echo 'root:wurstmeister' | chpasswd
Changes password of user root to wurstmeister
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Replaces the text PermitRootLogin without-password with PermitRootLogin yes in file /etc/ssh/sshd_config to allow root user to login with password or without (e.g., with public key).
An important general note: If you run SSHD in your Docker containers, you're doing it wrong!
wget supervisor docker.io openssh-server are parameters to the apt-get call. Those are the names of the packages to be installed.
echo is used in combination with | to send the new password to the chpasswd command.
sed is used to update some text in a configuration file (here to allow passwordless login). The s/text/newText/ part does text substitution.

Windows Bash (WSL) - sudo: no tty present and no askpass program specified

After following this tutroial I get the following error when trying to run the commands as user or even sudo:
sudo: no tty present and no askpass program specified
The comments from Lurdan in this article state that you need to run
sudo -S <YOUR_COMMAND>
chmod 0666 /dev/tty
chmod doesn't work but sudo -S does, but surely there's another fix?
So silly, after looking further down I see a solution from Beorat:
To avoid the sudo tty issue and others, run these commands just before running do-release-upgrade:
sudo -S apt-mark hold sudo
sudo -S apt-mark hold procps
sudo -S apt-mark hold strace
If you've already upgraded, run the above commands, then manually downgrade to the Trusty packages:
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/s/sudo/sudo_1.8.9p5-1ubuntu1.1_amd64.deb
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/p/procps/procps_3.3.9-1ubuntu2_amd64.deb
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/s/strace/strace_4.8-1ubuntu5_amd64.deb
sudo -S dpkg -i sudo_1.8.9p5-1ubuntu1.1_amd64.deb
sudo -S dpkg -i procps_3.3.9-1ubuntu2_amd64.deb
sudo -S dpkg -i strace_4.8-1ubuntu5_amd64.deb
More info here: https://github.com/Microsoft/BashOnWindows/issues/482
WSL uses the lxrun executable for management from Windows:
lxrun -h
Usage:
/install - Installs the subsystem
Optional arguments:
/y - Do not prompt user to accept
/uninstall - Uninstalls the subsystem
Optional arguments:
/full - Perform a full uninstall
/y - Do not prompt user to accept
/setdefaultuser - Configures the subsystem user that bash will be launched as. If the user does not exist it will be created.
Optional arguments:
username - Supply the username
/y - If username is supplied, do not prompt to create a password
/update - Updates the subsystem's package index
Given that, you can use lxrun /setdefaultuser root. Just thought I'd point out this side of it since it was required when I ran into the same issue as you after trying to upgrade to Xenial. I can confirm that running this command, then the wget / dpkg commands my issues were resolved.
The commands I used:
wget http://mirrors.kernel.org/ubuntu/pool/main/s/sudo/sudo_1.8.9p5-1ubuntu1.4_amd64.deb
wget http://mirrors.kernel.org/ubuntu/pool/main/p/procps/procps_3.3.9-1ubuntu2_amd64.deb
wget http://mirrors.kernel.org/ubuntu/pool/main/s/strace/strace_4.8-1ubuntu5_amd64.deb
dpkg -i sudo_1.8.9p5-1ubuntu1.4_amd64.deb
dpkg -i procps_3.3.9-1ubuntu2_amd64.deb
dpkg -i strace_4.8-1ubuntu5_amd64.deb
Finally, you might need to run sudo apt-get install -f in case you get The following packages have unmet dependencies [xxx] but it is not going to be installed
I got rid of the error by moving /etc/hosts to /etc/hosts.bu. After closing the shell en opening again, /etc/hosts is recreated and your computer name is added. The error is gone (for me.)

completely uninstall r linux

I am trying to update my version of R on linux mint, however broken dependencies are stopping me doing this. after trying everything such as adding repos from Cran, sudo apt-get update, I still cannot install the latest version of R.
MY question is how to i completely remove R from my machine, so that I can restart. I have tried :
sudo apt-get remove r-base
however when I run R it still works:
laptop$ R
R version 2.13.1 (2011-07-08)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
and doesn;t seem to be removed at all.
I want a clean, fresh install, but I don't think I am removing R properly
The R binary (well, front-end script) is part of the r-base-core package which contains the core R system.
The package r-base is a so-called virtual package which exists to just pulls other packages in. Removing it does not remove any parts of the R system --- for which you need to remove r-base-core.
You might want to check on all currently installed R packages.
You can list all packages whose name starts with "r-" like this:
dpkg -l | grep ^ii | awk '$2 ~ /^r-/ { print $2 }'
To uninstall all of them, pipe the output to xargs apt-get remove:
dpkg -l | grep ^ii | awk '$2 ~ /^r-/ { print $2 }' | xargs apt-get remove --purge
dpkg -l | grep ^ii | awk '$2 ~ /^r-/ { print $2 }' | sudo xargs apt-get remove --purge -y
Worked for me on Ubuntu 14.04. Note sudo addition to the previous suggestion by others.
At your Linux command line, try:
dpkg --get-selections | grep "^r\-"
This will list R packages installed on your system. You can then delete them by name.
Hopefully this proves useful.
In addition to running:
sudo apt-get remove r-base
sudo apt-get remove r-base-core
try also:
sudo apt purge r-*
The following does the job especially if you want to update your R version.
sudo apt-get remove r-base-core

Remove packages contain specific word in name

In Linux (Fedora), how I see all the installed packages which contain a certain words in the package name. Then, remove all these packages installed.
Assuming you are using a debian-based Linux (like Ubuntu or Mint, for example), you can do like this to search for mysq:
dpkg -l | grep mysq
and to get only the names
dpkg -l | grep mysq | awk '{print $2}'
if you want remove all packages, containing a specific word, you don't need to pipe lists through grep or whatever. Just type
$ sudo yum remove "*word*"
If you want to review list of such packages before removing, then type
$ rpm -qa "*word*"
That's it.
Fedora is an RPM-based distribution. So you'll want to use the rpm or yum commands.
To list installed: yum list installed | grep <name> or rpm -a | grep <name>
To remove a package: rpm -e <package-name> or yum remove <package-name>
Sources:
Fedora RPM Guide
Fedora yum wiki
in linux, you can install programs in a variety of places. Most of the time, your rpm on your distro is responsible for removing all of the bits and pieces of a program. One solution, although this is fraught with danger, is to grep your usr/bin like this:
ls /usr/bin || grep 'some package name'
and pipe that to an rm-rf ... 'shuddurs'
A safer bet though is to just use apt-get uninstall 'someApp' much safter

Resources