In the spirit of this thread, I thought it might be nice to take a simple script and build on it. This will allow us to learn as we go.
For the first example I've taken a script that I wrote a few years ago that searches for a text string, inserted some comments on what's going on in the program. This is a VERY simple start. From there I'll be adding features to "build" it to something a bit more complex and making it more usable.
This example is find-string.sh. As is, it searches ALL files under the current directory for a specified "string" of text. This is the the most basic start so, you more experienced scripters don't be put off by it's simplicity, we'll build it as we go.
A little background. This is my first opportunity to use a debian based distro. I cut my teeth on IBM's AIX operating system. I've since administered Sun Solaris, Silicon Grahics IRIX, SCO Unix, Red Hat and other RPM based Linux distros. I gotta say, this distro rocks!!! Since my start was with AIX (back in the '80s), I use the korn shell (ksh) almost exclusively. There are subtle differences in the function output, amongst other idiosyncrasies that over the years I've become accustomed to. For this example I'll be using bash.
I'll try to put more than enough comments in the code to explain what's going on. I don't mean to insult any ones intelligence, just trying to make it easy for the beginners to catch on to. That being said, I'll try to break down each statement as we go. This will probably make for more comments than code, but what the hay!
P.S. I don't get as much time to peruse the site as I'd like, so understand if it takes me a day or two to respond.
Here's the code;
Code: Select all
#! /bin/bash
# ^ the shebang. What this really does is spawn a new instance of the bash
# shell to run the program in. Otherwise, the current instance (the window
# you're running it in) is susceptible to any problems that may occur. This
# can be very important on console based systems. Unlike DOS a window,
# output from a program can be interpreted and run. This could cause some
# undesirable issues. The shebang is not absolutely 100% necessary, simple
# scripts will work fine without it. However the more intense the program the
# greater the need for it. It can NEVER hurt to have it.
###########################################################################
# MPH (2010/11/19)
# find-string.sh
###########################################################################
# Purpose: Search all files under the current directory for a string of
# text.
# Note: This is a very basic program on which we'll build. Future
# enhancements will include error checking, argument handleing, file
# specification, search for whole words or partial and more... As I
# think of them ;) Suggestions are always welcome.
###########################################################################
# Statements:
# find: The "./" tells it to use the current directory. The "-type f" tells
# it to only search for files.
#
# The "|" (pipe sign) tells it to pipe the output through the following command.
#
# while: One could use a "for" statement here, but if there are any spaces
# in the file names, you may encounter unexpected results. Without getting
# into IFS (Input Field Separator) etc... The while statement works best in
# this case. Also note that the variables are double quoted "" It is almost
# always a good practice to do this.
find ./ -type f | while read file
do
# i= sets the value of "i" to the output of the command.
# grep: -i tells grep to be case insensitive. Note the `` (back ticks) the
# command line is encased in, run the command and return it's value.
# 2> /dev/null outputs the stderr stream to the bit bucket.
# That being said. 1> = stdio (standard I/O) 2> = stderr (Standard Error)
i=`grep -i "$1" "$file" 2> /dev/null`
# if: For one reason or another I find that negating the -z will help reduce
# false positives.
if [ ! -z "$i" ]
then
echo "$file:"
echo "$i"
echo
fi
done
Sample output:
Code: Select all
./find-string.sh grep
./find-string.sh:
# grep: -i tell grep to be case insensitive. 2> /dev/null outputs the stderr
i=`grep -i "$1" "$file" 2> /dev/null`