Introduction to Shell Scripting
A Shell provides you with an interface to the UNIX . It gathers input from you and executes programs supported that input. When a program finishes executing, it displays that program’s output.
Usually Shells scripting is an interaction with system, it accept command from users as input and execute them. However a while we would like to execute a bunch of commands routinely, so we’ve type altogether commands whenever in terminal.
As shell also can take commands as input from file we will write these commands during a file and may execute them in shell to avoid this repetitive work. Files with runs these scripts are generally known as shell programs or scipts. Shell scripts are almost like writing commands in word file. Each shell script is saved with .sh file extension eg. simplescript.sh
Why do we need shell scripts?
There are many reasons to write down shell scripts:
1] To avoid repetitive work and automation
2] Administrator do routine backups using shell scripting
3] System monitoring
4] Adding new functionality to the shell etc.
Types of Shell
There are two main shells in Linux:
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 knew as sh
Bourne Again SHell also knew as bash (most popular)
2. The C shell: The prompt for this shell is %, and its subcategories are:
C shell also is known as csh
Tops C shell also is known as tcsh
How to Write Shell Script in Linux
Let us understand how to write a Shell Script:
- Create a file using a nano editor(or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
3. Write some code. You can add comment using # in the starting of the line.
4. Press Ctrl + o to write the file and then Save the script file as filename.sh
5. Execute the script by typing bash filename.sh
Advantages of shell scripts:
- The command and syntax are precisely the same as those directly entered in instruction , so programmer don’t got to switch to thoroughly different syntax.
- Writing shell scripts are much quicker.
- Quick start.
Disadvantages of shell scripts:
- Slow execution speed.
- Some Design flaws are in the language implementation or syntax.
- Provide minimal arrangement unlike other scripting languages. etc