Writing Your First Script in Linux Shell Scripting

1
3015
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:

clear
date
echo "Hello, I am learning Linux Shell Scripting"

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:

bash first.sh

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

[[email protected] ~]# which bash
/bin/bash
[[email protected] ~]#

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.

[[email protected] ~]# chmod a+x first.sh
[[email protected] ~]#
[[email protected] ~]# ls -l first.sh
-rwxr-xr-x. 1 root root 73 Aug 17 16:56 first.sh
[[email protected] ~]#

Now, execute the script as follows:

[[email protected] ~]# ./first.sh
Mon Aug 17 16:59:49 EET 2015
Hello, I am learning Linux Shell Scripting
[[email protected] ~]#

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:

#!/fullpath/to/the/interpreter/executable

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:

#!/bin/bash

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:

./scriptname.sh

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:

[[email protected] ~]# cd /opt/
[[email protected] opt]# /root/first.sh
Mon Aug 17 17:14:07 EET 2015
Hello, I am learning Linux Shell Scripting
[[email protected] opt]# 

/root/first.sh is the full pathname for the file first.sh

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:

[[email protected] opt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] opt]#

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).

[[email protected] opt]# ls /root/bin
ls: cannot access /root/bin: No such file or directory
[[email protected] opt]# mkdir /root/bin
[[email protected] opt]# mv /root/first.sh /root/bin/

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

[[email protected] opt]# pwd
/opt
[[email protected] opt]# which first.sh
/root/bin/first.sh
[[email protected] opt]# first.sh
Mon Aug 17 19:30:32 EET 2015
Hello, I am learning Linux Shell Scripting
[[email protected] opt]#

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:

# This Script Prints numbers from 1 to 100 in ascending and descending orders.

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

date               #Prints the Current date and time.

* * * * * *

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