@@ -81,17 +81,28 @@ cleanup_snapd() {
8181
8282 rm -rf /var/lib/snapd/cache/*
8383
84- # List all snaps and filter for disabled ones
85- snap list --all | awk ' /disabled/{print $1, $3}' | while read -r snapname revision; do
84+ # Get snap list output once and store it
85+ if ! snap_output=$( snap list --all) ; then
86+ print_err " Error: Failed to retrieve snap list."
87+ return
88+ fi
89+
90+ # Check if no snaps are installed (only header line present)
91+ if [ " $( echo " ${snap_output} " | wc -l) " -le 1 ]; then
92+ return
93+ fi
94+
95+ # Process the stored output to find disabled snaps
96+ echo " ${snap_output} " | awk ' /disabled/{print $1, $3}' | while read -r snap_name revision; do
8697 # Check if variables are set and not empty
87- if [ -z " $snapname " ] || [ -z " $revision " ]; then
98+ if [ -z " ${snap_name} " ] || [ -z " ${ revision} " ]; then
8899 print_err " Error: Snap name or revision is empty. Skipping..."
89100 continue
90101 fi
91102
92103 # Attempt to remove the snap revision
93- if ! snap remove " $snapname " --revision=" $revision " ; then
94- print_err " Error: Failed to remove $snapname (revision $revision )."
104+ if ! snap remove " ${snap_name} " --revision=" ${ revision} " ; then
105+ print_err " Error: Failed to remove ${snap_name} (revision ${ revision} )."
95106 fi
96107 done
97108}
@@ -102,7 +113,7 @@ cleanup_snapd() {
102113# Provides output to indicate the cleanup process and handles errors gracefully.
103114# Usage: Call this function to automate system cleanup tasks after updating the system.
104115clean_up () {
105- case ${ADJUSTED_ID} in
116+ case " ${ADJUSTED_ID} " in
106117 debian)
107118 # rm -rf /var/lib/apt/lists/*
108119 cleanup_snapd
@@ -140,9 +151,9 @@ update_snapd() {
140151# Supports Debian-based (apt-get), RPM-based (dnf/yum/microdnf), and Alpine (apk) package managers.
141152# Prints messages indicating the update process and handles errors gracefully.
142153update_os_pkg () {
143- case ${ADJUSTED_ID} in
154+ case " ${ADJUSTED_ID} " in
144155 debian)
145- if [ " $( find /var/lib/apt/lists/* - maxdepth 1 -check_cmd f 2> /dev/null | wc -l) " -eq 0 ]; then
156+ if [ " $( find /var/lib/apt/lists/ -mindepth 1 - maxdepth 1 -type f 2> /dev/null | wc -l) " -eq 0 ]; then
146157 println " Updating ${PKG_MGR_CMD} based packages..."
147158 if ! (" ${PKG_MGR_CMD} " update -y &&
148159 " ${PKG_MGR_CMD} " upgrade -y &&
@@ -304,7 +315,7 @@ install_pkg() {
304315 pkg_name=" $1 "
305316
306317 if ! check_command " ${pkg_name} " ; then
307- case ${ADJUSTED_ID} in
318+ case " ${ADJUSTED_ID} " in
308319 debian)
309320 " ${PKG_MGR_CMD} " update && " ${INSTALL_CMD} " " ${pkg_name} "
310321 ;;
0 commit comments