r/linux4noobs 3d ago

shells and scripting [Debian based] bash script to update all packages

Hi !

I'm using PikaOS (debian based) and I'm trying to write a small simple script for updating my packages. I don't know how to since I'm new into linux and even newer on scripting

What I want it to do :

1- launch a terminal
2- display a prompt that asks me if I backed up my files (currently using timeshift, when I'm more used to CLI maybe I will automate backups in that script)
3- If no, echo something like "Do it then"
3b- If yes, go on
4- update system packages (using pikman upgrade ; equivalent of sudo apt update && upgrade)
5- update flatpak packages

Ideally I would like 4 and 5 to execute at the same time with a single command, but if not possible, then do it one after the other.

What I have currently :

#!/bin/bash

#Launch terminal to update and upgrade 
x-terminal-emulator --hold -e pikman upgrade

I'm launching this script from a button of a widget.

PS : When I launch the scrip like this, there is no user@host:~$ like when I launch an empty terminal. So I'm never sure when the script is finished. Is there a way to have that too ?

Upvotes

9 comments sorted by

u/Midnight_Whispers1 3d ago

This should do it!

#!/bin/bash

# PikaOS System Updater
# Checks for a Timeshift backup, updates PikaOS packages,
# then updates Flatpak packages.

x-terminal-emulator -e bash -c '

echo "======================================"
echo " PikaOS System Updater"
echo "======================================"
echo

read -p "Have you backed up your files with Timeshift? [y/N]: " backup

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

echo
echo "Backup confirmed."
echo

echo "======================================"
echo " Updating PikaOS packages..."
echo "======================================"
echo

pikman upgrade

if [ $? -ne 0 ]; then
echo
echo "ERROR: PikaOS package update failed."
echo "The Flatpak update will not be attempted."
echo
read -p "Press Enter to close..."
exit 1
fi

echo
echo "======================================"
echo " Updating Flatpak packages..."
echo "======================================"
echo

flatpak update

echo
echo "======================================"
echo " Updates completed!"
echo "======================================"
echo
echo "You are now back at the shell."
echo

exec bash
'

u/Dr_No-Nymous 3d ago

Aw man, I didn't expect a full written script, thanks so much <3

I just tested it, it works perfectly !

Now all I have left to do is to read and understand it ;)

u/Midnight_Whispers1 2d ago

Let me know if it works!

u/Dr_No-Nymous 1d ago

It does, thanks !

u/Dist__ 3d ago

are you serious about backup before every update?

u/Dr_No-Nymous 3d ago

I'm not doing updates so often, so yeah I prefer doing a backup before updating. Is it not a good practice ?

u/DannoXYZ 3d ago

Definitely believe in backups. Most of my test/demo systems are VM, so easy to do snapshot before upgrades.

Timeshift has saved me numerous times on my host system as well. Don't underestimate usefulness of backups.

Just yesterday at office, I restored a 2-year old backup for one of our devs. We've been having issues with recent build for couple months. Finding original build system and its original configuration along source uncovered bugs that crept in over time.

u/Dist__ 3d ago

i don't want to seem anti-backup.

honestly, the only time i used timeshift, it really saved my install - i did it before upgrading to version 22 when it said it'd be better to make a backup. things went wrong due to some modifications i've done before, so i rolled back just fine!

but never since 2023 i had any critical problems with regular updates. if it broke on every other update, or if i had very sensitive data, then i'd say different...

your choice, anyway.

u/Crafty_Mastodon320 2d ago

This is the oldest debate in computing. Back ups and redundancy are always worth it in most instances. The worst that can happen is a bit of wasted space and time. Vs the best that can happen and is verified stories of people almost losing years of work except for a back up.