Writing Your First Script in Linux Shell Scripting

1
2072
Linux Shell Scripting (3)

Linux Shell Scripting (3)Now, the show starts… enjoy!!

We know that a shell script is a collection of Linux commands saved together in a file. So, let’s follow the definition and write several Linux commands in a text file.

We have already learned some useful commands in Article (1) – Introduction. Let our first script be a small one that clears the entire screen, and prints the data and time, followed by the message “Hello, I am learning Linux Shell Scripting”

Okay, start your Linux shell, and type the command vim first.sh

In the vi editor page, press i  to enter the insert mode, then type the following commands:

Press Esc to return to the command mode, then type :wq and press Enter to save and exit.

To execute the script, type the following on your shell:

1
The Shebang
To execute our first script, we used the command bash followed by the script name. The command bash invokes the language interpreter that will execute the commands read from the script file.
An equivalent approach is to use a special command statement called Shebang. The Shebang usually appears as the first line of a script. Its purpose is to tell the program loader to use the bash as the interpreter for executing this script.

To use the Shebang, we need to know the path of the interpreter executable file “bash”. This can be done using the command which

Now, open your script for editing, and insert a new line before the first, and type the following as the first line:
#!/bin/bash

Now, your file should appear like this:
2
Save and exit the file.

Next, we need to make the file executable. As you remember from the last article, this can be done using the chmod command.

Now, execute the script as follows:

The new line we inserted at the top of the script consisted of the Shebang statement. In general, the Shebang is written in this way:

In our case, the full path of the bash executable (as we obtained from the which command) is /bin/bash , so the Shebang statement becomes:

3

Learn the Basics of C Programming Language
Executing Scripts using the Full Pathname
In the previous section, we executed the script using the following method:

Where:
scriptname.sh is the name of the script, and
./ means the file is in the current directory.

So, what if we need to execute the script while we are on another directory other than the one containing the script?

In this case, we can execute the script using its full (absolute) pathname:

Executing Scripts as Linux Executable Commands
Scripts can be executed the same way Linux commands are executed. At the shell, when we type a command (like any of the ones we learned in Article 1 – Introduction), the shell checks a variable called PATH. This is an environment variable that tells the shell which directories to search when looking for the Linux executable commands. The value of the PATH variable may differ from system to another, and even from user to another on the same machine. To see the value of the PATH variable, type the following command at the shell:

So, for an executable file to be executed as a command, it should exist inside one of the above (colon-separated) directories. Let’s move our script to the directory /root/bin (after checking it exists, and creating it if not).

Now, let’s try executing the script as if it were a command:

It worked as expected!! Congratulations!!!

* * * * * *

Using Comments
One of the best programming habits that I always mention and stress on is writing comments. Comments are readable sentences inserted in a computer program code in order to make it easier to understand. Comments need to be meaningful; otherwise, they won’t be useful. For this purpose, comments should be selected and written carefully. Comments are totally ignored by the interpreter. To mark a portion of text as comment, precede it with hash character “#”. Once appeared in a line, all the characters that appear to the right of the hash character up to the end of the line are considered part of the comment.

For example:
The following represents a full line comment:

While the following line have a statement followed by a comment that illustrate it.

* * * * * *

In this article, we have written our first script. We learned how to execute it using either of three possible ways. We have seen the Shebang, and its usage. We have also talked about comments and their importance in computer programs as whole, not only shell scripts.

The concepts discussed in this article, and in Article 1 and Article 2 are very important, as they constitute the base we will build upon in the next articles. So, if you miss something, kindly revisit the three articles until you feel comfortable with what you learned.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here