Check last upgrade date of Debian base distros

by

in
#!/bin/bash
#####################################################################
# BRIEF DESCRIPTION
# This script looks for the keyword 'upgrade' in the log files
# then it compares the date of all occurrences of the key word and
# and prints to shell the most recent date.
#####################################################################

# Check numer of availabe log dpkg files 
mapfile -t files < <(ls /var/log/dpkg.log*)
# Debug print
# echo ${#files[@]}

# Iterate over all log files found
for ((i=0; i <${#files[@]}; i++))
do 
    # Debug print
    # echo ${files[i]}
    
    # grep a log file for upgrades store in an array.
    mapfile -t lines < <(grep upgrade ${files[i]})
    
    # Debug print
    # echo ${#lines[@]}
    
    # Store the number of lines on the last grep file
    leng=${#lines[@]}
    # loop through each line in thearray
    for ((j=0; j<leng;j++)) do
	# If length of $STRING is larger than one compare the new $DATE with the $STRING
        # If the date is more resent store the new date in $STRING 
        if [ -n $STRING ] 
        then
	    DATE=${lines[j]:0:10}
            # echo $DATE
	    if [[ "$STRING" < "$DATE" ]]; 
	    then
		STRING="$DATE"
	    fi
        else
	# Initialize the $STRING
            DATE=${lines[j]:0:10}
	    STRING="$DATE"
	fi
    done
done

# Output the most recent upgrade date of the system if any was found.
if [[ -n $STRING ]]
then
    echo "$STRING"
fi

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *