Category Archives: Programming

c-compiling and make

Make is a very useful tool and makes compiling very easy. Let’s take classical Hello World as an example. You can save the following file as hello.c

Now you can compile the code in linux box with a command

and then run it with

You can also make a simple makefile. For example you can give compiler flags to compiler with a following makefile. Save it as makefile to your programming directory

It means that the make gives the flags to c compiler. -g adds debug symbols (if you are using a debugger),  -Wall gives all warnings, -std=gnu11 uses c11 standard which is the latest c-standard and -o3 optimizes for speed.


Shell scripting

There are many shells available like bash, dash, ash, csh. Bash (Bourne Again Shell) is the richest with features but at the same time also big (combiled image in my system about 1 MB) and sometimes also slow. Therefore Debian and many derivatives (Ubuntu, Mint, …) use dash as the startup shell. Dash is smaller (compiled image size about 0.12 MB in my Ubuntu based system).

If you want to see the source code of the shell you can get it in Debian and derivatives with commands (following gets the source of bash)

Remember that you should not use sudo when getting the source code.

Next to the scripting itself. The scripts can be driven from the command line like

You can make a very simple Hello World script with editor and save it as

And you can run it on the command line

By adding to the script #!/bin/bash #!/bin/bash

you can call the script directly by giving the execute rights with chmod

Variables can be defined like variable_name=”some text” or in the case of numbers or single words variable=1 (without quotation marks). There are some predefined variables like PWD which means current working directory. When you are using the variables you have to use $-sign before the variable. Example

prints your current working directory.

Conditions. If then -structure.The test will be made inside of brackets [ ]. The structure is ended with fi.

Below we will test if some port is open with a netcat program. nc – z localhost 22 test if the port is open. The return value is stored in $? variable. If it is open the return value is 0 otherwise something else.

For-loop. We test now if some port is open. This is basically a very simple netstat.

It is usually a very good habit to try to keep the shell scripts as portable as possible between different shells. There is a command line tool called checkbashism available to chech if you have used something that would work only on bash.

Some topics to cover

  • If you want to read variables you can used read-function.

There are books on shell scripting

Adding code snippets to WordPress post

Probably the easiest way to add nice code lines to a post is to use some plug-in like Crayon Syntax Highlighter or SyntaxHighlighter Evolved.

codehighlighterMy favourite is Crayon Syntax Highlighter. Good thing is that there are lots of options available. Bad thing is, that there are for the first time too many options available. It means there is a bit of learning curve. You have to play a bit with it.

The code is inserted (copy pasted) in a editing window.  Here is an example of Hello World in c-language.

The second choice called SyntaxHighlighter Evolved is used by writing the tags direct with WordPress text editor. You should not use Visual mode editing, while it might remove the code tags. Remember that code begins with [ code ]-tag do not use < code > -tags!. You can add some variables like language. The code

will produce

main( )
printf("hello, world\n");