Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jackyq2015/9ad0c3915566c99a66bcae04247660c7 to your computer and use it in GitHub Desktop.
Save jackyq2015/9ad0c3915566c99a66bcae04247660c7 to your computer and use it in GitHub Desktop.
apt reinstall
#!/bin/bash
# Function to recursively reinstall packages
reinstall_packages() {
local package=$1
local dependencies=$(apt-cache depends $package | grep "Depends:" | awk '{print $2}')
for dep in $dependencies; do
reinstall_packages $dep
done
echo "Reinstalling $package..."
sudo apt-get install --reinstall -y $package
}
# Function to generate dependency graph
generate_dependency_graph() {
local package=$1
local dependencies=$(apt-cache depends $package | grep "Depends:" | awk '{print $2}')
for dep in $dependencies; do
echo "$package: $dep" >> dependency_graph.txt
generate_dependency_graph $dep
done
}
# Main function to initiate reinstallation
reinstall_from_graph() {
local leaf_packages=()
local package
# Generate dependency graph
generate_dependency_graph $1
# Find leaf packages in the dependency graph
for package in $(cut -d ':' -f 1 dependency_graph.txt); do
if ! grep -q ".*:.*$package" dependency_graph.txt; then
leaf_packages+=("$package")
fi
done
# Loop through leaf packages and reinstall them along with dependencies
for leaf_package in "${leaf_packages[@]}"; do
reinstall_packages $leaf_package
done
}
# Usage: ./reinstall_packages.sh package_name
reinstall_from_graph "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment