how to use grep with logical operator AND - shell

when searching for packages, i'm using grep, if not installed, i have to install it using shell script as mentioned below:
if
list packages installed | grep rap | grep rap-devel
## only if both are installed then exit with status 0
else
install the missing packages
How should I do this?
I tried this but its very lengthy
yum installed packages | grep rap
if $? = 1
yum install rap
yum installed packages | grep rap-devel
if $? = 1
yum install rap-devel
And I dont think its a good practice to do like this. Can anyone help me to shorten this? what if there still more to search and install 🥲

use && and || operate:
(yum installed packages | grep rap || yum install rap) && (yum installed packages | grep rap-devel || yum install rap-devel)
for more search packages in the list:
pack_list=(rap rap-devel)
for i in "${pack_list[#]}"; do yum installed packages | grep rap || yum install rap; done
or if it could, you could use which rap instead of yum installed packages | grep rap.

Related

How to test if package has NOT been installed, in .ebextensions?

How would I check to see if a package has not been installed, in the .ebextensions folder of an Elastic Beanstalk setup? What I want to do is similar to this command, but I only want to run the command if the package does not exist, rather than if it exists.
commands:
install_package:
test: rpm -qa | grep -c example_package
command: yum install -y example_package.rpm
So in pseudocode, this is what I am after:
commands:
install_package:
test: not(rpm -qa | grep -c example_package)
command: yum install -y example_package.rpm
Update: I have gotten this to work without the test parameter, using a double pipe in the command itself instead, but it isn't as neat as I'd like; I'd rather use the test parameter instead to make it more explicit:
commands:
install_package:
command: rpm -qa | grep -c example_package || { yum install -y example_package.rpm; }

How to sudo yum install a list of packages only if they are not installed?

I am trying to improve a build script (bash or Makefile) to install some prerequisites if necessary.
I do have a long list of packages (10-15) and I do want to install them only if they are not installed.
As the build script may be run by non root I do want to run sudo yum install only once and only if needed (at least one package is not installed).
My current implementation is clearly not optimal:
for PACKAGE in a b c ... ; do
yum list installed $PACKAGE >/dev/null 2>&1 || sudo yum install -q -y $PACKAGE ;
done
How can I do this?
You could, of course, just put the list of installed packages into a file and run grep against that in a loop, but should you wish to avoid creating any files, running rpm should work:
for p in a b c ... ; do
rpm -q > /dev/null 2>&1 $PACKAGE || sudo yum install -q -y $p
done
When querying package information, rpm returns zero when the package is found and non-zero otherwise.

scripting yum to install a list of packages

I am having trouble getting yum to work with a list of packages I want to install. I've tried:
yum -y install $(cat /home/pkglist.txt)
and
for i in cat pkglist.txt; do yum -y install $i; done
and
yum -y install $(cat pkglist.txt | tr '\n' ' ' | tr '\r' ' ')
but none of them install the packages.
All I get back is:
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
available.perl-CPAN
available.cpp
available.fontconfig
available.gcc
available.gd
available.net-snmp-libs
available.mailcap
available.perl-libwww-perl
available.perl-XML-LibXML
available.perl-DateTime
available.perl-Time-HiRes
available.make
available.net-snmp-perl
available.perl-Test-Pod
available.perl-Net-SSLeay
available.expat-devel
available.expat
Error: Nothing to do
Thanks in advance! -Luke
You can this-
cat /home/pkglist.txt | xargs yum -y install

Will this bash loop apt-get install all my anaconda packages?

I don't want to just try it and mess something up.
$ dlpackages=$(ls -l anaconda3/bin | awk '{print $9}')
$ for package in $dlpackages; do sudo apt-get install $package; done
or as root: $ for package in $dlpackages; do apt-get install $package; done
Add a safety check for each package, to see if it can be located.
dlpackages=$(ls -l anaconda3/bin | awk '{print $9}')
for package in $dlpackages; do
[[ $(apt-cache search $package) ]] && sudo apt-get install $package
done
Now for every string, the install will only be executed if the package can be found.
Alternatively use the -s option of install as Eric Renouf suggested.
In general things in bin aren't the same as package names. conda list may be closer, but you'll ultimately probably have to figure out the translation of package names manually.

How can I make dpkg automatically choose my own virtual package over an already installed one?

I have several packages that provide the same functionality - and in the device
'there can be only one' at the same time.
I have read about the 'Provides, Conflicts, Replaces' in the debian policy, but I have not found a way (using dpkg with commands/switches) to automatically replace an already installed virtual package without removing it manually first.
My package's control file specifies the following for all the packages in question:
Provides: myown-virtual-package
Conflicts: myown-virtual-package
Replaces: myown-virtual-package
Here is what I do, It seems to work, but I was wondering if there is a standard way using only dpkg
# remove any conflicting virtual packages
for i in /tmp/upgrade_software/*.deb
do
# find out what package name and what it provides
provides_line=$(dpkg --info $i | grep "^ Provides: ")
package_line=$(dpkg --info $i | grep "^ Package: ")
virt_package=${provides_line##*: }
this_package=${package_line##*: }
# skip if it is not a virtual package
[ -z "${virt_package}" ] && continue
# remove any package that provides the same
otherpackage_line=$(dpkg-query -W -f='${Provides}: ${Package}\n' \
| grep "${virt_package}:" | grep -v ${this_package})
if [ -n "${otherpackage_line}" ] ; then
otherpackage=${otherpackage_line##*: }
echo " ------ removing ${otherpackage} because of conflict -------"
dpkg --purge ${otherpackage}
echo " -------------"
fi
echo \'$virt_package\' checked for conflicts
done
Thanks in advance, jj
dpkg will not take this kind of automatic conflict resolution measures. For these tasks, there is apt-get and aptitude. It may just work with
dpkg -i package.deb ; apt-get -f install
The latter command is supposed to resolve the conflicts. If it opts to remove your own package for resolution, you may even want to try
dpkg -i package.deb ; apt-get -f install <package>
I.e., tell apt to install your package (without a .deb extension) as it should now be visible to apt.
This can be done with dpkg alone, by giving it enough information so that it can perform the operation. The way to prepare dpkg for this is via selections.
In this case you'd tell that removing the old provider is ok, and then when you install the new one dpkg will be able to remove the other package during the upgrade.
Try something like:
echo old-provider deinstall | dpkg --set-selections
dpkg -iB new-provider.deb
That should in principle do it, and no need for apt-get fixing it up (-f), or for pre purges (possibly with --force options if there are packages depending on the virtuals).

Resources