Thoughts on Programming

August 8, 2011

Unix “make” command

Filed under: Linux — shadiyya @ 6:27 pm

This post is a short introduction to the Unix make utility.The make utility is a software engineering tool for managing and maintaining computer programs. Make provides most help when the program consists of many component files. As the number of files in the program increases so to does the compile time, complexity of compilation command and the likelihood of human error when entering command lines, i.e. typos and missing file names.

By creating a descriptor file containing dependency rules, macros and suffix rules, you can instruct make to automatically rebuild your program whenever one of the program’s component files is modified. makeis smart enough to only recompile the files that were affected by changes thus saving compile time.

If you run:


this program will look for a file named ‘makefile’ in your directory, and then execute it.
If you have several makefiles, then you can execute them with the command:

make -f MyMakefile

Simple Example

This is an example descriptor file to build an executable file called prog1. It requires the source files file1.c, file2.c, and file3.c. An include file, mydefs.h, is required by files file1.c and file2.c. If you wanted to compile this file from the command line using c the command would be

% cc -o prog1 file1.c file2.c file3.c

This command line is rather long to be entered many times as a program is developed and is prone to typing errors. A descriptor file could run the same command better by using the simple command

% make prog1

or if prog1is the first target defined in the descriptor file

% make

This first example descriptor file is much longer than necessary but is useful for describing what is going on.

prog1 : file1.o file2.o file3.o

cc -o prog1 file1.o file2.o file3.o

file1.o : file1.c mydefs.h

cc -c file1.c

file2.o : file2.c mydefs.h

cc -c file2.c

file3.o : file3.c

cc -c file3.c

clean : rm file1.o file2.o file3.o

Let’s go through the example to see what make does by executing with the command make prog1and assuming the program has never been compiled.
makefinds the target prog1 and sees that it depends on the object files file1.o file2.o file3.o

makenext looks to see if any of the three object files are listed as targets. They are so make looks at each target to see what it depends on. make sees that file1.o depends on the files and mydefs.h

Now make looks to see if either of these files are listed as targets and since they aren’t it executes the commands given in file1.o’s rule and compiles to get the object file.

make looks at the targets file2.o and file3.o and compiles these object files in a similar fashion.

make now has all the object files required to make prog1 and does so by executing the commands in its rule.


Unix commands: sed & awk

Filed under: Linux — shadiyya @ 5:20 pm


It is a stream editor is used to perform basic text transformations on an input stream. The sed utility is a stream editor that reads one or more text files, makes editing changes according to a script of editing commands, and writes the results to standard output.

The pattern to match is typically included between a pair of slashes // and quoted. For example, to print lines containing the string “1024”, we may use:

cat filename | sed -n ‘/1024/p’

Here, sed filters the output from the cat command. The option “-n” tells sed to block all the incoming lines but those explicitly matching the expression.
Here is another example, this time for deleting selected lines:

cat filename | sed ‘/.*o$/d’ > new_file

In this example, lines ending with an “o” will be deleted. It uses a regular expression for matching any string followed by an “o” and the end of the line. The output (i.e., all lines but those ending with “o”) is directed to new_file.

To search and replace, use the sed ‘s’ action, which comes in front of two expressions:

sed ‘s/string_old/string_new/’ filename > newfile


The awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions. Awk breaks each line of input passed to it into fields. By default, a field is a string of consecutive characters delimited by whitespace, though there are options for changing this.

Awk runs through a text file by reading and processing one record at time. Its commands are written with the intention that they act repetitively on each record as it is read in to awk. A record that has been read by awk is broken into separate fields, and actions can be performed on the separate fields as well as on the whole record.

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.

Let’s start with a file, words.txt, that contains these lines:

nail hammer wood
pedal foot car
clown pie circus

Now we’ll use the print function in awk to plug the words from each input line into a template, like this:

awk ‘{print “Hit the”,$1,”with your”,$2}’ words.txt

Then the output will be:

Hit the nail with your hammer
Hit the pedal with your foot
Hit the clown with your pie

Shell Scripting

Filed under: Linux — shadiyya @ 10:28 am

Shell scripts combine lengthy and repetitive sequences of commands and generalize a sequence of operations on one set of data.

Following steps are required to write shell script:

(1) Use any editor like vi to write shell script.

(2) After writing shell script set execute permission for your script as follows

chmod permission your-script-name

$ chmod 755 your-script-name

This will set read write execute(7) permission for owner, for group and other – permission is read and execute only(5).

(3) Execute your script as

bash your-script-name
sh your-script-name

Following is an example of a very simple shell script

echo You are
who am i
echo The directory you are in is
echo The date is
echo calendar

It will print the user’s name, current directory, date & time and calendar of this month.

Now, we can color this output on the screen, by the following code word :

echo -e “33[31m This is in Red”

Now, to get a red colored calendar, we can use the following code:

echo -e “33[31m calendar”

There are many more interesting features in shells. Some detailed informations are available here:

July 24, 2011

Piping and Redirection in Linux

Filed under: Linux — shadiyya @ 6:07 pm

With pipes and redirection, we can “chain” multiple programs to create extremely powerful commands. Most programs on the command-line accept different modes of operation. Many can read and write to files for data, and most can accept standard input or output. This means that we can direct the output of one program as input to another program. We can then take the output of the second program and redirect it as input to yet another program, or redirect the output to a file.

A pipe is used to to send the output of one program to another program for further processing. With pipes, we can combine multiple commands together in a powerful way. The general syntax for pipes is:

$ command_1 | command_2 [| command_3 . . . ]

This chain can continue for any number of commands or programs.

The following is one of the most common ways of using pipes:

$ ls | less

As another example, suppose we have a text file called applist.txt and we want to find out what lines of it contain the word “desktop”. It’s easy with pipes. We list the contents of the file applist.txt and send the results to grep, which then filters all lines containing the desired word “desktop”, and displays those lines on your screen:

$ cat applist.txt | grep desktop

This direct connection between programs allows them to operate simultaneously and permits data to be transferred between them continuously rather than having to pass it through temporary text files or through the display screen and having to wait for one program to be completed before the next program begins.

Redirection is the transferring of standard output to some other destination, such as another program, a file or a printer, instead of the display monitor (which is its default destination). Standard output, sometimes abbreviated stdout, is the destination of the output from command line programs in Unix-like operating systems.

For example, to redirect the output to a file, we can use the > character like this:

$ ls > dir_listing.txt

The above redirects the output of the ls command to a file called dir_listing.txt. Because the output is redirected to a file, we won’t see any results of ls on your screen.

Each time we repeat the above command, the file dir_listing.txt is overwritten with the results of the ls command. If we want to append the new results to the file instead of rewriting it, we can use >> instead.

$ ls >> dir_listing.txt

Each time we repeat the above command, the new output of ls is added at the end of the dir_listing.txt file instead of overwriting the file.

The following adds the contents of File1 at the end of File2:

$ cat File1 >> File2

May 13, 2011

Shell programming exercises

Filed under: Linux — shadiyya @ 8:23 am

Some simple exercises in shell programming

1. Removing Specified file

2. Listing and sorting

3. Display few lines of a file

4. Locating a word

5. counting non-header lines

6. Sorting chromosomes based on occurrence

Blog at

%d bloggers like this: