remove multiple rpm packages using one bash command - bash

I'd like to use a single bash command to uninstall several packages.
# rpm -qa | grep php
php-common-5.4.16-45.el7.x86_64
php-5.4.16-45.el7.x86_64
php-mysql-5.4.16-45.el7.x86_64
php-pdo-5.4.16-45.el7.x86_64
php-cli-5.4.16-45.el7.x86_64
will give me an output of all the pakcages I'd like to remove, however, how can I pipe that into a remove package command? Something like this:
# rpm -qa | grep php | yum remove ${package}

I tried this and it worked.
rpm -qa | grep php | while read -r line; do yum remove -y $line; done

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 can I grep a count of available updates from yum check-updates

I want to check if updates are available, and if so, perform some steps before I install the updates. I run yum check-updates to see a list of updates available for installed packages, but I'd like to grep this and get a count that I can use for some logic in a bash script. So ideally, I would like to grep that output of check-updates and return 0 if there are no updates, or if five updates are available then I would like the grep to return 5.
How can I grep this to return the count?
I like a simpler approach.
"yum -q" reduces the output from yum so it only displays a list of packages. Combine this with "wc -l" to count the number of lines output.
So to get a count of packages requiring updates I would run
sudo yum -q check-update | wc -l
Are you aware of grep -c? I've just created some nonsense file, giving following result:
Prompt> grep "AA" test.txt
1A01 TCCTTGAAAG
TCAACAAGAA
TCGCAAA
TTTAAAGTCGT
GGCGGAATCAATAC
GATGGAATATGCGCC
If I use grep -c, this is the result:
Prompt> grep -c "AA" test.txt
6
In case this does not answer your question completely, please edit your question and add some more information, just to show what you are looking for.
Also, please be aware that adding | wc -l behind every UNIX command reads the amount of results of that command.
This combination of awk and grep gives the count of available updates for installed packages:
yum check-updates | awk 'p;/^$/{p=1}' | grep -c "\."
This was based on the info in How to get just a list of yum updates
The -q for quiet is great but you may also want to grep out any blank lines and also the trailing Loaded plugins message. This works nicely for an accurate count:
yum check-update -q|egrep -v "Loaded plugins: langpacks, product-id, subscription-manager|^$"|wc -l

Automatically update outdated pip3 packages with bash scripts

I have a bash script to automatically update pip3 packages:
It is one line script:
pip3 list --outdated | cut -d' ' -f1 | xargs pip3 install --upgrade
and it has always worked.
Now (maybe after the upgrade to Ubuntu 18.04) it is not working anymore apparently because of a wrong usage of pip3 install.
What is wrong with the script?
Looks like headers of the pip3 list are causing failure in installation of package.
You can trim those header lines by using tail.
pip3 list --outdated | cut -d' ' -f1 | tail -n+3 | xargs pip3 install --upgrade
tail -n+3 removes the header and boundary line which only gives package names to xargs.

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 to reproduce the command "rpm -qa | grep xxx" via script

How can I reproduce the command rpm -qa | grep xxx with a script:
#!/bin/bash
read -p "RPM is installed?" name
rpm -qa | grep $name
If you are calling the script like this:
script.sh 'foo*'
you need to quote $name to make sure its value is passed to grep unexpanded:
read -p "RPM is installed?" name
rpm -qa | grep "$name"

Resources