Automate Tasks on Linux with Scripting

Topics Covered

Overview

In the world of Linux, it is common for system administrators to come across various repetitive tasks that are to be performed frequently. Such tasks can be tedious, time-consuming, and prone to human error. This is where automation comes in to save the day.

In this article, we will explore several scripting concepts and understand how system administrators use scripting to get their tasks automated in Linux. We will start with an introduction to shell scripting. Furthermore, we will learn how to write shell scripts that will help us automate tasks.

By the end of this article, you will have several automated tasks in Linux, which you will be achieving, by writing your very own shell scripts.

Introduction

Linux is an open-source operating system that is widely used in various applications, including web servers, databases, and other mission-critical applications. Linux system administrators are responsible for managing the system and ensuring optimal performance. As the complexity of systems increases, managing these tasks manually becomes cumbersome and prone to human error. With the help of shell scripts, we can automate some of these repetitive and error-prone tasks improving efficiency, accuracy, and productivity.

Scripting allows system administrators to automate various routine tasks, such as file backups, log file analysis, performance monitoring, and system updates. We will learn more about shell scripting further in this article and by the end of it, we will have our first system administration task that has been automated using shell scripts.

Why Automate?

Who wants to deal with the hassle of heavy, error-prone, or similar tasks every time you want to monitor the system or just look for something as simple as the IP address and go through all that gibberish?

Surely system administrators don't, and we shouldn't too. Let us go through a few reasons why we should automate system administration tasks:

  • Increases efficiency:
    Automating tasks increases the efficiency of system administrators as it reduces the time required to complete routine tasks. System administrators can automate tasks such as backups, log file analysis, and system updates, freeing up time to focus on more critical tasks.
  • Improves accuracy and consistency:
    It improves accuracy and consistency in the execution of tasks. When tasks are automated, the risk of human error is reduced, and tasks are executed consistently across multiple systems, ensuring that the systems are in a desired state.
  • Scalability:
    Automation allows for better scalability. As the number of systems to be managed increases, it becomes increasingly difficult to manage them manually. Using scripts for the automation of tasks allows for better scalability and makes it easier to manage large-scale deployments.
  • Security:
    By automating tasks such as system updates and patching, system administrators can ensure that systems are up-to-date with the latest security patches, reducing the risk of security breaches.

What is a Shell Script?

A shell script is a text file that contains a series of commands and instructions to be executed, or a program that is written in a scripting language. Shell scripts can be written using text editors such as Nano, Vim, or any other text editor and are saved with the .sh extension. These are executed by the shell interpreter and can be run whenever required from the command line.

The syntax of shell scripts is similar to that of programming languages and contains similar concepts such as variables, control statements, conditional statements, and functions. The most commonly used scripting languages used to write shell scripts in Linux are Bash, Korn Shell, Z Shell, Perl, and Python.

Bash or Bourne-Again SHell is the default shell on most Linux distributions due to its simplicity and flexibility. We will be learning how to write shell scripts and a lot more using Bash further in the following sections.

How to Run a Shell Script?

To understand how a shell script runs, we need to create a shell script using any editor of choice. In this example, we will be using nano.

Let us create a shell script named "hello_world.sh", which will print a greeting.

This will create a text file where we can start writing our script.

Writing a Shell Script

In shell scripts, the very first line we write is a shebang followed by commands and instructions. No matter how bizarre it sounds, shebang or "#!" is just a single statement that we need to use to tell the operating system which interpreter to use. For example, if we are using bash we need to provide the path of the bash interpreter preceded by "#!".

To find the path of a specific scripting language interpreter, we can use the "which" command followed by the name of the scripting language.

In case, shebang is not used, the operating system will execute the script using the default interpreter.

Finally, after writing all the commands and instructions, save the file. To save the file press Ctrl+x, followed by y to save the modified buffer, and Enter to finalize the filename.

Executing a Shell Script

We can run shell scripts by the following methods:

By specifying the name of the interpreter before the name of the file. For example, if we are running a Bash script, the following can be used.

We can also run a shell script like a regular program in case, in case the interpreter is not known. To do that we need to change the permissions for the file, and make it executable.

By default, shell scripts only have read and write permissions. To modify it and make the script executable like other programs we can use the "chmod +x" command followed by the filename.

We can now run the scripts by the name of the script preceded by its path.

executing-a-shell-script

Now that we know how to create and run a shell script, let us go through some of the scripting concepts which will help us automate various system administration tasks.

Performing Tasks Using Bash Scripts

To understand the various tasks we can perform using a shell script and its possibilities, let us go through some examples where we perform various tasks using Bash scripts.

Taking Input from the User and Storing it in a Variable in Bash

