The collection of Linux commands in an executable file is called Shell Script and we can run simultaneously commands with the help of shell script at the same time without type manually. shell file is always .sh extension. Shell Scripting introduce by STEPHEN R BOURNE in the 1970s at AT&T Bell labs. script file is always executed by the command order listed in a file.
Types of Shells
There are two types of shell available in Linux sh shell and a c shell
- Bourne Shell
- Kron Shell
- Bourne again Shell
- C shell
- Tenex c shell
Nowadays Bourne Again Shell (bash) is a default. To check what is your default shell open your terminal and type a command echo $0 and hit enter you can see your output is bash.
Like you see in the image I echo that command and my default shell is bash and that dollar sign is my prompt, in sh type os shell dollar sign is default prompt
C shell and this type of shell-like Tenex c shell are very similar to c programming language it is used, very similar to c type of syntax. this type of shell has % character is a default prompt.
But in this tutorial series, we only use the bash shell because now in Linux bash shell is very popular so we do focus only on this shell.
Benefits Of Scripting
Execute multiple commands at the same time without typing that commands manually. Just create that script file and write all commands in that file and now you run that file to execute those multiple commands.
Automate daily you can do your daily common task automate like daily backup process, delete some old log file to free up space, etc.
one of the script I daily use to ease my work that I have almost 30 to 40 servers and I have to ping all that servers to check that all servers are up and running.
it is very painful to check that server to run ping command for each server with its IP so daily I am running ping command 40 times manually, to ease my work I create my .sh script to ping all the servers now I just execute that script and ping my all servers.
Create A Example Script
For Instance script, I create that ping script to ping all servers, Lets Begin
First, create a file with .sh extension give any name that file but after name gives extension sh. create file with touch command or any file editor like nano or vim
this command creates a blank file whose name is ping and file extension is sh.now open that file with your favorite text editor I mostly use nano for that because that is user-friendly to use.
when you run nano ping.sh your empty file open with a nano Text editor. now write your Shell Script But the key thing to remember every shell script starts with #! sign and give your shell path.
To check your shell path run command which and your shell name which bash the output of your is /bin/bash your bash is in /bin directory. the beginning line of your script is #!/bin/bash.
after that enter your ping command with your server IP.
in the upper image give each server IP in a single row. you can type as much as IP you want to ping. after entered IP hit ctrl + o to save and then nano will ask you to save to enter then press enter and to exit from editor press ctrl + x now your file script file is saved.
Now before you start using it your file is not ready to execute if you did not read my previous blog about file permission so please read. read click here, you have to give execute permission to file to run that file as a program.
give execute permission run chmod u+x ping.sh after that your file is ready to use.
run an executable file there are two ways first is type your shell name and your filename. another way is before your file name add period and forward-slash (./)(filename). now your file is ready to run just run your file and check.
Tips to remember
- Always give a sh extension to file.
- Every script code start with #! and your shell path.
- Give comments in your code to understand the code that is going with that code.
- Every line that starts with the hash sign(#) is a comment line that line is not executed.
- like another programming language, you don’t need to compile your shell script code to run.
- shell script is interpreted.
Subsequent parts of this tutorial will cover Linux Shell Scripting in detail.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.omment me if you have some questions