What makes us different from other similar websites? › Forums › Tech › How to update Linux through the terminal, Debian based. › Reply To: How to update Linux through the terminal, Debian based.

Here is another update script, that will shut down the system with a 60-second counter, and say yes to all options.
#!/bin/bash
# Function to display a purple background message
print_message() {
echo -e "\033[30;45m$1\033[0m"
}
# --- System Update and Upgrade ---
print_message "Updating APT repository index..."
sudo apt-get update -y # -y added for non-interactive update, though usually not strictly needed for 'update'
print_message "Upgrading existing installed packages..."
sudo apt-get upgrade -y
print_message "Performing distribution upgrade..."
# Using DEBIAN_FRONTEND=noninteractive to make do-release-upgrade less interactive.
# However, this command can still require manual intervention for certain decisions.
DEBIAN_FRONTEND=noninteractive sudo do-release-upgrade -f DistUpgradeViewNonInteractive
print_message "Removing unnecessary packages to save disk space..."
sudo apt-get autoremove -y
print_message "Upgrade installed Flatpak applications..."
flatpak upgrade -y # -y added to automatically confirm flatpak upgrades
print_message "Refreshing Snap..."
sudo snap refresh
# --- Shutdown Sequence ---
print_message "All updates and upgrades are complete."
echo "" # Empty line for spacing
echo -e "\033[30;46mSystem will shut down in 60 seconds...\033[0m" # Cyan background for shutdown message
for i in {60..1}; do
echo -ne "\033[K" # Clear current line
echo -ne "\rShutdown in $i seconds... Press Ctrl+C to cancel."
sleep 1
done
echo "" # New line after countdown
print_message "Initiating system shutdown now!"
sudo shutdown -h now
Here is what all of it does:
System Maintenance & Shutdown Script
This document explains a Bash shell script designed to automate common Linux system updates and upgrades, followed by an automatic system shutdown after a 60-second countdown.
Important Considerations for do-release-upgrade
The sudo do-release-upgrade
command is a powerful tool for upgrading your operating system to a new major version. It’s typically designed for interactive use because it often requires user decisions regarding configuration file changes and potential conflicts.
While this script attempts to automate this process using DEBIAN_FRONTEND=noninteractive
and -f DistUpgradeViewNonInteractive
, it’s crucial to understand that fully unattended distribution upgrades carry inherent risks. Issues might arise that require manual intervention, or automatically confirming all prompts could lead to unexpected system behavior or breakage. It is generally recommended to perform do-release-upgrade
interactively, especially on critical systems.
Script Breakdown
Let’s go through each part of the script to understand its function:
Shebang
#!/bin/bash
This line is called the shebang. It tells the operating system to execute the script using the Bash shell, which is located at /bin/bash
.
print_message
Function
# Function to display a purple background message
print_message() {
echo -e "\033[30;45m$1\033[0m"
}
This defines a reusable function named print_message
.
echo -e
: Prints text to the terminal and enables the interpretation of backslash escape sequences."\033[30;45m"
: This is an ANSI escape code for text formatting.\033[
: Marks the start of an escape sequence.30
: Sets the text (foreground) color to black.45
: Sets the background color to magenta (purple).m
: Terminates the formatting sequence.
$1
: This is a positional parameter that represents the first argument passed to the function. When you callprint_message "Your message"
,$1
will be replaced by “Your message”.\033[0m
: This escape code resets all text formatting (colors, bold, etc.) back to the default, ensuring that subsequent output in the terminal isn’t also purple.
This function is used throughout the script to display important messages with a distinct black text on a purple background.
System Update and Upgrade Commands
print_message "Updating APT repository index..."
sudo apt-get update -y
print_message "..."
: Calls the function to display the current action.sudo
: Stands for “superuser do”. It allows the following command to be executed with administrative (root) privileges, which are required for system-level package management.apt-get update
: This command downloads the latest package information (lists of available packages and their versions) from the configured software repositories. It does not install or upgrade any software itself, only refreshes the index.-y
: This flag automatically answers “yes” to any prompts thatapt-get
might present, making the command non-interactive.
print_message "Upgrading existing installed packages..."
sudo apt-get upgrade -y
apt-get upgrade
: This command installs the newer versions of all packages that are already installed on your system. It performs upgrades without removing existing packages or installing new ones (unless required by dependencies).-y
: Automatically confirms the upgrade.
print_message "Performing distribution upgrade..."
DEBIAN_FRONTEND=noninteractive sudo do-release-upgrade -f DistUpgradeViewNonInteractive
DEBIAN_FRONTEND=noninteractive
: This is an environment variable that instructs Debian-based tools (likedo-release-upgrade
) to run in a non-interactive mode, attempting to suppress user prompts.do-release-upgrade
: This command is specific to Ubuntu/Xubuntu and similar Debian-based distributions. It initiates a major operating system upgrade to the next stable release (e.g., from Ubuntu 22.04 LTS to 24.04 LTS). This is a more significant operation than a regularapt-get upgrade
.-f DistUpgradeViewNonInteractive
: This flag is passed todo-release-upgrade
to force a non-interactive view for the upgrade process.
print_message "Removing unnecessary packages to save disk space..."
sudo apt-get autoremove -y
apt-get autoremove
: This command removes packages that were automatically installed to satisfy dependencies for other packages but are no longer needed (e.g., if the package they depended on has been uninstalled). This helps free up disk space.-y
: Automatically confirms the removal.
print_message "Upgrade installed Flatpak applications..."
flatpak upgrade -y
flatpak upgrade
: This command updates all installed Flatpak applications to their latest versions. Flatpak is a universal packaging system for Linux.-y
: Automatically confirms the Flatpak upgrades.
print_message "Refreshing Snap..."
sudo snap refresh
snap refresh
: This command updates all installed Snap packages to their latest versions. Snap is another universal packaging system, primarily developed by Canonical (for Ubuntu).snap refresh
typically doesn’t require a-y
flag as it’s non-interactive by default.
Shutdown Sequence
# --- Shutdown Sequence ---
print_message "All updates and upgrades are complete."
echo "" # Empty line for spacing
echo -e "\033[30;46mSystem will shut down in 60 seconds...\033[0m" # Cyan background for shutdown message
for i in {60..1}; do
echo -ne "\033[K" # Clear current line
echo -ne "\rShutdown in $i seconds... Press Ctrl+C to cancel."
sleep 1
done
echo "" # New line after countdown
print_message "Initiating system shutdown now!"
sudo shutdown -h now
This final section manages the countdown and system shutdown:
print_message "..."
: Displays a message indicating completion of updates.echo ""
: Prints an empty line for visual spacing.echo -e "\033[30;46mSystem will shut down in 60 seconds...\033[0m"
: Prints an initial shutdown message with black text on a cyan background.for i in {60..1}; do ... done
: This is afor
loop that iterates from60
down to1
.echo -ne "\033[K"
: Clears the current line from the cursor’s position to the end. The-n
flag prevents a newline, and-e
enables escape sequence interpretation.echo -ne "\rShutdown in $i seconds... Press Ctrl+C to cancel."
:\r
: A carriage return character. It moves the cursor to the beginning of the current line without moving to the next line. This, combined with\033[K
, makes the countdown number appear to update in place on the same line.$i
: The current value of the loop variable (the countdown number).
sleep 1
: Pauses the script for 1 second, creating the countdown effect.
echo ""
: After the loop finishes, this prints a final newline to ensure subsequent output starts on a fresh line.print_message "Initiating system shutdown now!"
: Displays a final message before the shutdown command.sudo shutdown -h now
:shutdown
: The command to power off or restart the system.-h
: Specifies that the system should be halted (powered off).now
: Instructs the system to shut down immediately.
How to Use the Script
- Save the code: Copy the script content and paste it into a plain text file. Save the file with a
.sh
extension (e.g.,full_update_shutdown.sh
). - Make it executable: Open your terminal, navigate to the directory where you saved the file, and run:
chmod +x full_update_shutdown.sh
This command gives your script permission to be executed. - Run the script: Execute the script from your terminal:
./full_update_shutdown.sh
You will be prompted to enter yoursudo
password at the beginning when the script attempts to run commands requiring administrative privileges.
-
This reply was modified 2 weeks, 6 days ago by
thumbtak.