Lab Assignment--Command-Line Linux and Parsing

Part 1-Linux/Unix Commands

Before you get started, you need to know the script command and how to
remotely log onto a Linux machine.  

You can think of the script as a "recording" of your Unix commands.
A script is started by typing: script filename.
Typically, you want to name your files with an extension of .log.
You end the script by typing exit.

To remotely log on to the Linux machines, you first need to know the Linux 
machines that are available to you.  The command to tell you that is: cs_clients CL115.

Then, you pick one machine from the list and type: ssh machine

Follow all of the steps:
  1. start a script in your home directory
  2. (hint: script commands.log will start the script and save it to commands.log)
  3. display all of the Linux machines in CL115
  4. log onto one of the Linux machines in the list
  5. (create and) change into your cs330 directory
  6. (create and) change into your labs directory
  7. (create and) change into your lab1 directory
  8. create two new directories named part1 and part2
  9. change into your part1 directory
  10. without using a text editor create three files called 'today', 'tomorrow', and 'yesterday'
  11. check the timestamps with ls -l
  12. copy 'yesterday' to a file called 'yesterday.bak'
  13. create a directory named BAK
  14. move the file 'yesterday.bak' to the directory BAK and rename it 'yesterday.bak20130911'
  15. change working directory to BAK
  16. check the timestamps and permissions with ls -l
  17. move up one directory
  18. remove the directory BAK and its contents
  19. logout of the Linux machine (hint: exit or lo)
  20. exit from the script (hint: exit)

Part 2-Parsing Line Input

General Goal:

Create a C++ class and test program that:


For this, you might like to review how to separate your code into three files.

Create the implementation (oneline.cpp) and test program (main.cpp) for the following oneline.h file:

#include <string>
using namespace std;

class OneLine
      void breakLine();
      void printReverse();
      istream &readLine(istream& is);
      string returnLine();
      string oneLine;
      char **words;
      int wordCount;
      void resetLine();

The following provides a description of each of the functions implemented in oneline.cpp:

  • initialize oneLine to empty string
  • initialize wordCount to 0
  • create a temporary C String version of oneLine
  • use strtok to break the temporary line into words
  • allocate enough space to hold all of the words and store them in words
  • store a count of the number of words in wordCount
  • (this is meant as a general algorithm, you may have to fiddle a little to get this working. Hint: this may involve cycling through all the words with strtok twice)
  • cycle through the words from wordCount-1 to 0
  • print each word as you are cycling
  • call resetLine to free up memory and reset oneLine and wordCount to empty string and zero respectively
  • read one line from is (in this case, a file stream) and store it in oneLine
  • return is
  • return oneLine
  • if allocation has occurred than free up the words
  • set oneLine to be an empty string
  • set wordCount to zero
  • if allocation has occurred than free up the words

The following provides the algorithm of main.cpp:

  1. create an ifstream for the file test.txt (please use this file for testing)
  2. create a OneLine object
  3. while you can still read from the file:
    1. call readLine
    2. call breakLine
    3. call printReverse
  4. close the file

For this assignment please use strdup whenever you need to allocate space and copy a string.

If your code is core dumping, you might want to try running it through valgrind with the command valgrind --leak-check=yes your executable. Look for messages of memory leaks. In the end, your code should have no memory leaks.


The following is a link to the output produced using test.txt:

A portion of your mark will be based on having no memory leaks. If your run produces core dumps, or if you want to double check to ensure that you have no memory leaks, you can use valgrind. Use it as below:


Submit 4 files to URCourses:
  1. script of Linux commands in Part 1
  2. code for oneLine.cpp
  3. code for main.cpp
  4. valgrind.out file

You will get a chance to work with this code again next week. If you get the valgrind output producing no errors, then you will be in good shape for next week. After next week's lab, your code should produce no errors or memory leaks.

Your mark this week will be based on a good attempt at main.cpp and oneLine.cpp.