Skip to content

Instantly share code, notes, and snippets.

@Azq2
Created April 1, 2024 21:28
Show Gist options
  • Select an option

  • Save Azq2/60307f3eea26682e8c9e61bd2c4af429 to your computer and use it in GitHub Desktop.

Select an option

Save Azq2/60307f3eea26682e8c9e61bd2c4af429 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
$ENV{LC_ALL} = 'C';
my $need_downgrade = `dpkg -l | grep ^i | awk '{print \$2}'`;
$need_downgrade =~ s/^\s+|\s+$//g;
my @final_cmd = ("sudo", "apt-get", "install", "--allow-downgrades", "--allow-change-held-packages", "--reinstall");
for my $pkg (split("\n", $need_downgrade)) {
my $cache_info = `apt-cache policy '$pkg'`;
my @versions;
while ($cache_info =~ /^( \*\*\* | )(\S+) (\d+)$/img) {
push @versions, {v => $2, p => int $3};
}
my ($installed_version) = $cache_info =~ /^ Installed: (.*?)$/m;
die $cache_info if !$installed_version;
@versions = sort { $b->{p} <=> $a->{p} } @versions;
if (@versions >= 2 && $versions[0]->{v} ne $installed_version) {
print "$pkg $installed_version -> ".$versions[0]->{v}."\n";
push @final_cmd, "$pkg=".$versions[0]->{v};
}
}
print "\n\n";
print join(" ", @final_cmd)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment