Shell Scripting | Topic "Scripting Standards"

learning bash shell scripting

In our previous exercise we learn about the basic concepts of Shell & Shell scripting. Now we are going to learn about the basic standards which we should have to follow when creating a shell script. lets see what are the basic standards for creating a shell script.

Naming Convention: When we create a shell script, We have to give it any name in order to recognize our script by its name. Below are the some naming conventions which you should follow while creating a bash shell script.

1) Always create a separate directory for your bash scripts.
2) Make your script names meaningful & clear for other to read it.
3) You have to save your scripts with proper extension using which shell you are creating that script e.g for bash shell save your file with "filename.bash".

File Permissions: Its important to give execution permission to your script before run or execute it.Without execution permission your file can not be run and it is just a like a text file. If you are system admin you have to check that the sensitive scripts don't have the universal execution permissions. Only the authentic users/groups should have the execution permission.

Below are some examples of giving execution permission.

To All :

sudo chmod a+x /path/to/script/file 
sudo chmod 777 /path/to/script/file

To Current User Owner Of File:

sudo chmod u+x /path/to/script/file
sudo chmod 711 /path/to/script/file

To Current Group Owner Of File :

sudo chmod g+x /path/to/script/file
sudo chmod 171 /path/to/script/file

You can use ACL to give specifically execution permission to a user without changing  the user ownership or group ownership.

sudo setfacl -m u:username:x /path/to/file/folder

Scripting Formats: When you create a script, its important to follow the standard scripting formats which makes your script more readable , understandable & clear. A systematically written script is always better when you are going to modify it. Below are some points which you should follow when you write your script.

1) Defining shell: Its important to every shell script using which shell they are running. If its bash then you have to define #!/bin/bash , if it is kornshell then you have to define #!/bin/ksh  in every starting line of your script.

2) Comments : Comments are nothing but the text which explains what about your script is. You have to use # sign before typing your comments.You can add comments for the author of the script , purpose of script , version / modification in a script.

3) Defining Variables : In Linux Shell scripting , Variable are the names which have assigned some values to it. The value can be characters string, numbers, device, file or anything else in Linux. We will cover it later in this course. But it is important in shell scripting that you have to defines your variables with proper & meaning full names that can be understandable for others. Avoid using variables like i,j,k etc.

4) Commands & Statements :  After doing above stuff now you are ready to start your script. All scripts we type in it are Linux commands (ls, echo, fdisk ,etc). Shell scripting supports the programming statements like if-else, while, do-while, for loop etc.

Scripting Sequence: Shell script is work on sequence of commands. If you put three commands first a, Second b, Third c in your shell script. Then they will execute in order one by one a to c.

Below is the example of standard format of shell scripting in bash shell.

learn shell scripting with examples

In next exercise we will create simple shell script & discuss about it.

Sharing is Caring😀

Previous                                                                                                              Next

Post a Comment