Unix Training Exercise (10 points)

It is the purpose of this exercise to walk you through the basics of using the NIU Computer Science Department's Unix servers to develop and hand in your programming homework assignments.

Set up the PuTTY SSH cliend (If Needed)

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, 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 users.

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

Setting up a csci501 directory

Create a suitable directory within which to implement your assignments.

  1. Make sure you are in your home directory. At the Unix prompt, type the following command and press [enter] to execute it.

       cd
    
  2. Create a csci501 directory. At the Unix prompt, type the following command and press [enter] to execute it.

       mkdir csci501
    
  3. Navigate to the csci501 directory by executing the following command:

       cd csci501
    

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

  4. 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.)

  5. You can type the following command to list the files in your current directory:

       ls
    

Read a bit about the Unix operating system

Read through the following three web pages to get some background on the Unix operating system:

Practice some basic Unix commands

  1. Navigate to the directory created for this course by by typing the command:

        cd ~/csci501
    
  2. Use the ls command to list the names of the files and subdirectories in your working directory:

       ls
    
  3. Use the mkdir command to create a new directory for this assignment:

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

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

       cd Assign1
    
  6. Use the pwd command to print the absolute pathname of your working directory:

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

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

       ls -a
    
  9. 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.)

  10. You can also combine the two options:

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

       cd ..
    

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

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

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

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

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

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

       rm newdir/file1 newdir/file2
    
  17. 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 is unforgiving and can not be "undone!")

       rm -r newdir
    

Creating, compiling, and running 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. (While nano is easy to learn, it is cumbersome to use. The vim editor is harder to learn but is more efficient to use to write programs. emacs takes this a step further.) To open the editor and create the new file, execute the command:

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

    //******************************************************************
    //
    // CSCI 501 Assignment 1
    //
    // Author: your name
    //
    // Unix Training Exercise
    //
    //******************************************************************
    
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout << "Hello World!" << 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 -std=c++11 -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
    #
    
    CXXFLAGS = -Wall -Werror -std=c++11
    
    labtrain: labtrain.o
    	g++ $(CXXFLAGS) -o labtrain labtrain.o
    
    labtrain.o: labtrain.cpp
    	g++ $(CXXFLAGS) -c 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.

Handing In Your Assignment

To hand in your assignment you must follow the instructions in the Assignment Submission Instructions.

As discussed on in the Submission Instructions, for this assignment you would use this sequence of commands:

    cd ~/csci501/Assign1
    make clean
    cd ..
    mailprog.501 Assign1

Answer the questions when prompted by the mailprog.501 script to specify the assignment number (in this case, 1) and, if requested, the section number for the course within which you are enrolled. (Some classes only have one section and thus you might not be asked to provide it.)

You can check that your assignment was submitted by following the instructions in the Assignment Submission Instructions.

DO NOT try to submit your assignments using blackboard. DO NOT submit your assignment by mailing it to your TA or instructor. Any Such submissions will not be accepted.

Logging out of Unix

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

   logout

Last modified: 2022-01-18 11:25:59 CST