Published January 14, 2022 by

Introduction to shell programming and shell script features


Introduction:

What is a Shell

A shell is an interpreter in a UNIX-like Operating system. It takes commands typed by the User and calls the operating system to run those commands.

Shell is a program constructed for shell commands.

Shell is just like BAT files In MS-DOS.

Shell Is an environment for user interaction, but not part of the kernel.

The shell gets started when the user logs in or starts the terminal.

For example cp, PWD , ls, mkdir …….

As shells are interface for users to interact with the kernel only through commands…As the following image depicts…

undefined
Unix/Linux Architecture

Unix/Linux Advantages

Two categories of using commands in shell Unix/Linux

  • Interactive mode
  • Non- Interactive mode

Interactive mode:

Interactive means, they accept commands as input from users and execute them. However time we want to execute a bunch of commands routinely, so we have to type in all commands each time in the terminal.

It takes single line command from the user and executes that command


The user types a single command and the result is printed out.


What is SHELL PROMPT

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press enter and execute.

Non-Interactive mode ( Shell script)

  • A script is defined as just a plain text file or ASCII file
    • With a set of UNIX commands
    • Flow of controls
    • I/O facility
  • Each shell script is saved with .sh file extension eg. myscript.sh
  • A shell script can be created using any text editors like vim , notepad++ , emac ….etc

By default save the files in .sh extension (bash file)

Shell script performs it takes a bunch of commands and executes in a sequential manner in the file where the commands are written,we can execute that file to execute that bunch of commands with the file name and extension (eg: sh myfile.sh).

A shell script has 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 the 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

Types of shells:

Below are lists of shells available in UNIX/Linux are

  1. The Bourne Shell

The Original Unix Shell is known as sh, short for shell or the Bourne shell, named for steven Bourne, the creator of sh. This is available on almost all UNIX-like operating systems. The Basic bourne shell supports only the most limited command line editing, You can type the Characters, remove characters one at a time with the Backspace key and Press enter to execute the command. If the command line gets messed up , you can press Ctrl-C to cancel the whole command.

  1. The C Shell

It is designed by Bill Joy at the university of California at Berkeley, the C shell was so named because much of its syntax parallels that of the C programming language. This shell adds some neat features to the Bourne shell, especially the ability to recall previous commands to help create future commands. Because it is very likely you will need to execute more than one command to perform a particular task, this C shell capability is very useful.

  1. The Korn Shell

It is created by David Korn at AT&T Bell laboratories , the korn shell or ksh offers the same kind of enhancements offers by the C Shell , with one important difference: The korn shell is backward compatible with the older Bourne shell Synatx. In UNIX like AIX & HP-UX korn shell is the default shell.

  1. Bash ( The Bourne Again Shell)

Bash offers command-line editing like the korn shell,file name completion like the C shell and a lot of other advance features. Many Users view bash as having the best of the Korn and C shells in one shell. In Linux and Mac OS X system , bash is the default shell.

  1. tcsh ( The T C Shell)

Linux systems popularized the T C shell ot Tcsh. Tcsh extends the traditional csh to add command line editing,file name completion and more. For example , tcsh will complete the file and directory names when you press Tab key(the same key used in bash). The older C shell did not support this feature.

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..

Advantages of shell script

  • 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. etc

Shell script features

  • Shells are CASE SENSITIVE.
  • Shells allow interactions with kernel.
  • It may be used interactively or non-interactively.
  • Provide help for each and every command using MAN or help.
  • Provides flow control constructs,quotation facilities, loops , and functions.
  • It supports input – output redirection and pipelines.
  • Provides set of built in commands.
  • It allows file and directory management features.
  • They do not require compilation before execution.
  • permission to the file systems.
  • It allwos use of variables.
  • Contains provisions for pattern matching

Thank you!