Anatomy of a Script

by mike on November 3, 2010

Scripts have a number of features that are basic to having them work correctly and enjoying the process.

Header
The header on a script will provide information about, the license  what the script is used for and who wrote the script.  I can contain any information which may be helpful in the future. The header must contain a declaration of what shell will be used, “#!/bin/bash” as an example of the bash shell.  If no declaration is made, the default shell is used.

#!/bin/bash
# This script comes with no warranty …use at own risk
# Copyright (C) 2009  Joe Smith
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA
#
# This script provides a daily backup using rsync

Note: In the interest of space, the majority of the headers in this manual will be eliminated in the examples.

Comments
Scripts are often filled with comments.  Comments provide you information about the script and how it functions which will be helpful for you down the road.  Comments begin with a “#” sign.

# This is a comment
cd /var # This is a comment
#        This is a comment

The key to each of the above comments is that the comment is preceded with a “#”, even if there is space.

Building Scripts
One of the excellent features about scripts is that you can continue to build on a basic script as your skills are enhanced and you have need.  Here you can see multiple, simple, lines have been added to the first script.

#!/bin/sh

echo “Date:”
date
cd /etc
echo “################################”
echo “All Configuration Files”
ls *.conf
echo “################################”
echo “Files Changed in Last 3 days”
find /etc -iname ‘*.conf’ -mtime -3
echo “################################”
echo “Server Running Time:”
uptime
echo “################################”
echo “Users Currently Logged In:”
who
echo “################################”

exit

Cleanup
The  end of the script provides a way to clean up the temporary files and return an exit code for the script.  Often times  “exit 0” is used to communicate to the script that the script has completed successfully.

exit 0

Executing a Script
When you save a script it usually will be saved with a .sh extension so the shell knows that it is a script and it must be make executable by doing a chmod 755 script name.

chmod 755 basic.sh

Interested in a self-directed Bash Shell course with a 500 page manual, 125 videos and VMWare images to practice with?  Check out our online Bash Shell Course.

Comments on this entry are closed.

Previous post:

Next post: