LinuxTechnology

Let’s do some scripting

bash scripting

I'm lazy. I admit it. I believe so strongly in being lazy that I'll do almost anything to facilitate even more laziness in the future. For example, if I think there is a slight chance that I will ever have to perform the same set of commands again, I create a shell script then and there. When I need to do that task again, I lazily execute my script. If maintenance needs to be performed on a system at 2: 00 in the morning, I write a script that does the required work and schedule a job to run it. Lazy people love to sleep. I also believe in helping people find their inner laziness. Why? Because lazy people solve problems. They eliminate the unnecessary. They simplify the complex.

I want to share with you some of my favorite tips and tricks I've used to automate repetitive, monotonous, tedious, and complex tasks. May they inspire you to be lazy.

First lets check it out and create the script using a text editor called nano.

daman@f-ingcomputer:~$ sudo nano FirstScript.sh

That little command sudo stands for "SUper user DO" you'll have to put in your password of course with the use of sudo without any additional commands. The file will be created and you will be able to use nano and follow the simple to use controls at the bottom of the screen. So, be sure to "write" the file and save it with the following.

#!/bin/bash <<---this bit will be commented out thanks to the # or hash 
echo "Scripting is fun!"

Before you try to execute the script, make sure that it is executable.

daman@f-ingcomputer:~$ sudo chmod 755 script.sh

This command "chmod" is used to change permissions of a particular file the numbers 755 dictate the level of which the file is permitted to be used and executed, and we will get into that later on.

So the desired outcome of the script execution is for the terminal to output the following.

    "Scripting is fun!"
chmod 755 FirstScript.sh

As you know the FirstScript.sh is our file name but not only that it is the spot on our drive where our actual file is located so if you wanted to be more accurate it would look something like this

daman@f-ingcomputer:~$ sudo chmod 755 ~/FirstScript.sh

"WAIT DUDE I DON'T KNOW WHAT THIS SHIT IS!!"

A script is a command line program that contains a series of commands. The commands contained in the script are executed by an interpreter. In the case of shell scripts, the shell acts as the interpreter and executes the commands listed in the script one after the other. Anything you can execute at the command line, you can put into a shell script. Shell scripts are great at automating tasks. If you find yourself running a series of commands to accomplish a given task and will need to perform that task again in the future, you can— and probably should— create a shell script for that task. Let's look at a simple shell script. The name of this script is script1. sh.

Scripts can look intimidating like this one.

#!/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() {

	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
}

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

But really it is more like legos, on this site I stray away from using the word "FOO" its simply ridiculous and not exactly useful unless you're familiar with coding tutorials from around 10 years ago. So, over the next few days we will be breaking down this script for parts and showing you what they are and how to manipulate them.

Comment here