How can I write a Bash script to update system and Flatpak packages?

0
4
Asked By MellowCedar42 On

I'm using PikaOS, a Debian-based distribution, and I'm new to both Linux and shell scripting. I want to create a small updater script launched from a widget button. It should open a terminal, ask whether I've backed up my files with Timeshift, stop with a clear message if I answer no, and otherwise run `pikman upgrade` followed by `flatpak update`. Running both update commands concurrently would be nice, but sequential execution is fine if that is safer or simpler. My current script is `#!/bin/bash` followed by `x-terminal-emulator --hold -e pikman upgrade`. I'd also like the terminal to show a normal shell prompt, or otherwise clearly indicate when the update process has finished.

2 Answers

Answered By QuietMaple63 On

Creating a Timeshift snapshot before every update is a cautious choice, especially while you're learning, but it isn't mandatory for every routine package update. You could keep the confirmation prompt for now and later decide whether to automate snapshots or only make them before larger system changes. Also remember that package updates and personal-file backups solve different problems: Timeshift is mainly for restoring system state, while important personal files should have their own backup.

Answered By BriskOtter7 On

A simple approach is to run the checks and both updates inside a shell started by the terminal emulator. For example:

```bash
#!/bin/bash

x-terminal-emulator -e bash -c '
echo "=== System Updater ==="
echo
read -p "Have you backed up your files with Timeshift? [y/N]: " backup

if [[ "$backup" != "y" && "$backup" != "Y" ]]; then
echo
echo "Please create a backup before updating. Update cancelled."
read -p "Press Enter to close..."
exit 1
fi

echo
echo "Updating system packages..."
pikman upgrade || {
echo
echo "The system package update failed; Flatpak updates will not run."
read -p "Press Enter to close..."
exit 1
}

echo
echo "Updating Flatpak packages..."
flatpak update

echo
echo "All update commands have finished."
exec bash
'
```

The `||` block handles a failed `pikman upgrade`. The final `exec bash` starts an interactive shell in the same terminal, which gives you the usual prompt after the script completes. Running the two package managers one after another is generally preferable because both may need authentication, locks, or user interaction.

SunnyQuill19 -

That worked well for me. I mainly needed the final shell because `--hold` only keeps the terminal open; it doesn't provide an interactive prompt by itself.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.