what is Shell scripting and How it can be used in routine work on Linux System ?

Linux Shell Scripting
Shell : when we say shell or in other words Unix or Linux shell it is nothing but a program that takes input from user and provide it to the kernel which is the inner most part of operating system so basically shell takes input in 2 modes, one is called command mode and the another  is called as Interpreter mode. In first mode shell  take input as  command language  one by one to process it . Shell can also takes commands in a bunch of commands and groups.but in the second mode the user use make too many work to be done and it can be with some conditional checks the user have to write a script which may interpreted by the shell to execute the complete script in a fashion that user is intended to do so. Shell are also available  in Windows system but we do not need to know as a user of windows system but in Linux it is prime requirement of the user to know the basics of shell to accurately process and operate the system easily because many tools in Linux environment are still not available in GUI mode .

History of shell : Journey of Modern UNIX Shell started in 1971 by KEN Thompson (of  Bell Labs) for the UNIX V6 computer OS. although this shell was independent program run out side of UNIX kernel. this separation makes the shell small only 900 lines of C Source code.refer for details link . the redirection and piping syntax that are also available in modern shell are introduced in Thompson shell first time.

The story of bourne shell started in 1977.The shell have two goal to serve  one is to work as command interpreter and execute the commands interactively and the second goal is to run the command in format of some script because script helps in easing the work by introducing the scripts re-usability.

The Bourne shell  started a new era of development of shells as many shell like Korn shell, Almquist shell, and the most popular one the Bourne Again Shell (Bash shell).



Image Source : ibm.com


 
Types of Shell Available : Shells are divided in two types for shake of understanding

1. The Bourne shell
2. The C shell
The Borne shell  have various sub categories like
The Bourne Shell (it is noted as sh)
The POSIX Shell ( it is also noted as sh)
The Bourne again Shell ( it is noted as bash)
The Korn shell  (it is noted as ksh)
The other c shells are Tops c shell and c shell(noted as tcsh and csh) tcsh is root shell of FreeBSD.

As of now we know what are shell and how they evolved and also we find that they are very essential part of UNIX like operating systems like Linux.

we concentrate on Bash shell only in this litrature.

Bash Shell Commands are similar commands of which 15 commands i have written in earlier post you can read there Link

A shell script start with first line  as "#!/bin/bash" this line indicate to shell that this is a shell script so what is this ?
#!/bin/bash is called as shebang and it is ignored by the interpreter. every script has to be start with it.

now we make our first script for bash shell ad execute it.

bash shell script

#!bin/bash
echo hello world  # this will print on the terminal 

we seen what to write but where and how run it ?

so now go to command prompt in linux terminal by pressing Ctrl+Alt+T  for Ubuntu.

now make a new script file by command: sudo nano first.sh press Return on keyboards

it will open a nano text editor where you can type your first script code  as shown above. if you get any error check if nano is installed or not, if not installed install it by sudo apt install nano or sudo apt-get install nano . you may use other text editor for it nano is not a prime requirement.

once you written the script and saved try to run it by writting following command:


$ ./<name of script>.sh 

if it returns a error it means the file is not executable till now.


so now we change the permission of this file as executable by this command
$ chmod +x <name of script>.sh
it will make this script as executable so you now can run the previous command .
Now the Script runs and the output is as: hello world

now we have completed our first script which just print a line given in the script.
lets do some thing more interesting.

Example 1.
make a shell script to make folder in root directory and and create a text file in it with a line of text in that file as" nice to see you scripting"

the script file for first Example:

#!/bin/bash                                                                                             
mkdir bkasyap                                                                                       
cd  bkasyap
touch blog.txt
echo nice to see you scripting > blog.txt 

this script is also available on github https://gist.github.com/BharatVigyan/e1dc67fd49b30950a3a712781867bf49


Video of Tutorial