Keeping your Ubuntu system updated and installing essential packages can be done efficiently using a simple Bash script. This guide will walk you through creating a script that automates system updates, upgrades installed packages, and installs specific packages of your choice. You can run the script manually or schedule it to execute automatically at a set time using cron jobs.
Step-by-Step Guide
Step 1: Create the Bash Script
Start by creating a new script file, for example, update_and_install.sh. This script will handle all the necessary steps for updating your system.
Example Script: update_and_install.sh
#!/bin/bash
# Update package list
echo "Updating package list..."
sudo apt update
# Check if dpkg was interrupted and run 'sudo dpkg --configure -a' if necessary
if [ $? -ne 0 ]; then
echo "Checking for dpkg errors..."
dpkg_error=$(sudo apt update 2>&1 | grep "dpkg was interrupted")
if [ -n "$dpkg_error" ]; then
echo "Error detected: