Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2632  / 2 Years ago, thu, november 3, 2022, 7:13:25

I am relatively new to Linux in general, still learning :)



I want to write a bash script for automation of apt update and apt upgrade. I have the idea of sending the apt update command, then watching the lines for specific words.



E.g, after apt update was done, it says:



 Reading state information... Done
89 packages can be upgraded.


So in this case I want to look for can be upgraded. If those words are in the output the script shall execute apt upgrade -y.



Any help is highly appreciated. Thanks in advance.


More From » apt

 Answers
7

There is this tool /usr/lib/update-notifier/apt-check giving simple output.



The --human-readable flag gives more information.



In short, /usr/lib/update-notifier/apt-check --human-readable gives two numbers :



<n1> packages can be updated.
<n2> updates are security updates.


/usr/lib/update-notifier/apt-check with no option has this output layout:



<n1>;<n1>


So it is possible to extract the first number and if it is greater than 0, say that some update is available.



Extract the first number, number of available updates:



/usr/lib/update-notifier/apt-check |& cut -d";" -f 1


Use it in a script:



#!/bin/bash
if [ $(/usr/lib/update-notifier/apt-check |& cut -d";" -f 1) -gt 0 ]; then
# Some commands
sudo apt-get -y dist-upgrade
fi

[#5040] Thursday, November 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
edgehogight

Total Points: 272
Total Questions: 113
Total Answers: 99

Location: Northern Mariana Islands
Member since Sun, Jul 19, 2020
4 Years ago
;