Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Shell
3.
Unix architecture
4.
Shell Scripting:
4.1.
Variables in Linux
4.2.
Writing Shell Script
4.3.
Running Shell Script
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Jun 30, 2023

Introduction to Linux Shell and Shell Scripting

Introduction

The shell program is the interpreter through which we communicate with the operating system. At the same time, the Shell script is the user interface to the computer system for the Linux operating system. Shell scripts are Linux commands strung together and executed as individual files.

Types of Shell

Name Path FreeBSD 8.0 Linux 3.2.0 Mac OS X 10.6.8 Solaris 10
Bourne Shell /bin/sh     Copy of bash  
Bourne-again Shell /bin/bash optional      
C shell /bin/csh Link to tcsh optional Link to tcsh  
Korn shell /bin/ksh optional optional    
TENEX C Shell /bin/tcsh   optional    

The system knows which type of shell to run for us according to the password file's final field in our data.

There are mainly five types of shell:
 

  1. The Bourne shell: It was Introduced by Steve Bourne at Bell Labs, and it has been in use since Version 7. It is provided with almost every UNIX system in existence. The GNU/ Linux version of the Bourne (sh) shell is GNU Bourne again shell (bash). 
  2. The Bourne-again shell: It is the GNU shell attached to all Linux system environments. It was introduced to be POSIX conformant while maintaining the Bourne shell. It contains all the features from the C shell and the Korn shell.
  3. The C shell: It was discovered by Bill Joy at Berkeley and is introduced with all the BSD features. Additionally, the C shell was attached with System V/386 Release 3.2 and was also included in System V Release 4 (SVR4).
  4. The Korn shell: It is considered an inheritor to the Bourne shell and was first discovered with SVR4. The Korn shell runs on most UNIX systems. It is upward-suited with the Bourne shell and has those features that made the C shell popular: job control, command-line editing, and so on.
  5. The TENEX C: It shell is an improved version of the C shell that borrows several characteristics, such as command completion, from the TENEX operating system (developed in 1972 at Bolt Beranek and Newman). It enhances the C shell by adding many features into it, and hence it is mostly used as an alternative for the C shell.  

Unix architecture

  • The software that controls the computer's hardware resources and provides an environment under which programs can run is Kernel.
  • The interface to the Kernel is the protection of software called the system calls.
  • The shell is a unique application that gives a prompt for running different applications.
  • In the long run, an operating system consists of the Kernel and other software that makes a helpful computer and gives it its personality.
  • For example, Linux is the Kernel used by the GNU operating system. Generally, It's referred to this combination as the GNU/Linux operating system, but it is more commonly referred to as simply Linux.

Shell Scripting:

The shell script is like the batch file in MS-DOS but has more power than the MS-DOS batch file. A shell script can take input from the user file and output it to the screen. It is Useful to create commands that can save us lots of time and automate some tasks.

Variables in Linux

Sometimes, variables must be kept in a computer's RAM to process our data. RAM is divided into remote locations, and each area has a distinct value called memory address, and it is used to store the data. Programmers can give a specific name to this memory location/address called memory variable.

In Linux, the Variables are of mainly two types:

  1. System variables: This type of variable is defined in CAPITAL LETTERS.
  2. User-defined variables: Created and maintained by the User. This type of variable is defined in lowercase LETTERS.

Examples of System Variables

System Variable Meaning
BASH=/bin/bash Our shell name
BASH_VERSION=1.14.7(1) Our shell version name
COLUMNS=80 No. of columns for our screen
Home=/home/shivani Our home directory
Lines=25 No. of columns for our screen
LOGNAME=students Our logging name
OSTYPE=Linux Our o/s type
PATH=/usr/bin:/sbn:/bin:/usr/sbin Our path settings
PS1=[\u@\h\W]\$ Our current working directory
USERNAME=shivani User name who is currently logging into the PC

Some of the above configurations can be different on your device. You can use any of the above to print variables contained as follows.

Defining User-defined variables

variable name=value 

NOTE: Here' value' is assigned to given 'variable name' and value has to be on right side = sign 

For Example:

$ no=10 # this is ok
$ 10=no # Error, NOT Ok, Value must be on the right side of = sign.
To define a variable called 'vech' having value Book
$ vech=Book
To define a variable called n having value 15
$ n=15

Writing Shell Script

We will display our first script as "Each word has its value" on screen. Write a shell script you can use in Linux's text editor such as vi or credit, or even use the cat command. Here we are using the "cat" command. You may use any of the above text editors—first type following cat command and rest of text as it is.

$ cat > first
#
# My first shell script
#
clear 
echo "Each word has its value." 

Press Ctrl + D to save. Now our script is ready. To execute it, type the command

$ ./first

The first screen will be clear, and then "Each word has its value." is printed on the screen. To print a message of variables containing we user echo command, the general form of echo command is as follows

echo "Message"

echo "Message variable1, variable2....variable N"

Running Shell Script

Because of the security of files, in Linux, the creator of Shell Script does not get execution permission by default. So if we wish to run a shell script, we have to do two things as follows

  1. Use the chmod command as follows to give execution permission to our script.

Syntax: chmod +x  shell-script-name 

OR Syntax: chmod 777 shell-script-name

  1. Run our script as

Syntax: ./your-shell-program-name

For e.g.

$ ./first
Here '.' (dot) is command and used in conjunction with a shell script. The dot(.) indicates to the current shell that the command following the dot(.) must be performed in the same shell, i.e., without loading another shell in memory. you can also try the following syntax to run Shell Script 
the following syntax to run Shell Script S
OR /bin/sh &nbsh;&nbsh; your-shell-program-name
E.g.,
$ bash first
$ /bin/sh first

 

Note that to run the script, you need to have it in the same directory where you created your script.

Frequently Asked Questions

Q1. What is shell scripting?

A shell scripting is a text file that contains a sequence of commands for a UNIX-based operating system. In Linux, shells like bash and Korn support programming constructs saved as scripts. These scripts become shell commands, and hence many Linux commands are scripts.

Q2. What are the applications of shell scripting?

Shell scripting is easy and effective. It allows us to program commands in chains and have the system execute them as a scripted event, just like batch files. It creates customized datasets on the fly and wraps programs over which you have no control inside an environment that you can control.

Q3.What is Input/Output Redirection?

When you execute a command, the command is read from the keyboard (the standard input), and the result of the command(output) appears on the screen (the standard output). If you want the command output to appear in a different place, or if you want to send the output to another command for processing, then we need to change the output with a process called Input/output redirection.

Key Takeaways

We learned about shell programming and its different types in brief. We understood the basics of Unix architecture.e, Kernel, system calls, and library routine. We understood the concept of Linux variables and their types, i.e., user-defined variables and system variables. We also learned how to write and run Shell Scripts in Linux Environment with examples.

Visit here to learn more about different topics related to database and management systems. Ninjas, don't stop here; check out the Top 100 SQL Problems to master frequently asked questions in big companies and land your dream job. Also, try  Coding Ninjas Studio to practice a wide range of D.S.A. questions asked in many interviews.

Live masterclass