What is Shell Scripting?
Before understanding the Shell Scripting, we first understand the components of an operating system. An Operating is made of some of the components, but the two main components are:
As shown in the below figure of the shell architecture the operating system is having two components that are kernel and shell.
A Kernel is an intermediate between operating system and hardware, so it makes the communication between the hardware and software possible. As we see the figure Kernel is the innermost part of an operating system and shell is the outermost part of an operating system.
A shell is accessible by the terminal which runs a command. A Shell is an intermediate between the operating system and terminal, so takes input from the terminal and run on an operating system that makes to take input from the user in the form of commands by using command prompt, which is having the symbol $, processes it, and then an output display on a terminal.
It is an operating system’s command line interpreter program that is designed to execute or run a set of user-provided commands.
Types of Shell in Linux
There are two types which are as follows
1. Bourne shell
The Bourne Shell known as sh shell, prompt for this shell is $. The derivatives of Bourne Shell are POSIX shell and Korn shell which are also known as sh and the Bourne Again SHell know as bash shell which is mostly used.
4.5 (928 ratings)
2. C shell
The C shell is known as csh shell, the prompt for this shell is %. The derivatives of C Shell is Tops C shell that is known as tcsh.
As the shell script is like any other programing language, so it is also having the syntax just like any other programming language. If we have any experience with any programming language like C, C++, Java, Python and so on. Then it would be very easy to lean and write shell script program. The shell script program can comprises keywords like if, while, break, else and so on, the shell commands like ls, cd, echo, move, pwd and so on, the functions which is performed the specific task and constructs or control flow like if .. then .. else, while loop, case and so on.
Why do we need Shell Scripts?
There are some of the reason where we need to write shell scripts to avoid repetitive work and automation, to perform the routine backups by system admins using shell scripting, too Adding new functionality to the shell, to perform System monitoring and so on.
Advantages of Shell Script
The programmer no need to switch to entirely different syntax as the command and syntax are exactly the same as those directly entered in the command line can quickly write and start the shell scripts, interactive debugging and so on.
Disadvantages of Shell Script
The disadvantages of shell scripts are like slow execution, minimal data structure unlike other scripting languages, little difficult to remember all the commands, the complex flaws design
Working with Shell Scripting
Shell Scripting program contains a series of command for the shell to execute.
Steps to create Shell Script
- Create a shell script a file using a vi editor or any other editor. Give the file name with extension .sh
- Start the script with #! /bin/sh
- Write the required code and save file.
- execute the script file by type bash file.sh
How to Write the Shell Script Program
Here we will discuss some of its programming components like variables, control structures and so on.
The “#!” is an operator which directs the script to the interpreter location called shebang. So, #! /bin/sh mean the script gets directed to the Bourne shell for execution.
Let’s see the script example where it using ls command the ls command is used to get the list of files and directory in the current directory. Let the code is written in a file.sh file as:
The ls command is executed when we execute the script file.sh file.
The commenting is a non-executable code which is important in any program. In Shell programming, the # symbol is used to add the comment. Let see example
#comment line is here
The Shell Variables, the variables are used to store data or constant value which can be of characters and numbers data types. Let see the example for the variable, the variable as using the name variable to store “John” name and then printing the name variable value. The variable value can be display by prefixing with the $. The echo command is used to display a line of text/string on standard output or a file.
The read command reads a line of text from standard input. The example of reading command is given below suppose is written in the file.sh file.
echo "what is your name?"
echo "Hello, $name"
Then the file.sh run as:
What is your name?
These are some of the simple sample scripts. Farther can develop advanced scripts which contain conditional statements, loops, and functions.
Who is the right audience for learning Shell Scripting technologies?
The target audience is Linux users and professionals who want to automate tasks on Linux using any of the shell scripts and other audience could be who want to learn a simple programing language.
It is very useful in various organizations to write workflows for ETL, epsilon and various other tools to save time. If you are in a company then the pay scale also increases as you become good at scripting.
- A Kernel is an intermediate between operating system and hardware.
- A Shell is an intermediate between the operating system and terminal, so takes input from the terminal and run on an operating system.
- The Bourne shell and the C shell are the most used shells in Linux.
- Shell scripting program contains a series of command for the shell to execute.
- It is used to store data.
- It can help you to create complex programs containing conditional statements, loops, and functions.
- The target audience for learning Shell Scripting technologies are Linux users and professionals who want to automate tasks on Linux using any of the shell scripts.
- The scope of the shell scripts as the Shell scripts are very useful in the various organization to write workflows for ETL, epsilon and various other tools to save time.
This has been a guide to What is Shell scripting. Here we discussed the working, types, scope, and advantages of Shell scripting. You can also go through our other suggested articles to learn more –