Bash Scripting

Bash (Bourne Again SHell )is a Unix shell or we can say it’s a command line interpreter which provides a traditional user interface for UNIX Operating System and for other similar Unix systems. This was the replacement for Bourne shell and was released in year 1989. This is distributed GNU OS as a default shell on Unix or LINUX operating system. This is POSIX shell with more number of extensions.

We can execute the BASH using command line or writing a series of commands on a text file and save it with “.sh” extension. If we want to execute any BASH script then first we have to provide the execute permission and if we don’t provide the permission or forgot to provide then it will simply given an error message by saying “Permission denied”.

For example here is my very simple BASH script which is named as “hello.sh” .

#!/bin/bash
# Sample Bash script
echo Hello Sloba!

Now let us see if we have the execute permission or not for the above script.

login as: sloba
sloba@**************'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)

Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Sun Oct 11 18:29:48 2015 from 192.168.2.8
sloba@sloba-VirtualBox ~ $
sloba@sloba-VirtualBox ~/Desktop/BASH $ ls -lrt
total 4
-rw-rw-r-- 1 sloba sloba 50 Nov 17 09:25 hello.sh
sloba@sloba-VirtualBox ~/Desktop/BASH $

 

And below is the screen shot from the server using WinScp tool.

image

 

Now we see that we don’t have the execute privileges on the script but let us go ahead and try to run it.

sloba@sloba-VirtualBox ~/Desktop/BASH $ ./hello.sh
-bash: ./hello.sh: Permission denied
sloba@sloba-VirtualBox ~/Desktop/BASH $

Now we can see that we get an error with “ Permission denied ”. Now let us try to provide the execute privilege  to our script and then run it.

ls -lrt
total 4
-rwxrwxrwx 1 sloba sloba 50 Nov 17 09:25 hello.sh
sloba@sloba-VirtualBox ~/Desktop/BASH $ ./hello.sh
Hello Sloba!
sloba@sloba-VirtualBox ~/Desktop/BASH $

Finally we are able to get the result of the simple hello script after giving the execute privilege, in general 755 permission is provide to execute the script or modify by the owner .

To execute the script we have seen that we use “./” before the script . The Dot means it is pointing to the current directory to find the script and execute it. And if we keep the script any other directory and we can use something like this “./<<path>>/hello.sh” .

We also see from the script that the first line starts with “#!” , this is called Shebang . The hash and exclamation mark is the character sequence which is basically referred as shebang , after that what we see is the interpreter path which should be used to execute the program for the next set of lines in the file.

 

Thank you for reading my blog . Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s