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 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.
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 exit's else echo check your ls commandfi
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 exit's 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.The following two tabs change content below.Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix- June 29, 2020
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018