Variables are used to store data temporarily during the execution of a script. A variable is essentially a name that is associated with a value, which can be a string, a number, or any other data type. In Bash scripts, we can take multiple inputs from users and store them in variables. These variables are referenced using the "$" sign.

Example:

Output:

taking-input-from-the-user-and-storing-it-in-a-variable-in-bash

In the above script, we prompt the user for their name, read the input using the read command, and store it in the name variable. We then print a personalized greeting using the echo command and the name variable.

Using Conditional Statements for Decision-Making in Bash

Conditional statements in bash allow us to make decisions based on the value of a variable or the outcome of a command. They are typically used to control the flow of a script and execute different sections of code based on certain conditions. Conditional statements used in Bash are "if", "elif", "else" and case statements. In this example, we are going to use an if-else pair.

Example:

Output:

using-conditional-statements-for-decision-making-in-bash

In the above example, we have created a script named "check_command.sh", that takes a command as an argument and checks if it exists or not. This script uses the command command along with the -v option to check if the command passed as an argument exists. The if-else conditional pair is then used to return an installed or not-installed acknowledgment based on the condition.

Note:
Unlike other programming languages, The "if" statement in bash is closed using "fi", and the "case" statement is closed using "esac".

Performing Repetitive Tasks Using Loops in Bash

Like other programming and scripting languages, Bash also utilizes loops. These are used to execute a set of instructions multiple times. With the help of loops in shell scripts, various system administration tasks can be automated. In Bash, there are three types of loops, these are for loop, while loop, and until loop. In This example, we are going to use a for loop.

Example:

Output:

performing-repetitive-tasks-using-loops-in-bash

In the above script, a for loop is used to iterate over all the ".txt" files in the current directory. The wc -l < "$file" command is used to count the number of lines in each of those files and the echo command is used to print it along with the respective filenames.

Automation Using Shell Scripts

Until here, we have gone through a few shell scripting concepts and learned how shell scripts are used, let us now automate a few real system administration tasks with the help of shell scripting.

In this example, we are going to write a bash script that automates the task of monitoring the CPU usage of a Linux system. The following script will set a threshold for CPU usage and if the CPU usage is higher, it sends a warning email notification to the system administrator.

Example - 1:

Output:

example-1-of-automation-using-shell-scripts

Explanation:

In the above script, two variables named threshold and current_cpu_usage are created. The threshold variable is assigned the CPU usage threshold and the current_cpu_usage variable is used to store the current CPU usage.

To extract the current CPU usage, the top -bn1 command is used to produce a one-time snapshot of the current state of the system. The output is then piped into the grep command that filters the output to only include the line that contains CPU usage information. Lastly, the awk command, is used to extract the second and fourth fields from the filtered output. These are then added and stored in the current_cpu_usage variable.

Finally, an if statement is used to compare these variables, and if the current CPU usage is greater than the threshold, an email is sent to the system administrator using the mail command, and a warning message is prompted. If the threshold is greater, an acknowledgment message stating that the CPU usage is under control is prompted.

Example - 2:

In this example, we will create a script that will help users create a new user account and set a password for that.

Output:

example-2-of-automation-using-shell-scripts

Explanation:

The above script prompts the user for a username and password. It then creates a new user account using the useradd command and sets the password for it using the chpasswd command. Lastly, it checks if the account creation was successful and displays an acknowledgment message.

Example - 3:

In this example, we will create a bash script that will iterate through a source directory and will back up all the text files in a destination directory.

Output:

example-3-of-automation-using-shell-scripts

Explanation:

The above script prompts the user for the source and the destination directories. It then checks if the source and destination directories exist. If the source directory doesn't exist it exits prompts an error message and exits the script. In case, if the destination directory doesn't exist, the script creates a destination directory of the name provided by the user.

Then using a for loop the script iterates through the source directory and copies all the text files to the destination directory using the cp command and prints an acknowledgment message.

Finally using the if-else conditional pair the script checks if the backup was successful and prints a relevant acknowledgment.

Conclusion

  • Shell scripting can help automate repetitive tasks that need to be performed regularly. This not only saves time but also reduces the chances of errors due to manual intervention.
  • A shell script is a set of commands in a text file. These are written in a scripting language that can be executed by a shell interpreter. Bash is the most commonly used scripting language among Linux users.
  • To create a script, a text file is saved using the ".sh" extension. The first line of the script, known as the shebang, is used to specify the interpreter to be used.
  • To run a shell script we, either need to execute it by specifying the interpreter name before the file name, or by changing the permissions to make it executable.
  • With shell scripting, system administrators can automate various tasks such as monitoring disk usage, managing user accounts, creating backups, and many more.
  • By automating system administration tasks with shell scripting, system administrators can improve the overall efficiency and reliability of their systems, reduce downtime, and improve security by ensuring that critical tasks are performed regularly and consistently.