Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 18 December 2016

Introduction - Linux / Unix Shell (Bash) Scripting

Introduction to Linux / Unix Shell (Bash) Scripting - It is a great pleasure to tell you that, we have started a new tutorial series on Bash or Linux/Unix Shell Scripting and with this article, we will be getting started with the topic. This is the introductory article on Shell scripting and intended to provide you a primer on the topic. So, let's begin.

learn-linux-unix-shell-scripting

What is a Shell Script?

The word 'script' suggests that someone is being instructed to do something. It can be a director instructing an actor to perform a scene in particular manner, a choreographer instructing a dancer to do a dance move in more elegant way or a coach instructing a Soccer player to adjust his position. In our case, it will be a programmer instructing a computer to perform a task. But, what task would that be?

Generally, anybody who would like to perform some tasks frequently, they automate it by writing shell scripts. A shell script is nothing but a file consisting a series of Linux/Unix shell commands, which we can normally run on terminal. Thus, whatever that can be run on the terminal, can be placed and executed using shell script. Of course, the sequence of the commands matters a lot for the script to behave properly. Thus, to learn shell scripting, you must be aware of important Linux / Unix commands, their options and usage.

Important Points to Remember

There are certain rules and conventions to be followed while writing a shell script. Bash, just like many other programming languages, provides conditional statements (if-else), looping statements (for and while) and functions, with which we can take certain decisive actions, perform a task repeatedly or create a re-usable block of code. We will be learning them in much more details in this series. For now, we see certain basic information you need to know when you write shell scripts.

1. The extension – As you may know that, extensions to not matter in Linux / Unix. A file may or may not have an extension. But, it’s a good practice to give an extension to a file, so that we get to know whether it is a PDF file or a Text file. Similarly, for shell script, we use .sh as an extension. Not compulsory, but recommended.

2. A running script = Process – As mentioned earlier, a script may contain sequence of several commands, conditional statements, loops, functions or even a call to another script. Whenever a script is executed, a replica of the script, to which we refer to as a Process, starts running. It fetches all the instructions and declared variables into the memory. A separate memory is allocated to store intermediate result and process state. When the process exits, all the reserved memory is freed up. Being a process, it has an identification number associated with it, called as PID or Process ID, with which its state (running/finished) can be determined.

3. Shebang – The first line of a shell script starts with #!/bin/bash and is referred to as Shebang or Hashbang. It instructs the OS to use a particular interpreter to run subsequent block of code, /bin/bash in our case. When writing a Python script, you would have used /usr/bin/python as the interpreter.

4. Setting the File Permission – Whenever a file is created, it is associated with certain permissions, namely – Read, Write and Execute. Whenever a script needs to be executed, it must have the Executable permission. If no proper permissions set, you might face an error message on the terminal screen. To do so, we use chmod command as - chmod 755 <myScript.sh> or chmod +x <myScript.sh>. Please check our article on File permissions to learn more about chmod command.

5. Executing the shell script – Once a file has executable permission, it is ready to be run. There are two ways with which you can run a script – using relative/absolute path and using bash command by providing script name as an argument to it, as bash <filename>. If you are in the same directory as that of the script, you can simply run it as ./myScript.sh (needless to mention that . indicates current working directory). But, bash must know the path where you have kept your script, in order to run it, or it will give out error message.

6. The $PATH variable - Whenever we do not mention absolute path (as /path/to/myScript.sh), bash will search for myScript.sh in certain directories. These directories are specified in the $PATH variable. If you run echo $PATH, you would see something like -

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

These are the directories which will be looked in for myScript.sh and it is executed as soon as it is found. Thus, whenever you wish to run your script as a shell command, without needing to mention absolute/relative path, you can put it into one of those directories.

7. Indentation(?) - Well, unlike Python, where indentation matters, bash scripts need not have code inside indented. But, a good programmer will always use indentation to prettify the code to make it look more elegant. Again, recommended, but not required.

Putting it together

In this section, we join the pieces together, whatever we have learned in above portion, and create our first 'Hello World' shell script - helloWorld.sh. It is very simple, the ingredients required for this recipe is just a shebang and a statement that will print out 'Hello World' on your terminal. Let's make it then and this is how our script will look like-

#!/bin/bash

echo 'Hello World !'

Make it executable, we use chmod command as below.

# Before making it executable
$ ll helloWorld.sh
-rw-r--r-- 1 root root 34 Dec 14 14:02 helloWorld.sh

$ chmod +x helloWorld.sh

# After making it executable
$ ll helloWorld.sh
-rwxr-xr-x 1 root root 34 Dec 14 14:02 helloWorld.sh

Now, time to execute it.

# With './' - it will work, as file is in the same directory
$ ./helloWorld.sh
Hello World !

# With absolute path
$ /root/helloWorld.sh
Hello World !

# With filename as argument to 'bash'
[root@LinuxBox ~]$ bash helloWorld.sh
Hello World !

# This won't work, as '/root' is not in $PATH
[root@LinuxBox ~]$ helloWorld.sh
-bash: helloWorld.sh: command not found

With this, we close our discussion on shell scripting introduction. Please let us know about your views in the comment section below and stay tuned for more articles on this topic. Thank you.

0 comments:

Post a Comment