Some Very Basic Unix Stuff

This is how to do some basic things in UNIX. If you are on windows then get the cygwin UNIX simulation tools and put them on your machine (or use the CS UNIX machines and you CS account). OSX is UNIX so you don't need to do anything special.

I/O

Standard input (denoted stdin) is the input that comes in from the console (that is what you type in). If a program accepts input from standard input it gets input from the person typing in.

If your program is fred which accepts input from standard input then just type fred (return) followed by typing in your input. In UNIX end with a control-D character. In windows end with a control-Z character.

Standard output is the character output to the terminal window you are using.

fred
it waits for you to type.

Redirection If you put a < then it redirects input from a file. To input from file "dogs" into command fred then type:

fred < dogs

If you put a > then it redirects standard output to a file. If fred outputs to standard output then

fred > cats
will write the output of fred to the file cats

Tar files

To create a tar file you use the unix tar command. To put the files cats and dogs into tar file animals.tar do:

tar cvf animals.tar cats dogs
WARNING: don't forget to put the name of the tar file (animals.tar) in or you could OVERWRITE your files!

To unpack a tar file

tar xvf animals.tar
To list the table of contents of a tar file animals.tar
tar tvf animals.tar

Man pages

A correctly configured UNIX system (OS X included) has manual pages that can be accessed by invoking the man command. Suppose you want to know more about the sort command. Type this

man sort
to get the manual page or "man page".

Make files

make is a dependency driven build tool. It allows you to rebuild only the parts of your project that have changed. I provide a make primer to show you on the general features of make. However, for a single flex file (see flex primer) you only need to put this into a file called makefile in the same directory as your code.


BIN  = tree
CC   = g++
CFLAGS = -g -DCPLUSPLUS

SRCS = $(BIN).l OBJS = lex.yy.o LIBS = -lfl

$(BIN): $(OBJS) $(CC) $(CCFLAGS) $(OBJS) $(LIBS) -o $(BIN)

lex.yy.c: $(BIN).l flex $(BIN).l


NOTE: The varilabe BIN above is the name of the binary to be created.

NOTE: the lines that begin with whitespace begin with exactly one tab (I am not kidding).

If you execute the program make it will look in the directory in which it was executed for a file named makefile and use that to direct a build in that directory. For example, suppose you had a file tree.l (the only file that you need to build) you wanted to turn in for an assignment you could:

  1. create file tree.l
  2. put the makefile code above into file makefile
  3. type make if you want to build your code
  4. type tar cvf assignment1.tar tree.l makefile to build a tar file to submit for assignment 1.
  5. submit it.
  6. wait for email about whether you built and tested.
  7. go to step 1.

Reading sdiff Output or How to Read Your Test System Results

sdiff stands for "side by side difference". It is a UNIX command used to compare the output of your program to the expected output. Here is how to read the output of sdiff. Assume you have two files bigthings is a list of big things and mammals is a list of mammals. The files are alphabetized and tend to have some overlap. Here is bigthings:

cows
horses
houses
planets
trucks
zebra
Here is mammals:
cowz
elephants
horses
zebra
executing the command:
sdiff bigthings mammals
would give you this output:
cows               |    cowz
                   >    elephants
horses                  horses
houses             <
planets            <
trucks             <
zebra                   zebra
The two files are presented side by side with a central column with one of 4 characters: |, <, >, space. These mean: Note: lines may be different because they have substituted blanks for tabs or one may have trailing blanks that other doesn't.

Note: sdiff tries to get the lines to "line up" in sequence. A random reordering of one file tends to make all the lines not match.

In the above example 3 things are in bigthings that are not in mammals, 1 thing is in mammals that is not in bigthings, and 1 thing is a line that is has been substituted for another.

Check the man page for sdiff for the options, in particular the -w option.