In this series, we are going to learn Linux Shell Scripting. Shell Scripting is a very important skill that UNIX/Linux administrators can never go without.
– Admins?! UNIX and Linux admins?! Isn’t it a scripting language for developers to use?!
No, actually it’s not. Shell Scripting means writing scripts (programs) for the UNIX/Linux Shell. So, let me (before you ask another question) tell you what Linux Shell is.
The computer system consists of two main components: Hardware and Software. The Hardware is the machinery; the circuitry: the electrical, electronic, magnetic, and optical parts that together constitutes the solid part of the computer system. On the other hand, the Software is the soul of the computer system; it is the part that acts as interface between the hard machine and the humans. It acts like a translator that translates the machine language (electric currents, voltage levels, electronic signals, etc.) to the form that the human user can understand and feel comfort to deal with.
The software itself is divided into layers. The one closest to the hardware is called the Kernel. The kernel is the core of the operating system. It is a software that contains the necessary modules (drivers) needed to communicate with the hardware. The layer above the kernel is called the Shell (which is the star of our series). The shell is also a software program that accepts user’s inputs (commands), interprets them, executes them, and presents the output results back to the user.
So, what are Shell Scripts?
A Shell Script is a collection of Linux commands to be executed in sequence. The commands are written and stored in an ordinary ASCII (text) file. When the script is to be executed, the shell reads the file contents, interprets and executes them as if they were typed on the shell command prompt.
Why Shell Scripts?
As being set of Linux commands, a shell script can be used to achieve one or more tasks. For example, a script could be written to start a database, shutdown a database, start backup, check whether a specific service is up, and if not it could start it. So many other tasks can be done using shell scripts. In general, when a specific task is frequently needed, it is wise to write a script that can do it for you. This is much better than having to remember, and type the commands every time the task is needed (which is both silly and error-prone job). Writing shell scripts half-automates the administration tasks, saves the administrator’s time, and minimizes typos effects.
Shell Scripts Combined with Cron Jobs
Crontab job is a UNIX/Linux tool that helps system administrators schedule jobs to run at specific times on periodic basis.
Together, crontab jobs and shell scripts can fully-automate many administration and monitoring tasks. For instance, the weekly backup that run every Saturday at midnight, the purging of old log files that takes place on the first of every month, and the system monitoring utility that checks the CPU, memory, file systems, paging space utilizations every 10 minutes, and notifies the admin by email, all considered typical usages for the shell scripts combined with crontab job automation.
* * * * * *
– Okay… Okay, let’s now start writing shell scripts.
Sir, who are you?!
– Are you kidding?! Don’t you know me?! We are learning Linux Shell Scripting together!!
Aha, nice to meet you sir. But, I can’t see any Linux Shells with you?!
– Linux Shells?! With me?! Are you kidding again?!
Have you ever seen a civil engineer without his T-ruler, or yellow helmet?!
– What do you mean?!
Bring your tools and follow me!!!
* * * * * *
Preparing Your Tools
Before writing shell scripts (which are by definition collections of Linux commands), we must be first aware of how to use the Linux shell, and how to type and execute commands in. And the most important is to have rich knowledge of Linux commands.
In this context, I will assume you know how to login to your Linux box using your credentials (username and password). After successful login, you should be given a command prompt (the shell). This is where commands are written, and their results are seen.
- The Bourne Shell, which is the default shell in many UNIX operating systems.
- The Bourne Again Shell (Bash). This is the de facto standard shell for almost all Linux Distributions, like Red Hat, CentOS, Fedora, Ubuntu, etc.
- The Korn Shell (ksh).
In this series, we will use the Bash shell.
The following table lists some of the basic useful Linux commands that every Linux admin should master, with short definition for each:
* * * * * *
In this article, we had an introduction to Shell Scripting. We have learned that Shell Scripting is a necessary skill for any UNIX/Linux system admin. Together, Shell Scripts and Crontab jobs can help automate many administration tasks. We have seen that several types of shells are available. As a system admin, you must possess a rich knowledge of Linux commands; because they are your toolbox.
I hope you enjoy learning this series with us.