Automate safe system upodates with a single script (for APT + systemd systems)

THE PROBLEM Keeping a Linux system fully updated usually means doing several things by hand: Update APT package lists Upgrade installed packages Remove unused dependencies and cached files Update Flatpak apps (if you use Flatpak) Update firmware via fwupd (if available) Decide whether to reboot or shut down None of that is hard, but it is repetitive and easy to skip steps, especially firmware updates. This script turns that whole workflow into a single, safe command. REQUIREMENTS This script assumes: Package manager Uses APT Example: Debian, Ubuntu, Linux Mint and similar Init system Uses systemd (for systemctl reboot/poweroff) Shell bash (script uses “#!/usr/bin/env bash” and “set -euo pipefail”) You can run it with: bash script.sh Privileges Your user has sudo rights Optional components Flatpak (optional) If not installed, Flatpak steps are skipped fwupd (fwupdmgr, optional) If not installed, firmware steps a...

Scripting essential

Bash built-in commands GNU manual:
https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html

Built-in, Special built-in and POSIX Inherited command list:
https://www.computerhope.com/unix/bash/index.htm


Basic syntax and special characters


Character Description
#                 Used to add a comment, except when used as \#, or as #! when starting a script
\                 Used at the end of a line to indicate continuation on to the next line
;                 Used to interpret what follows as a new command to be executed next
$                 Indicates what follows is an environment variable
>                 Redirect output
>>                 Append output
<                 Redirect input
|                 Used to pipe the result into the next command


Parameters




Command substitution



Variables

To export the variable in order to be valid also for the child processes:
export VAR=value
or
VAR=value ; export VAR
where "VAR" is the variable we want to export.

When a child process modifies a variable, that remains within the child processes and it doesn't affect the variable of the parent process. In other words, exporting means copying and inheriting.


Functions

Entering a function in a script means:
  1. Declaring a function
  2. Calling a function
The function declaration requires a name which is used to invoke it. The proper syntax is:
function_name () {
   command...
}







Comments