Question:
Is it possible to exclude some of a commands output its output based on an array of words?**
Why?
On Ubuntu/Debian there are two ways to list all available pkgs:
apt list
(show all available pkgs, installed pkgs too)apt-cache search .
(show all available pkgs, installed pkgs too)
Difference is, the first command, you can exclude all installed pkgs using grep -v
, problem is unlike the first, the second command you cannot exclude these as the word "installed" isnt present. Problem with the first command is it doesnt show the pkg description, so I want to use apt-cache search .
but excluding all installed pkgs.
# List all of my installed pkgs,
# Get just the pkg's name,
# Swap newlines for spaces,
# Save this list as an array for exclusion.
INSTALLED=("$(apt list --installed 2> /dev/null | awk -F/ '{print $1}' | tr 'rn' ' ')")
I then tried:
apt-cache search . | grep -v "${INSTALLED[@]}"
Unfortunately this doesnt work as I still see my installed pkgs too so I’m guessing its excluding the first pkg in the array and not the rest.
Again thank you in advance!
2
Answers
Sorry if I’m misunderstanding, but you just want to get the list of packages which are not installed, right?
If so, you can just do this –
Would you please try the following:
installed
is used to check if the command isinstalled.
while
loop scans over the installed list of the command andstore the command names in the associative array
installed
.while
loop scans over the available command list and if thecommand is not found in the associative array, then print it.
BTW your trial code starts with
#!/bin/sh
which is run withsh
, notbash
.Please make sure it looks like
#!/bin/bash
.