Sunday, 19 May 2013

Linux Development

Linux Development

A standard Linux system comes with many compilers and interpreters
for a lot of languages. We cover most of them, and will see tips and tricks on how to use them for small
scripts or powerful applications.

Tip 1: Graphical messages to the world

Often we write shell scripts and at one point we would like to display messages
or otherwise interact with the
user using a graphical way, without rewriting all our script in C.

A small program called gtk-shell was created for that purpose. It is a GTK-based program meant to be used in
shell scripts, that can interact with the user, and even return values to the script. Let's see a very simple


xv -quit -root `gtk-shell -t "Which file do you want me to use?" -fs`
This will display a graphical browsing box with the specified title, where the user can select a file, and the
file path will be returned to xv which will display it as background.
Gtk-shell can be used in many ways including displaying a message, asking for input, and more. Here is an
output of "gtk-shell --help":
gtk-shell (C) 1999 Patrick Lambert under GPL
This program will use the GTK library ( to ask for user input,
and can be used in scripts or called from other programs.
Command line options:
--help, -h This help text
--version, -v Show gtk-shell's version
--output <file>, -o Set the output file, default is stdout
--title <title>, -t Set the window title
--label <label>, -l Show a label to the user
--file-selection, -fs Display a file selection box
--size <x> <y>, -s Set the size of the window
--position <x> <y>, -p Set the position of the window on the screen
--query, -q Display a query box
--query-value, -qv Set a value in the query box
--append-eol, -eol Appends an EOL char at output time
--exit-code <str>, -ec String to display if user press Cancel
--view-file <file>, -vf View a file in a text box
--edit-file <file>, -ef Edit a file in a text box
--button <str>, -b Add a button to quit
--choice <c1> <c2>.., -c Display a choice box with the specified choices
Example: gtk-shell -l "Pick a choice" -c 1 2 3 4 -b Ok -eol
Example: xv -quit -root `gtk-shell -t "Which image?" -fs`
Gtk-shell is available at

Tip 2: Code reuse

Why write code that we already wrote for previous programs? Well here is a small tip that might save you
some time: Make a generic file with reusable functions.

Many languages make this easy to do. Object oriented languages like Java or C++ for example allow you to
make a class with utility functions, and then import them in your main program. This way you can use the
generic class in all your programs. Even in C you can do this by including the generic C file when compiling
with a command like:

gcc -o myprogram myprogram.c generic.c
Your generic utility functions would be in generic.c and gcc would simply add the file to myprogram.

Tip 3: Makefile don't equal C

Makefiles are used in most Unix C or even C++ programs. But nowhere does it say that they can't be used for
other languages. Make is a program installed with most Linux distributions, and on most Unix systems too.
Makefiles make your program portable, and easy to compile. These files can be used in C, C++, Java, and
any program that requires compilation.

Tip 4: Parsing the command line in BASH

Bash is a shell, but it's also a scripting language. You can make bash scripts, and you can pass parameters to
it. You can access the parameters with $1, $2, ... But what if you need the whole command line?
Bash uses special variables for all kind of purposes. One of them is $* which contains the whole command
line. You can use it rather than the $1, $2 if all you need is a string of text, passed on the command line.

Tip 5: Don't grep grep

A useful tool in scripting is the "grep" function. This program will find a string in a file. It is often used also
to find if a process is running:
ps ax | grep sendmail
That command will find if sendmail is running. The problem is that you might end up with an other entry for
this command. Because you grep for "sendmail", you may well end up with this command showing because
the "sendmail" string is in the command line. To avoid that, you can use the -v flag to grep:
ps ax | grep sendmail | grep -v grep
That command will find all the lines in the current process list that have the "sendmail" string in it, and will
remove the lines containing the string "grep".

Tip 6: Move a text into upper case letters

When you want to do text manipulation, you can use Sed and Awk. These 2 tools which come on most Linux
distributions, will allow you to modify text files in many ways.
To move a text file into upper case letters, you can use Awk in the following way:
awk '{ print toupper($0) }' old_file > new_file
Sed and Awk are useful for a lot of other uses, and are integrated in several products.

Tip 7: Using PASCAL on Linux

Linux comes with a lot of compilers and interpreters. These include programs for C, C++, Perl, TCL/TK and
more. Unfortunately most Linux distributions don't come with a Pascal compiler. Is it possible to compile
Pascal programs? It sure is.
Several projects were started to make a Pascal compiler for Linux. One is called GNU Pascal and is available
from That program will also run on any operating system
that supports the GNU C compiler.

Tip 8: Segmentation fault

One of the most common problems when making software is errors like "Segmentation fault", also called
SegFault. Here is what a SegFault is.
Virtual memory in a computer can be created in 2 ways: pages or segments. Paging means that the memory is
divided in pages of equal size, containing words of memory in it. Segmentation means that every process has
a segment of memory of needed size, with gaps of empty memory blocks between the segments.
The operating system knows the upper limit of every segment, and every segment begins at a virtual address
0. When a program accesses a memory block, it calls a virtual address that the Memory Management Unit
(MMU) maps to a real address. If the operating system sees that the requested address doesn't match any
valid address in the segment, it will send a signal to the process terminating it.
SegFaults are the direct result of a memory error. The program has a bad pointer, a memory leak or any kind
of error that makes it access the wrong memory address. To correct these errors you need to check pointers
and arrays for errors.

Tip 9: Who is online?

Many system administrators use scripts to help them in the process of managing a server. A common problem
is finding out exactly what users are on the system and what they are doing.
Several tools are available on the system to see who is online, what processes are running, and pipeing them
together can resolve many problems. Here are 2 small scripts that will show, first if a user is online, and then
what he is running:
who | grep $1
ps -aux | grep $1
The variable $1 here means the first command line argument, from a shell script. The who command first
checks if the user is online, then ps will show all the processes currently running under that user's UID.

Tip 10: Graphical toolkits

Most Unix programs are console based. This is mainly because several years ago, most computer users were
on text-only stations linked to big mainframes.
Now, Linux is becoming a full graphical desktop, and needs graphical programs, or at least frontends to
text-only programs. This is where graphical tookits come in. Here is a list of some of the toolkits available to
Linux programmers:
•??GTK - This is currently a very popular toolkit, and is the base of the GNOME desktop. It is C
based and easy to work with.
•??QT - QT is a C++ toolkit made by Troll Tech. It is the base of the popular KDE desktop.
•??WXwindows - This is a very complete C++ toolkit that is cross-platform. The same code
should work on Windows, the MacOS, Unix (Motif and GTK) and others.
•??GraphApp - This is also a cross-platform toolkit, but is one of the easiest libraries I ever saw. It
supports a smaller set of widgets, but takes only hours to learn. It will work on Windows, the
MacOS, Unix (Motif and Athena) and others.

Tip 11: IDE and visual interfaces

If you are a programmer that comes from the Windows world, you may be used to code in a visual interface
or an IDE. Coding in a text editor and then going to the shell to compile may be the traditional way of Unix
and Linux users, but it may not be what you are looking for.
You can use a visual interface in Linux too. Searching for "visual" or "IDE" at will
return a few programs available to you. But there may be a much better way.
The purpose of a visual interface is to make coding more efficient. There are many so-called programmer
editors. These are text editors so powerful that you can do everything with them. This includes editing
multiple files, compiling and even debugging your program.
One of them is emacs, available from the GNU project. It is so powerful that it enables you to edit text,
compile, read mail, newsgroups, and even browse the Web. It may seem far more complex than your usual
Windows visual editor, but it may prove really interesting in the end.

Tip 12: Free software and copyleft

Most software programs under Linux come under the GPL license. The so-called copyleft agreement. It may
be confusing to new Linux programmers. Here are the main features of the license, and why you may or may
not want to use it for your programs. The full license is available from the GNU site at
The GPL license provides software free. Not as in free of charge, but in freedom. You can charge money for
free software or software under the GPL. What you can't do is restrict its use.
You must provide source code, or a way for the user to have access to that source code. This means that if
you release a program as a binary only, it can't be under the GPL license and may not contain GPL code from
other people.
If you use code available from other people under the GPL license, you can modify it and redistribute it under
the same license, with source code. Also, you must keep the previous copyright notices, and write down in
the program what changes you have made to it. This also means that anyone can modify your programs, as
long as they keep your copyright notices there, and release their modifications under the same license.

Tip 13: Talking to the terminal

Under DOS, or other text based systems, there is only one way in and one way out. Under Linux and every
Unix system, there is one way in but two ways out.
The terminal has 2 outputs, one for standard output and one for errors. By default, they both display on the
same device: your monitor. But they can be redirected, for example, to different logs. This is why it's
important for you to use them properly.
The standard output is called stdout, and printing to it will print to the screen, or where it is redirected. In C, it
may look like this:
fprintf(stdout, "Test\n");
The standard error is called stderr:
fprintf(stderr, "Error\n");
To input, you need to read from the standard input, called stdin. They are all available to you from the stdio.h

Tip 14: Internet technologies

The Internet is of course a big phenomena, and Linux is a big part of it. Linux servers are very popular, and
you can be a part of this revolution.
Among available technologies, here are the most popular ones:
•??Java - The Java language is a new and powerful language made to be cross-platform. Since the
Web must be available on all systems, this makes Java a very powerful tool. You can make
Java applets that will run on the user's system and display information or interact with him.
•??JavaScript - This is similar to Java, but in a Web page itself. It is embedded in HTML, to do
small functions like rollover scripts, and making text appear on the screen.
•??CGI - The CGI protocol is very much in use for servers with some sort of account system, like
online shops and search engines. You can make CGI scripts in any language, and make your
Web server run them on the server.

Tip 15: Library types

Two types of libraries exist on most operating systems: shared and static. On Windows, they are .DLL, for
dynamically linked library, and .LIB for static library.
On Linux and most Unix, they are .so and .a files. The shared libraries, the .so files, are loaded at runtime.
The .a files, or static libraries, are loaded at compile time and are no longer required to run the binary
When you make a program, you must decide if you will link it to a static library or a shared one. You will
want a shared library in most cases because standard libraries are available on most systems, and would be
too big to include in a binary file.
If you have a small library that is not one of the standard ones that you need, then you may decide to include
it in your binary program. In that case, simply add it like any other object file in your compilation:
cc -o program file1.o file2.o library.a

1 comment:

  1. BlueHost is ultimately one of the best hosting company for any hosting plans you need.