Introduction to Scripting - LINUX: Easy Linux For Beginners, Your Step-By-Step Guide to Learning The Linux Operating System and Command Line (2015)

LINUX: Easy Linux For Beginners, Your Step-By-Step Guide to Learning The Linux Operating System and Command Line (2015)

Chapter Nine: Introduction to Scripting

We have come to the last chapter of the book. By this time, you are already familiar with how a Linux environment works and you must have already noticed how different it is from your previous OS. My suggestion to you is to take the experience a notch higher and start to learn how to create scripts in Linux. In this chapter, I will give a quick preview of scripting in Linux.

Choosing A Text Editor

Shell scripts are text-based files. To start your scripting journey, choose a text editor that works best for you. Linux distributions come with pre-installed text editors but the most commonly used ones are listed below:

· vi – Usually installed by default. Preferred by administrators because it is a powerful editor that is small in size and flexible.

· emacs – contains a lot of features but is not beginner-friendly

· pico – simplified version of emacs (without the features)

· nano – a clone of pico but comes with features

Download the packages for these editors and experience using it yourself. I personally use vi because of its ease in use, simplicity, and my overall comfort and familiarity with it.

Figure 31: vi text editor

Vi uses keys for commands. Here are some examples:

vi Command

What it does


Type this in command mode to insert a new line and enter text


Type I to insert succeeding character to the left side of the cursor


Undo changes


To quit insert mode


To save your changes and quit

Table 12: Sample vi keys

Have you chosen a text editor? Let’s now proceed to trying a simple script.

Simple Scripting

A script is a program that can be interpreted by a shell or a compiled program. We call them shell scripts in Linux because most scripts are run in bash or in any other shell (ksh, csh, bash).

Scripts are useful in automating and simplifying administrative tasks, log monitoring of the system, and data processing. To begin scripting, open a text file with vi or a text editor of your choice.

The first line in the file indicates which shell should be used to interpret the script.


This indicates that the bourne shell should be used to interpret the script.

Let’s do a simple script. Enter the following lines of script. Save the file as Ensure that the file permission for the user is set to 744 so you can run the script.


echo "My name is: $1"

echo I’m using Linux distro: $2

echo Today is: $3

Run the script along with 3 arguments:

./ Steve Fedora Tuesday

The output should be like this:

My name is: Steve

I’m using Linux distro: Fedora

Today is: Tuesday

Here’s an example of how you can use it in maintaining your computer’s performance. If you have already used 80% of your disk space, it might be good to check which files are the biggest in your computer.

Write a new script with the command below:

find / -type f -atime +30 -size +1000k -exec ls -l {} \; > /tmp/filecleanup

This shell script will generate a new file called fileCleanup containing a list of old and big files. As an administrator, the next step is to contact the file owners to give them a notice before deleting these files.

As you might have observed, you can combine different Linux commands with variables, conditional expressions, loops and functions to create your scripts.

At this point, I highly encourage you to proceed in learning advanced Linux where you will learn about more sophisticated and more powerful awk and sed utilities. These two are especially handy when dealing with data.

Here is a quick recap of what we covered in case you need a refresher on a certain step:

1. You now have an understanding of what Linux is.

2. You now know how Linux compares to other operating systems.

3. You are now familiar with the different Linux distributions and how they differ from each other.

4. You learnt how to prepare for a Linux installation and now have an idea how Linux is installed.

5. You now know the different Linux desktops and their corresponding look and feel.

6. You now have an insight on how a Linux filesystem looks like.

7. You can now navigate in a Linux filesystem.

8. You learnt the basic administration tasks in Linux.

9. You can now create users and restrict access in Linux as part of securing your system.

10. You now have an idea of how scripting works in Linux.

Turn to the next page to gain access to a free video course and to also see my other best-selling books part of this series!