Skip to content

Instantly share code, notes, and snippets.

@simplexidev
Last active July 16, 2023 06:04
Show Gist options
  • Save simplexidev/27dd4de2f444fe5836ac5089d00ed56c to your computer and use it in GitHub Desktop.
Save simplexidev/27dd4de2f444fe5836ac5089d00ed56c to your computer and use it in GitHub Desktop.
TempPackageList
#!/bin/bash
# Check if output file argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <output_file>"
exit 1
fi
output_file="$1"
# Get the current timestamp
timestamp=$(date +"%Y%m%d%H%M%S")
# Step 1: Update package lists
sudo apt update
# Step 2: Retrieve package information and save to file
dpkg-query -W -f='<Package Name="${Package}" Version="${Version}" Repository="${Origin}" Section="${Section}" Origin="${Origin}">\n' |
awk -v timestamp="$timestamp" '{gsub("\\$\\{Section\\}", ""); print}' |
awk -v timestamp="$timestamp" '{gsub("\\$\\{Origin\\}", ""); print}' |
awk -v timestamp="$timestamp" '{gsub("\\$\\{Package\\}", ""); print}' |
awk -v timestamp="$timestamp" '{gsub("\\$\\{Version\\}", ""); print}' |
awk -v timestamp="$timestamp" '{gsub("\\$\\{Repository\\}", ""); print}' >"$output_file"
# Step 3: Install apt-rdepends if not already installed
if ! command -v apt-rdepends &>/dev/null; then
sudo apt install apt-rdepends -y
fi
# Step 4: Retrieve dependencies and append to the file
while read -r line; do
package_name=$(echo "$line" | awk -F '=' '/Name/ {print $2}')
dependencies=$(apt-rdepends --reverse --depends --follow=Depends,PreDepends --print-state=Unknown,NotInstalled,UnPacked,HalfConfigured,HalfInstalled,ConfigFiles,Installed "$package_name")
if [ -n "$dependencies" ]; then
echo -e "\t<Dependencies>\n$dependencies\t</Dependencies>\n</Package>\n" >>"$output_file"
else
echo "</Package>" >>"$output_file"
fi
done <"$output_file"
# Step 5: Remove the Version and Description attributes from Dependency tags
sed -i 's/^\([[:space:]]*\)<Dependency Name="\(.*\)" \/>/\1<Dependency Name="\2" \/>/' "$output_file"
echo "Package list generated. Please check the '$output_file' file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment