To achieve the tasks specified in your request, we can create a Bash script that processes the pacman.log to extract the recently updated, installed, or removed packages and then checks them against the monitored applications' dependency trees. I will layout the steps and the script for accomplishing this.
Here is a breakdown of the steps we want to perform:
- Find the last "transaction started" line in
pacman.log.
- Extract all updated, installed, or removed packages from that point to the end of the log file.
- Write the list of packages to
UpdatedPkgList.
- Check for the latest two folders in
PacTreeHistory considering the date format provided.
- Compare the packages in
UpdatedPkgList against the content of files named after packages listed in MonitoredAppList.
- Write the relevant dependency information to
UpdatedDepsOfMonitoredApps.
Now, here's a Bash script to perform the above tasks:
#!/bin/bash
# Path to the pacman.log
PACMAN_LOG="/var/log/pacman.log"
# Path to the PacTreeHistory directory containing folders with dependency trees
PAC_TREE_HISTORY_DIR="PacTreeHistory"
# Path to MonitoredAppList file
MONITORED_APP_LIST="MonitoredAppList"
# Output files
UPDATED_PKG_LIST="UpdatedPkgList"
UPDATED_DEPS_OF_MONITORED_APPS="UpdatedDepsOfMonitoredApps"
# Ensure that the output files are empty
> "$UPDATED_PKG_LIST"
> "$UPDATED_DEPS_OF_MONITORED_APPS"
# Find the last "transaction started" line number in pacman.log
last_transaction_line=$(grep -n "transaction started" "$PACMAN_LOG" | tail -n1 | cut -d: -f1)
# Collect information about updated, installed, or removed packages since the last transaction
sed -n "${last_transaction_line},\$p" "$PACMAN_LOG" | \
grep -E "upgraded|installed|removed" | \
awk -F ' ' '{print $3 " " $4}' | \
sed -e 's/[()]//g' | \
sed -e 's/->/:/' > "$UPDATED_PKG_LIST"
# Get the latest two directories in PacTreeHistory
latest_two_dirs=$(ls -d "$PAC_TREE_HISTORY_DIR"/[0-9]*_[0-9]* | tail -n2)
# Read MonitoredAppList into an array
mapfile -t monitored_apps < "$MONITORED_APP_LIST"
# Get updated, installed or removed packages
mapfile -t updated_packages < "$UPDATED_PKG_LIST"
# Loop over each of the latest two directories
for dir in $latest_two_dirs; do
# Loop over packages from UpdatedPkgList
for pkg in "${updated_packages[@]}"; do
pkg_name=$(echo "$pkg" | cut -d ' ' -f1)
# Check if the package is part of the monitored applications
if [[ " ${monitored_apps[*]} " =~ " $pkg_name " ]]; then
pkg_files="$dir/$pkg_name"
# Check if the dependency tree file for the monitored package exists
if [[ -f "$pkg_files" ]]; then
# Write the package info and the corresponding filename into UpdatedDepsOfMonitoredApps
echo "$pkg - Found in $pkg_files" >> "$UPDATED_DEPS_OF_MONITORED_APPS"
fi
fi
done
done
echo "Dependencies for monitored apps written to $UPDATED_DEPS_OF_MONITORED_APPS"
Make sure you give execution permissions to the script before running:
chmod +x [script_name].sh
To run the script, use:
./[script_name].sh
Ensure that the paths to pacman.log, PacTreeHistory, and MonitoredAppList are correct and accessible to the script. Also, make sure that the MonitoredAppList file contains one monitored package name per line. Note that this script expects the latest two directories in PacTreeHistory to be named in the expected date format and that the system's file timestamps allow for correct directory ordering. Adjustments may be required depending on the actual setup.