Introduction to Computer Science

 

Lab Three – Cygwin and Bash

     The UNIX operating system was first created over 30 years ago.  It has become one of the most widely used systems in the world.  Currently there are many different “versions” of UNIX.  Cygwin is a UNIX emulator (an environment which behaves like a UNIX operating system).

     One part of Cygwin (or any UNIX system) is the command interpreter or command shell.  The command shell we are using is called Bash (which stands for Bourne-Again Shell).

     In this lab you will learn more about how to manipulate the Cygwin Environment using Bash.

 

The Filesystem   

     All the information Cygwin uses is stored in the filesystem.  The filesystem is a hierarchical (or tree) structure consisting of directories (similar to folders in Microsoft Windows) and files.  Each file and directory has a name.  A directory may contain other directories and files.  The main directory at the top of the hierarchy (often called the “root” directory) is named “/”.  To move from one directory to another use the “cd” command:

 

cd directoryName

 

To move up one directory in the filesystem hierarchy, type:

 

cd ..

 

This changes the current directory to the main or top-level directory.  To see the name of the current directory use the “pwd” command (which stands for “path to working directory”).

 

     To see what is stored in the current directory use the “ls” command:

 

ls

 

     The “ls” command also has a number of options (try these to see what they do):

 

ls –a

ls –l

ls –F

ls –al

 

Command options are usually specified using “-“ followed by the option desired.


Files

Files can be copied using:

 

cp originalFilename newFilename

 

For example typing:

 

cp hello.cpp newHello.cpp

 

will copy hello.cpp and name the newly copied file “newHello.cpp”.  You can also include directory names:

 

cp /home/jeffrey/hello.cpp /hello.cpp

 

will copy the hello.cpp file contained in the /home/jeffrey directory to the main directory.  The new file will also be named “hello.cpp”.

 

To copy a directory and all of its contents, use the “cp” command but specify directory names (instead of individual file names) and an additional option:

 

cp –r /home/jeffrey  /temp

 

If you wish to move a file you can use the “mv” command:

 

mv oldName newName

 

NOTE:  There is no “rename” command.  To rename a file, use the “mv” command to move the file from the old name to the new name.

 

ALSO NOTE:  The move command will OVERWRITE the file specified as the second argument.

 

To remove a file:

 

rm nameOfFile

 

There are a number of ways to look at the contents of a file:

 

less nameOfFile

more nameOfFile

gvim nameOfFile

 

Notice that “less” and “more” do the same thing.  Both commands will show you one windows-worth of the file contents.  You can then press the Enter key to advance one line or press the space bar to advance to the next windows-worth of contents.  You can move backwards through the file using the “b” key.  To quit, just press the “q” key.

 

Directories

Manipulating directories is a little different than manipulating files.  To create a new directory the command is:

 

mkdir  directoryName

 

To remove a directory:

 

rmdir directoryName

 

Note that the directory must be empty for you to remove it.

 

Command History

Bash remembers the last 500 commands you have used.  To see these commands you can use the “history” command.  You can access these commands in a number of different ways.  You can use the up arrow, scroll back through the commands and hit enter when you find the one you want.  You can also use the history substitution commands.  These commands start with “!” followed by different options.

 

!! will repeat the last command

!40 will repeat the command numbered forty in the history list.

!g++ will repeat the last command which started with “g++”.


 

A Better way to Quit

     When you are finished using Cygwin use one of the following commands:

and the Cygwin Environment window will be closed.

 

Lab Activity:  Creating a Directory Structure

     Create a new directory structure to organize your programs and lab work.  First create three directories called “archive”, “projectOne”, and “lab” in your home directory.  The structure should be as follows:

 

 

 

 

 

 

 

 

 

 

Next move your project one C++ file into the projectOne directory and copy any C++ files created during the lab into the lab directory.

 

Finally, create backup copies of the projectOne and lab directories as follows:

 

cp –r projectOne archive

cp –r lab archive

 

The archive directory should now contain an exact copy of both the projectOne and lab directories.


 

Lab Homework: Evaluating Expressions

     Suppose that a, b, c are int variables with a = 10, b = 4, and c = 3.  For each problem below determine what result is printed on the screen.  Each problem is independent of the others.  Turn in your answers next week at the beginning of lab.

 

  1. cout << a + b + c << endl;
  2. cout << a * a + 2 * b + c << endl;
  3. cout << a % b << b % c << endl;
  4. cout << a++ << ++b << ++(c++) << endl;
  5. b += a + c; cout << a + b + c << endl;
  6. a = b; b = c; c = a; cout << a + b + c << endl;
  7. cout << a << “+” << b <<  “+” c  << endl;
  8. a = b/c; cout << “b is: “ << a << “ times “ << c << “ plus “ << b%c << endl;