Shell Scripting – It is file or program containing the sequence of the commands executing in it.
Shell – It is an command line interpreter which reads the program line by line and converts it into the machine language. So if we talk about linux, the shells like
which are used to read your commands and execute them on the system. So we actually use them as per the requirements. For example “/usr/bash” is for “Linux” and “/bin/sh” is used for “Unix” etc.
Scripting – Scripting is a program containing the multiple number of commands in it. Which is used to complete a task with a single command. You can do repeated tasks in short period of time and avoid running several commands every time.
We actually put all the linux commands in a file and execute it on shell.
Following are the steps to execute any shell script on linux:-
- Shebang Value – This is the very first line to be written in every shell script. It makes the linux kernel to understand that which shell is to be used before running this script. eg. #!/bin/bash or #!/bin/sh . Sometime people write their scripts without shebang value in it but this is not the correct practice. It will work but not effectively like once you encrypt your shell script code and put it in the /usr/bin directory to make it a executable command for linux, then the same script will not work as you wanted. Try it 🙂
- Make file executable – Once we saved the shell script file, just need to make it executable as mentioned below:-
chmod +x filename
chmod 755 filename
- Run as a command – Copy the same file to /usr/bin directory so that you can run it directly as “filename” on linux like as all other commands.
- Other methods – You can also tun the shell script from the same place where it is being created like as “./filename OR sh filename”.
- Debug Mode – Sometime we need to check if the script is showing some errors while running then we can have the below option to run the same script in debug mode.
sh -X filename
Examples of basic scripts:-
Objective: Create a script to see date, calendar and to check the status of the commands.
[chetan.jangir@ip-172-21-0-221 ~]$ vi testscript.sh
[chetan.jangir@ip-172-21-0-221 ~]$ chmod +x testscript.sh
[chetan.jangir@ip-172-21-0-221 ~]$ sh testscript.sh
Mon Jun 20 05:46:47 UTC 2016
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
At the end “0” shows that the above commands did run successfully.
Enjoy Scripting 😉