Shell Scripting in a Stupid way – Understanding Shell Script Architecture

Shell scripting is a boring topic for those who want to be perfect in every part of it from the day one. Just not to make you feel bored, I just want to keep this shell scripting series in simple way ( …. full time programmers say it is stupid way :) ) .

In this post we will talk about the architecture of the Shell script and some important guidelines that helps us to write awesome shell scripts.
shellscript

 

Now Let’s Create a Sample Script following the above structure

 

Purpose of the Script :  We want to execute a file level operation ( e.g.  changing permission, changing owner, creating a backup file )  on all the files in current directory.

We can do this task, directly from the  command  line without any script  by using below command

#  find * -type f -print |xargs  “command-to-run”

But in this article we will do this task using script, as below

#  ./scriptname  command-to-run

 

Guideline 1:   Chose a valid script name . In our case, we have chosen our script name as  file_op.sh,  because this script performing file level operations. And the extension “.sh” gives us a hint that we are going to write this script using borne shell ( i.e sh) script.

 

Guideline 2:  What shell we are using. In our case , we want to use borne shell so our first line of the script will be

#!/bin/sh

Guideline 3: Define Header of the Script, to describe the purpose of the script. In our case, our header will have following text.  Please note, all the lines in the header should start with “#” so that they will be treated as comments while script executions

############################################################

# Title    :    file_op.sh   :  perform file level operations on files of current directory

# Author : Ramkumar Ramadevu

# Date  :  12 – Jan – 2014         –  Version      1. 0

############################################################

Guideline 4 : Is your script required any variables.In our case , we want to define a variable  that will store the script name as it’s value.

SN=`basename “$0″` 

Guideline 5 :  Is your script required input arguments, if so validate them. If the script missing any required arguments, display the script usage statement and exit from the script.

In our case we need a “command-to-run” as input argument. If that argument is missing we should exit from the script with an exit statement. There are several ways to do this checking, and in this script we will talk about beginners way to do this. And that code appears like this.

if  [ $# -lt 1 ]

then

echo >&2  “usage: $SN command [arg  …] “

exit 1        #  this will exit from the script with a return value 1

fi

 

Guideline 6: Split the code into multiple functional blocks. In our case we have to run only one command so we are just using the below code directly in the main script without making any sub sections of the scripts.

find * -type f -print | xargs “$@”

 

Finally the Overall script  –  file_op.sh  –   looks like this

#!/bin/sh

############################################################

# Title    :    file_op.sh   :  perform file level operations on files of current directory

# Author : Ramkumar Ramadevu

# Date  :  12 – Jan – 2014         –  Version      1. 0

############################################################

SN=`basename “$0″`

if  [ $# -lt 1 ]

then

echo >&2  “usage: $SN command [arg  …] ”

exit 1        #  this will exit from the script with a return value 1

fi

find  *  -type  f -print | xargs “$@”

Watch the Demo

 

 

 

Ok, now do it yourself.    

 

terminal-first-time

 Launch Linux Terminal

Ramdev

Ramdev

I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

4 Responses

  1. VIPIN KUMAR says:

    Good post: but I have a question for you..

    let say we have a file like file.txt
    vipin kumar vipin
    vipin kumar
    vipin
    vipin vipin
    kumar kumar

    So, my question is how to i count the number of vipin in the file using sed (only sed), or how to count the replaced word in a file..
    Thanks in advanced, please reply me on this @ vipinkumarr89@hotmail.com

  1. September 15, 2015

    […] Shell Scripting in a Stupid way – Understanding Shell Script Architecture […]

  2. September 17, 2015

    […] Shell Scripting in a Stupid way – Understanding Shell Script Architecture […]

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us