• RSS
  • SmsJobs
  • Sms
  • GoogleBuzz
  • Facebook
  • Twitter
  • Linkedin
  • Youtube
The Linux Juggernaut

The Linux Juggernaut

roles on……

Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.
shell_scripting_debug

How to write a shell script professionally and why to write it?

Most of the time when we work and write shell script we write on the go not considering others who want to understand it. Suppose you write a shell script and did not given any info what that shell script do, all your efforts will be in vain if a new system admin is unable to understand it. We have to make a habit to give as much as info in the script what actually script is how to run it etc. Let’s see how to write a script in professional manner.

Always make sure that to write some Meta data which give info about the script at the start of the script.

The Meta data should contains below details 5 lines, if you want you can add more depending on your requirement.

Line1: Shebang statement(#!/bin/bash)

Line2: Script purpose:

Line3: Author details

Line4: Creation date

Line5: Modification date and details

Here is the Meta data example for your reference.

#!/bin/bash

#Purpose: To automate user creation

#Author: Surendra Kumar Anne

#Date/Time: 06-08-2011.19:10

#Modified date: 2011-00-00: Reason for modification.

Tip1: Once the Meta data is completed try to give as much info as possible if there is a complex command which you written.

Tip2: When writing shell scripts try to use vim editor instead of just normal vi editor. Vim editor uses color codes for different commands and control statements. See below example

Tip3: Try to check if all the open brackets are closed or not before executing it.

Tip4: Try to use $? Inbuilt variable to check the previous command status.

TIP5: Try to use tab’s for better writing of the script to make others to understand quickly..

Let’s see this with an example.

Script:

No format and just vi editor..

ls –l 

a=`echo $?`

if [ $a –eq 0 ]

then 

echo ls executed successfully.

elif [ $a –eq 1 ]

echo ls command does not exits

else 

echo check your ls command
fi

With systematic format and using vim editor..

ls –l 

a=`echo $?`

if [ $a –eq 0 ]

 then   echo ls executed successfully.

 elif [ $a –eq 1 ]

 echo ls command does not exits

 else 

 echo check your ls command

fi

How are second script and first script? Both are same but understanding second one is very easy and easy to find If there is any syntax error when compared to first one.

Here is one example to show you how to write a script.

#!/bin/bash

 #Author: Surendra Kumar Anne

 #Purpose: To automate user creation

 #Date/Time:06-08-2011.19:10

mkdir -p /home/admin/useraccounts

for (( i=0; i<=500; i++ ))

 do

#users are added with their prefix as baci, so users will be baci1, baci2, baci3 etc..

 useradd baci$i

#The below command is used to generate a 8 character length password string

 < /dev/urandom tr -dc A-Na-n1-9_ | head -c8 > /tmp/passwd.txt

#the below command will set password to user by taking input from /tmp/passwd.txt

 cat /tmp/passwd.txt | passwd --stdin user$i

 echo -e "Username:baci$i" > /home/admin/useraccounts/baci$i

 echo -e "password:" >> /home/admin/useraccounts/baci$i

 cat /tmp/passwd.txt >> /home/admin/useraccounts/baci$i

done

rm -rf /tmp/passwd.txt

Please feel free to comment on this post if you know a more better techniques to write a shell script.

Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.
bySurendra Anne On Sunday-August - 14 - 2011

email newsletter

Redhat Flagship dist

Redhat announced that they are going to release RHEL7 by ...

12 examples to use f

Flower brackets examples This is a small post on how to ...

7 Linux read command

read command examples read command is useful in scripts when reading ...

7 linux sort command

Linux/Unix sort command examples This is a small tutorial on how ...

How to find command

How to find command location in Linux and Unix This is ...

  • About Linux Juggernaut

    How the blog name selected? Juggernaut: * a massive inexorable force that seems to crush everything in its way ...readmore>>

  • Contact Me

    Contact us Send an Email Phone number and vCard LinkedIn profile Advertise with us
  • About Me

    Surendra Kumar Chowdary

    My name is Surendra Kumar Anne. I am from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present i work at Persistent Systems as Module Lead.   read more »