LinuxTechnology

Pulling the legos apart part 3

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 housekeeping and leave methods. Which do exactly what you would think they do.
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
}

housekeeping() {

	sudo apt-get autoremove -y;
	check_exit_status

	sudo apt-get autoclean -y;
	check_exit_status

	sudo updatedb;
	check_exit_status
}

As depicted above the sudo or super user do is using apt-get to autoremove. So if you have removed an application since you last ran this particular script the dependencies it had will stay on your system. So apt-get autoremove will remove those dependencies that were installed with applications and that are no longer used by anything else on the system and the -y is an option used by autoremove to answer "Yes" to the question posed by the autoremove. Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control. Won't insult your intelligence you know what the -y does by now. And updatedb, does alot of what you think 'locate' and 'updatedb' are the front runners in searching for the existence of any file on a Linux system. In order to operate efficiently, 'locate' uses a database rather than hunting individual directory paths. For this to operate, and remain current, it means the database itself must be updated which is why it is included this way.

leave() {

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

The leave method prepped here leaves some white space and shows that update is completed.
And while we have reached the end of this script we can show that the methods/functions are called in order separated by a space exactly as they are made and yes in linux uppercase and lowercase are essential. I truly hope that this explanation has helped someone learn and take the smallest steps towards learning how to script.

Comment here