#!/bin/sh #Program: # Let user uninstall unused kernels which installed as debian package form. #Author: # mtyy110 if["`whoami`"!='root']; then
echo 'Requires superuser privilege.' exit 1
fi
dpkg --get-selections | grep 'linux-'| grep -v 'deinstall'| grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-" while[ 1 ] do
total=`dpkg --get-selections | grep 'linux-'| grep -v 'deinstall'| grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-"| wc -l` read-p "Which version would you like to uninstall?(0 to quit)" version if[ $version ="0"]; then break
fi
tmp=`echo $version | grep "^[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-\{0,1\}[0-9]\{0,2\}$"| wc -l` if[ $tmp -eq 0 ]; then
echo "Not an available version format,please input full version." continue
fi
sum=`dpkg --get-selections | grep 'linux-'| grep -v 'deinstall'| grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-"| grep "$version"| wc -l` if[ $sum -eq 0 ]; then
echo "Not find version $version.Ignored." continue
fi
tmp=`uname -r | grep "$version"| wc -l` if[ $tmp -eq 1 ]; then read-p "This will uninstall the kernel current used:`uname -r`.Are you sure?(y/N)" choice if["$choice"!='y'-a "$choice"!='Y']; then continue
fi
fi if[ $total -le $sum ]; then read-p "This will uninstall all the kernels in the system.Are you sure?(y/N)" choice if["$choice"-o 'y'-a "$choice"-o 'Y']; then continue
fi
fi
apt-get remove `dpkg --get-selections | grep 'linux-'| grep -v 'deinstall'| grep "\-$version"| cut -f 1` read-p "Continue to uninstall other kernel?(Y/n)" choice if["$choice"='n'-o "$choice"='N']; then break
fi
dpkg --get-selections | grep 'linux-'| grep -v 'deinstall'| grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-"
done exit 0