Skip to content

Instantly share code, notes, and snippets.

@jonesinator
Created March 14, 2018 14:53
Show Gist options
  • Save jonesinator/e07e07947c6dccb92c63e7235677cb76 to your computer and use it in GitHub Desktop.
Save jonesinator/e07e07947c6dccb92c63e7235677cb76 to your computer and use it in GitHub Desktop.
Generate the difference in packages between two Debian-derived chroots.
#!/usr/bin/env bash
set -e
# Validate the command-line arguments.
if [[ $# -ne 3 ]]; then
echo "Usage: $0 [chroot1] [chroot2] [only_in_1,only_in_2,in_both]"
exit 1
elif [[ ! -d "$1" ]]; then
echo "$1 is not a directory."
exit 1
elif [[ ! -d "$2" ]]; then
echo "$2 is not a directory.";
exit 1;
elif [[ "$3" != "only_in_1" && "$3" != "only_in_2" && "$3" != "in_both" ]]; then
echo "$3 is invalid. Valid options are only_in_1, only_in_2, and in_both."
exit 1
fi
# Get installed package lists from both chroots and compute their difference.
PKGS_1=$(sudo chroot "$1" /bin/bash -c "apt list --installed" 2>/dev/null | \
tail +2 | awk 'BEGIN{FS="/"}{print $1}')
PKGS_2=$(sudo chroot "$2" /bin/bash -c "apt list --installed" 2>/dev/null | \
tail +2 | awk 'BEGIN{FS="/"}{print $1}')
DIFF=$(diff --unified=999999 <(echo "$PKGS_1") <(echo "$PKGS_2") | tail +3)
# Produce the requested output type.
if [[ "$3" == "only_in_1" ]]; then
echo "${DIFF}" | grep "^-" | cut -c2-
elif [[ "$3" == "only_in_2" ]]; then
echo "${DIFF}" | grep "^+" | cut -c2-
elif [[ "$3" == "in_both" ]]; then
echo "${DIFF}" | grep "^ " | cut -c2-
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment