Unix Training Exercise


This is a training exercise intended to walk you through the basics of using the Unix operating system. There is nothing to submit for this exercise and it is not graded.

Prerequisites

Before you can do this training exercise, you must connect to Unix and activate your departmental Unix account.

  1. If you're using Windows and want to download and install the PuTTY SSH client, follow the instructions found here. Note that users of Windows 10/11, macOS, and Linux already have access to command-line software that can connect to the departmental Unix servers. Downloading and installing PuTTY is optional (but recommended) for Windows 10/11 users.

  2. Follow the instructions on how to connect to Unix and activate your departmental Unix account.

Configuring Your Unix Account

Once you have successfully logged in, you can run a script to do some basic configuration of your Unix account for CSCI 241.

At the command prompt, type the following command:

   /home/turing/t90kjm1/bin/configure

Note: In the command above, the last character of the directory name t90kjm1 is the digit one, not a lower case letter 'l'. Make sure that you type this correctly.

It's also possible to copy a command like this from a web page and paste it into the PuTTY terminal window. Select the command in the web page, type Ctrl-c to copy the command text, click on the PuTTY window to give it the focus, and then right-click to paste the command.)

If successfully executed, the configure script will:

  1. Create a directory named bin, change the permissions on it, and make links to two other scripts that will let you obtain files for the 241 programming assignments
  2. Create a directory called csci241 to hold the files for your CSCI 241 programming assignments and change the permissions on the directory to make everything under it unreadable to other users.
  3. Provide you with a basic .vimrc configuration file for the vim editor (assuming you don't already have one of your own). You may or may not ever make use of this.

If you already have bin or csci241 directories from a previous semester or another class, they will not be altered.

Read a bit about the Unix operating system

Before you proceed, read through the following three web pages to get some background on the Unix operating system:

Practice some basic Unix commands

  1. Run the setup script for this Unix Training Exercise (Assignment 0) by typing:

        setup 0
    

    To set yourself up to begin a new programming assignment, you should use the setup program because it will save you from a number of possible problems. To use it, you simply have to tell it which assignment you want to get set up for. For example, if you want to get set up for Assignment 1, you would run the following command:

        setup 1
    

    If you want to set things up for Assignment 2, you would replace the 1 in the example with 2 and so on for Assignments 3, 4, 5, etc. Running the setup script like this will create a new directory and install the appropriate skeleton files. These files include what is known as a makefile as well as any sample data, header and possibly some source files that your instructor might have for you.

    The setup script will always create a directory named Assign0, Assign1, etc. with the same number as the assignment number specified on the command line when you ran the script. These assignment directories will always be placed in the directory named csci241 which will be created (if it does not already exist) in your home directory.

    It is possible to run the setup script multiple times for the same assignment (if you accidentally delete one of the skeleton files, for example). The script will ask you whether or not you want to overwrite any existing header files, source files, or makefile with the same names as the skeleton files it is copying. Respond with y to overwrite the existing file or n to skip copying that skeleton file.

  2. Navigate to the directory created for the training exercise by typing the command:

       cd ~/csci241/Assign0
    

    Notice how the working directory name displayed in the command prompt changes?

  3. Use the ls command to list the names of the files and subdirectories in your working directory:

       ls
    
  4. Use the mkdir command to create a new directory:

       mkdir newdir
    
  5. Use the ls command again to list the names of the files and subdirectories in your working directory:

       ls
    
  6. Use the cd command to change to a new working directory:

       cd olddir
    
  7. Use the pwd command to print the absolute pathname of your working directory:

       pwd
    
  8. List the names of files in your working directory:

       ls
    
  9. Use the -a option to see "hidden" files (file names that begin with .):

       ls -a
    
  10. Use the -l option to see a "long" listing of more information about the files:

       ls -l
    

    (You can read more about the information displayed by the -l option here.)

  11. You can also combine the two options:

       ls -al
    
  12. Change your working directory to the parent directory:

       cd ..
    

    (That's "period period", which means "parent directory.")

  13. Use the cp command to copy some files from olddir to newdir:

       cp olddir/file1 olddir/file2 newdir
    
       cp olddir/.anotherFile newdir/.anotherFile
    
  14. List the contents of newdir to verify that the copy has taken place:

       ls -al newdir
    
  15. Use the mv command to rename .anotherFile so that it is no longer "hidden":

       mv newdir/.anotherFile newdir/anotherFile
    
       ls -l newdir
    
  16. You can also use mv to move a file from one directory to another:

       mv newdir/anotherFile olddir
    
       ls -l newdir
    
       ls -l olddir
    
  17. Use the rm command to remove the files in newdir:

       rm newdir/file1 newdir/file2
    
  18. Once a directory is empty, it can be removed with the rmdir command:

       rmdir newdir
    

    (You can also use the rm command with the -r option to remove a directory and all of the files and subdirectories within it. Be very careful when doing this!)

       rm -r newdir
    

Creating and executing a C++ program

  1. Open a text editor and create a file called labtrain.cpp. For beginners, the easiest editor to use would be nano. To open the editor and create the new file, execute the command:

       nano labtrain.cpp
    
  2. Type in the following program:

    /**
     * CSCI 241 Assignment 0
     *
     * Author: your name
     *
     * Unix Training Exercise
     */
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
        char name[50];
      
        cout << "What is your first name? ";
    
        cin >> name;
    
        cout << "Hello, " << name << endl;
    
        return 0;
    }
    
  3. Save the file by executing Ctrl-O.

  4. Exit nano by executing Ctrl-X.

  5. Compile and link the program by executing the command

       g++ -Wall -Werror -ansi -pedantic -std=c++14 -o labtrain labtrain.cpp
    
  6. If there are any errors, open the labtrain.cpp file in nano and make the necessary fixes. Then run the previous command again.

  7. Once your program has compiled and linked successfully (no error messages), you can run it by typing the following command:

       ./labtrain
    

Creating and using a makefile

Using the g++ command to compile and link your program repeatedly can involve a lot of typing, especially if you have multiple source code files (which is the norm). Also, if you have multiple source code files, you probably don't want to recompile all of them every time - typically, you only want to recompile the files that have been changed since the last time you compiled your program. Makefiles and the make utility are a way to avoid these issues. You can read more about make and makefiles in the relevant section of the Course Notes.

  1. Create another file, this time called makefile by executing the command

       nano makefile
    
  2. Type in the following code.

    IMPORTANT NOTE: The lines that begin with g++ and rm MUST be indented by using a tab character, not spaces.

    #
    # PROGRAM: Lab Training Exercise
    # AUTHOR:  your name
    #
    
    CXX = g++
    CXXFLAGS = -Wall -Werror -ansi -pedantic -std=c++14
    
    labtrain: labtrain.o
    	$(CXX) $(CXXFLAGS) -o labtrain labtrain.o
    
    labtrain.o: labtrain.cpp
    
    clean:
    	rm -f *.o labtrain
    
  3. Save the file by executing Ctrl-O

  4. Exit nano by executing Ctrl-X

  5. Execute the command make at the command prompt. This will use the make utility to compile and link the labtrain program. At this point, you shouldn't have any errors in labtrain.cpp, but you may need to fix mistakes you made in your makefile. For example, if you get an error message that says "*** missing separator. Stop.", that means you've indented your makefile using spaces rather than the required tabs.

  6. Run the program by typing the following command:

       ./labtrain
    
  7. If the program runs successfully, execute the command make clean at the command prompt to remove the executable file and the object file (labtrain.o). This step is necessary because these two files can become quite large.

You will not normally be required to write a makefile for your assignments in CSCI 241. Instead, they will usually be provided to you by the setup command or will be available for download from the Grade-o-Matic auto-grader.

Logging out of Unix

When you are done with a Unix session, you can log out of the system by typing the command

   logout