Interview Questions

Shell Script Tutorial

What is Shell?
In the Unix Operating System, Shell is the Command interpreter where you can execute System commands defined by the Operating System.

What is Shell Script?
Shell Script is a plain text file which has set of commands to be executed automatically. Generally, these commands are executed by the System users. Instead of executing all the commands manually, when these commands are placed in a common file for execution then it is termed as Shell script.

Why to use Shell Script?
1. Manual or repeated tasks can be automated easily.
2. Any complex processing can be easily programmed.
3. Command execution is faster and saves lot of time.
4. Very much useful for System management tasks.

How to create Shell script?
If you are new to Shell script then read the below Shell Script execution procedure. Others can skip this section.

1. Create a text file and name it is as “Test.sh”
2. Open the file and place the below commands in the script file.

#! /bin/sh
whoami

The first line of the Script will have the path of the interpreter. In our case, it is in /bin/sh

3. Check whether the file has executable permission.
a. Execute “ls –ltr Test.sh” and check for the execute permission
b. If it doesn’t has execute permission, then execute the below command
     chmod 755 Test.sh
4. Now execute the file as, sh Test.sh. This will display the logged in user name.

Note: All the above examples are developed and tested using Cygwin unix emulator tool.