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.
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:
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).
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.
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).
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.
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 to it, and hence it is mostly used as an alternative for the C shell.
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 eacharea has a distinct value called memory address, it is used to store the data. Programmers can give a specific name to this memory location/address called a memory variable.
In Linux, the Variables are of mainly two types:
System variables: This type of variable is defined in CAPITAL LETTERS.
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.
$ echo $USERNAME
$ echo $HOME
Defining User-defined variables
variable name=value
NOTE: Here' value' is assigned to the given 'variable name' and the value has to be on the 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
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
2. 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.
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.
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.
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.
Conclusion
We learned about shell programming and its different types in brief. We understood the basics of Unix architecture, 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.