Shell ScriptingWriting Your First Script in Linux Shell Scripting

Writing Your First Script in Linux Shell Scripting

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

[root@centos6 ~]# which bash
/bin/bash
[root@centos6 ~]#

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.

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

Now, execute the script as follows:

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

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:

[root@centos6 ~]# cd /opt/
[root@centos6 opt]# /root/first.sh
Mon Aug 17 17:14:07 EET 2015
Hello, I am learning Linux Shell Scripting
[root@centos6 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:

[root@centos6 opt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos6 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).

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

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

[root@centos6 opt]# pwd
/opt
[root@centos6 opt]# which first.sh
/root/bin/first.sh
[root@centos6 opt]# first.sh
Mon Aug 17 19:30:32 EET 2015
Hello, I am learning Linux Shell Scripting
[root@centos6 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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -