Shell Scripting Introduction

Shell Scripting

Introduction to shell scripting

Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

What is Kernel?

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system. It manages following resources of the Linux system.

 File management

 Process management

I/O management

Memory management

Device management

What is a shell ?

A shell is special user program which provide an interface to user to use operating system services. A shell accepts human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal

 

Types of Shells

Bourne shell (sh)

Korn shell (ksh)

Bourne Again shell (bash)

POSIX shell (sh)

C Shell

  1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:

  • POSIX shell also is known as sh

  • Korn Shell also know as sh

  • Bourne Again SHell also knew as bash (most popular)

  1. The C shell: The prompt for this shell is %, and its subcategories are 

  •  C shell also is known as cs
  •  Tops C shell also is known as tcsh

Shell Scripting

Usually shells are interactive that mean, they accept command as input from users and execute them. However some time we want to execute a bunch of commands routinely, so we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh

A shell script have syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. it would be very easy to get started with it.
 

A shell script comprises following elements –

 Shell Keywords – if, else, break etc.

Shell commands – cd, ls, echo, pwd, touch etc.

 Functions

 Control flow – if..then..else, case and shell loops etc.

Why do we need shell scripts

There are many reasons to write shell scripts –

  • To avoid repetitive work and automation

  • System admins use shell scripting for routine backups
  • System monitoring
  • Adding new functionality to the shell etc.

Become master in shell script click here

Advantages of shell scripts

The command and syntax are exactly the same as those directly entered in command line, so programmer do not need to switch to entirely different syntax

 

Writing shell scripts are much quicker

Quick start

Interactive debugging etc.

Disadvantages of shell scripts

Prone to costly errors, a single mistake can change the command which might be harmful

Slow execution speed

Design flaws within the language syntax or implementation

Not well suited for large and complex task

Provide minimal data structure unlike other scripting languages.

Simple demo of shell scripting using Bash Shell

To learn more about Linux click here