LinuxTechnology

Pulling the legos apart part 2

scripting is just legos

Moving on from the other day we will be discussing the next two methods/functions that are found in this script below.

#!/bin/bash

check_exit_status() {

	if [ $? -eq 0 ]
	then
		echo
		echo "Success"
		echo
	else
		echo
		echo "[ERROR] Process Failed!"
		echo
		
		read -p "The last command exited with an error. Exit script? (yes/no) " answer

            if [ "$answer" == "yes" ]
            then
                exit 1
            fi
	fi
}

greeting() {   <<--this one

	echo
	echo "Hello, $USER. Let's update this system."
	echo
}

update() {    <<--and this one

        sudo apt-get update;
	check_exit_status

        sudo apt-get upgrade -y;
	check_exit_status

        sudo apt-get dist-upgrade -y;
	check_exit_status
}

housekeeping() {

	sudo apt-get autoremove -y;
	check_exit_status

	sudo apt-get autoclean -y;
	check_exit_status

	sudo updatedb;
	check_exit_status
}

leave() {

	echo
	echo "--------------------"
	echo "- Update Complete! -"
	echo "--------------------"
	echo
	exit
}

greeting
update
housekeeping
leave

The two methods pointed two here are greeting and update methods. Which do exactly what you would think they do.
greeting() {

echo
echo "Hello, $USER. Let's update this system."
echo
}

update() {

sudo apt-get update;
check_exit_status

sudo apt-get upgrade -y;
check_exit_status

sudo apt-get dist-upgrade -y;
check_exit_status
}

greeting() {   <<--this one

	echo
	echo "Hello, $USER. Let's update this system."
	echo
}

As depicted above the greeting says hello $USER which the $USER is actually you and whatever user name you have chosen when you installed linux.

update() {    <<--and this one

        sudo apt-get update;
	check_exit_status

        sudo apt-get upgrade -y;
	check_exit_status

        sudo apt-get dist-upgrade -y;
	check_exit_status
}

The command sudo is obviously super user do but you already know this from previous discussion. The "apt-get update" is the portion of the command that uses aptitude to collect updates from your sources list generally located at /etc/apt/sources just a text file with a listing of urls you can see yours like this.

sudo nano /etc/apt/sources.list.d

The check_exit_status is an invocation of the method made earlier in the script by the same name. Which does what was described in the previous post.
The subsequent upgrade and dist-upgrade the latter of which you can leave off to avoid malfunctions that could come from programs not being ready to move to the new version of the OS.
The -y is a simple yes added to those commands to answer the yes or no question associated with them throughout their execution. Upgrade will actually install the updates that had been downloaded from the use of the update command at the beginning of this method.
This was a relatively short post but taking scripting in bite size pieces is probably the best method to learn how to do this.

Comment here