More on *nix

Programming tools

There are a wide variety of tools available to aid the programmer, several of these we'll consider here:

Makefiles

Makefiles simplify life when you have a number of commands to run in order, depending on whether certain files have been modified
foo:  foo.o
      g++ -o foo foo.o

foo.o: foo.cpp
       g++ -c foo.o foo
The makefile above says (basically)
  1. If foo.o has changed:
    1. If foo.cpp has changed then
      execute command g++ -c foo.o foo.cpp
    2. execute command g++ -o foo foo.o

GDB debugger

Debuggers allow programmers to analyse the run-time behaviour of a program.

To try a debugger:

  1. Recompile the program for the debugger:
    g++ foo.cpp -g -o foo
  2. Run the debugger on the program:
    gdb foo
Options in the debugger include:

grep and find

Often we need to search our directories for a file with a specific name, or a file which contains a specific word or series of words.

To search the current directory (and all below it) to find a file named foo, use the following command:

find . -name foo -print

To search the current directory (and all below it) to find a file whose name begins with foo use:
find . -name 'foo*' -print

To search the current directory (and all below it) to find a file whose name ends with .foo use:
find . -name '*.foo' -print

To search all files in the current directory for ones which contain the phrase blahblahblah use the command:
grep blahblahblah *

Note: you can only run grep and find on files and directories for which you have `read' permission

Command interpreters: shells

A shell is an interactive command interpreter, and uses a syntax similar to C++.

You can either type commands directly in via the keyboard, or put the commands in a file (which we then call a shell script)

We are using the bash shell, and the first thing it does when you log on is to run the commands in your .bashrc, .bash_profile, and .bash_aliases files

Each individual command is run as a seperate process

When you logout, the last thing done by the shell is to run the commands in your .bash_logout file

You can learn more about the shell by entering man bash

Other useful tools

There are many other useful programs available in the libraries, here are a subset of them:

diff to compare files

sort to sort lines in a file

history for repeating commands

Calling commands from C++ programs

Compressing large files: gzip, gunzip

tar for directories

Controlling processes and monitoring resources

Monitoring processes

The ps -f command

Lists the processes you have running, and related information

Controlling processes

The kill command

There are two ways to kill a program, the kill command, or ^C (control-C)

The fg and bg commands

These move processes between foreground and background

The nice command

Controls how much of the CPU a job can access per minute

Monitoring disk utilisation

The du command

The df and quota commands

- df lists the system directories and space used/available in each
- df directoryname lists the space used/available in that directory

quota displays your use and limits on disk space and number of files
- quota is a soft limit you can temporarily exceed
- limit is the hard limit
- filesystem indicates which directory area your files are stored in

Monitoring users

While the sysadmin does most of the worrying about who is doing what on the system,

*nix assumes a co-operative environment where anyone can get much of this information

The w command

The w command lists who is on the system and what they're doing

The who, whoami, and users commands

These commands list who is currently on the system, in different formats

Checking your environment

The uptime and hostname commands

hostname gives the exact name of the machine you are logged into.

uptime gives:

I/O streams, redirection, and command lines

There are four main techniques for getting information into and out of programs Each of these options has advantages and disadvantages

I/O redirection

I/O redirection is carried out from the prompt, rather than from inside the program

Redirection examples