Monthly Archives: August 2011

Make Utility In Unix

                  Compiling a program that consists of more than one source
file involves two steps: compiling each module into its respective object
file and then linking the object files into the executable program. Object
files are intermediate code files created by the compiler which by 
themselves are not executable, but when linked together, are. They are 
easily identified by their .o extension.Object files are created with the
-c option on the compiler. For example, the commands

cc -c prog.c
cc -c func.c

will compile the source files prog.c and func.c into the object files 
prog.o and func.o.An additional command to the compiler,

cc prog.o func.o

will compile prog.o and func.o into a working a.out file which you can execute.
The process of putting together .o files is called "linking." These are the 
basic steps for compiling a program with more than one source file.So compiling 
source code files can be very tedious when it contains several source files and
we want to type compile command every time.
               MAKE allows you to automate this by specifying the compilation 
commands in a file which MAKE uses, called the "Makefile".The basic idea 
behind creation of a Makefile is to tell MAKE what you want to compile and how
to compile it.
              A Makefile is a normal text file that may be created with any Unix
editor,such as vi. The usual form of a Makefile entry is:

target: dependency ...
(tab)command
(tab)command
   ....
MAKE is very particular about how targets, dependencies, and commands are 
arranged in Makefile entries. The target should be at the leftmost margin
with a colon immediately after it. There should be at least one space or
tab between the colon and the first dependency. Dependencies should be 
separated by spaces or tabs.The command should be on the next immediate 
line and spaced from the left margin by ONE tab. 
        In general, the Makefile tells MAKE how to do each step of the 
compilation process. For each .c file, MAKE creates a corresponding .o file.
Then when all the .o files have been compiled, MAKE links them together into
an executable.
         Let us continue with an example. We have two source files, prog.c and 
func.c.The prog.c file has an #include "head.h" statement, the func.c file has
no includes,and the executable program is to be named "myprog". One way to 
construct a Makefile would be as follows:

myprog: prog.o func.o
	 cc prog.o func.o -o myprog
prog.o: prog.c head.h
	 cc -c prog.c
func.o: func.c
	 cc -c func.c

This Makefile says that myprog is dependent on prog.o and func.o. That is,
prog.o and func.o are required to exist and be up-to-date in order to put 
together the program myprog. Likewise, prog.o is dependent on prog.c and 
head.h. Finally func.o is dependent on func.c.We will now use MAKE to 
create prog.o, func.o, and myprog with the command:

	% make myprog

          MAKE looks for a file specifically called "Makefile". If it does 
not find one,MAKE will not work.MAKE starts at the first target, "myprog".
It notes that there is a dependency on prog.o and so proceeds to the target
"prog.o". Now MAKE sees that prog.o in turn is dependent upon prog.c and 
head.h. MAKE then checks to see if prog.c is current. Since there is no 
prog.c target in the Makefile and prog.c exists,MAKE is satisfied that prog.c
is up-to-date. Likewise for head.h. MAKE returns to the business of assuring
that prog.o is up-to-date. Since prog.o does not exist,MAKE invokes the 
command "cc -c prog.c" to make a current prog.o. MAKE returns to "myprog" 
and continues to the next dependency, func.o. MAKE goes through a sequence
of steps to create func.o similar to those described for prog.o. Returning
to "myprog", MAKE sees that there are no more dependencies to check. Since
"myprog" does not exist, MAKE invokes the command "cc prog.o func.o -o myprog"
and the job is done.

An Introduction To AWK

              The awk language is a small, C-style language designed 
for the processing of regularly formatted text. This usually includes
database dumps and system log files.The AWK utility is a data extraction
and reporting tool that uses a data-driven scripting language consisting
of a set of actions to be taken against textual data either in files or 
data streams for the purpose of producing formatted reports.Awk's funny 
name comes from the names of its original authors, Alfred V. Aho, 

Brian W. Kernighan and Peter J. Weinberger.

Basic structure
                 AWK is a language for processing files of text. A file is
treated as a sequence of records, and by default each line is a record. Each 
line is broken up into a sequence of fields, so we can think of the first word
in a line as the first field, the second word as the second field, and so on.

               An AWK program is of a sequence of pattern-action statements.
AWK reads the input a line at a time. A line is scanned for each pattern in 
the program, and for each pattern that matches, the associated action is 
executed.

An AWK program is a series of pattern action pairs.

condition {action}

Here the condition is typically an expression and action is a series of 
commands. The input is split into records, where by default records are 
separated by newline characters so that the input is split into lines. 
The program tests each record  against each of the conditions in turn, and 
executes the action for each expression that is true. Either the condition
or the action may be omitted. The condition defaults to matching every record.
The default action is to print the record.

           Another important pattern is specified by the keywords "BEGIN" and
"END" .These two words specify actions to be taken before any lines are read, 
and after the last line is read. The AWK program below:

BEGIN { print "START" }
      { print         }
END   { print "STOP"  }

This will adds one line before and one line after the input file.
             The general form of the awk command is 
awk <pattern> '{print <stuff>}' <file>

In this case, stuff is going to be some combination of text, special 
variables that represent each word in the input line, and perhaps a 
mathematical operator or two. As awk processes each line of the input
file, each word on the line is assigned to variables named $1 (the first
word), $2 (the second word), and so on.The variable $0 contains the 
entire line.
     Let's start with a file, words.data, that contains these lines:

nail hammer wood
pedal foot car
clown pie circus

Now we'll use the print function in awk like this:

awk '{print "Hit the",$1,"with your",$2}' words.data
Hit the nail with your hammer
Hit the pedal with your foot
Hit the clown with your pie

We can also put some numeric data in the input file as in the grades.data
file:

Rogers 87 100 95
Lambchop 66 89 76
Barney 12 36 27

Then we can perform some calculations like this:

awk '{print "Avg for",$1,"is",($2+$3+$4)/3}' grades.data
Avg for Rogers is 94
Avg for Lambchop is 77
Avg for Barney is 25
Also if we want to exclude lines from being processed, we can enter 
something like this:

awk /^clown/'{print "See the",$1,"at the",$3}' words.data
See the clown at the circus

Here, we told awk to consider only the input lines that start with clown.
Note that there is no space between the pattern and the print specifier.

Starting With Shell Script

What is a shell script?

A shell script is a command which would have been directly typed into a shell.

It can be a sequence of commands that you type regularly.By putting them into

a script we can reduce them to a single command.Also there is no difference in

syntax between interactive command line use and placing the commands in a

file.But there are some commands line history which we can only type in

interactive command line.

Why use shell scripts?

Shell scripts are used to combine lengthy and repetitive sequence of commands

into a single and simple command.We can create new commands using combinations

of utilities.It can also be used to generalize operations on a single set of data, into a

procedure that can be applied to any similar set of data.

Writing the script

First use one of the text editors like vi and write this inside it:

#!/bin/sh

clear

echo “Hello world”

and save the file using .sh extension,say hello.sh.

Making it executable

To do execute,tell linux that this is an executable file.

chmod +x hello.sh

Running the script

To run the script just go to the directory where the file is saved and type the following

command:

./hello.sh

The output will be: