-1

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?

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
Shawn
  • 8,374
  • 5
  • 37
  • 60

4 Answers4

5

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

Michael Hirt
  • 71
  • 1
  • 3
  • I had to filter out empty lines from the yum output. So I ended up doing `sudo yum -q check-update | sed '/^$/d' | wc -l` and that gives me the correct update count. – mgefvert Jul 25 '23 at 14:52
3

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.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Yes, I have been trying to use the -c option with grep, but I am not sure how to limit the output of check-updates to just the lines that show updates, so right now my count isn't accurate. I am looking for an answer that deals with the output of the check-updates command, not something generic. – Shawn Sep 22 '20 at 15:50
  • @Shawn: in that case, please edit your question, show what a regular check-updates output looks like, and what you expect. – Dominique Sep 22 '20 at 15:55
  • The output varies. If the output was fixed I would have already written the regular expression, but I am a bash/grep newbie and I don't know all the variations of output that may exist in `check-updates`. So I don't want to put something specific, and instead, I hope for an answer that accommodates the potential differences. – Shawn Sep 22 '20 at 15:57
  • 1
    Does [this question](https://stackoverflow.com/questions/23698638/how-to-get-just-a-list-of-yum-updates) help you? – Thomas Sep 22 '20 at 16:17
  • Yes, @Thomas, thank you--that gives an answer specific to `yum check-updates` that I was seeking. – Shawn Sep 22 '20 at 16:38
  • @Shawn Please accept your own answer as correct for future reference. – Thomas Sep 22 '20 at 20:27
  • I will do so, but StackOverflow says I must wait two more days before I can do that. – Shawn Sep 22 '20 at 21:03
3

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

Shawn
  • 8,374
  • 5
  • 37
  • 60
  • plus one for adding the answer for reference purposes. – Dominique Sep 22 '20 at 17:58
  • You never need grep when you're using awk. I'm not sure since idk what the output of `yum check-updates` looks like but `awk 'f && /\./{c++} NF{f=1} END{print c+0}'` might be what you want. – Ed Morton Sep 22 '20 at 22:39
0

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
Victor Lee
  • 2,467
  • 3
  • 19
  • 37