CS246-Assign01:  Winter 2012



SubmitInstructions to submit assignments (using Marmoset):



            Instructions to submit to Marmoset can be found here:


Bash.  All shell scripts you write should work on the bash shell.

Linux.  Do assignments on UW CS Linux machines.

ASCII.  Turn in the written (non program) part of this assignment as an ASCII file.

ASCII editor.  You will need to use an ASCII editor such as vi or emacs to do this assignment.


Asgn 1a: Basic shell features.  For each part of this question, give one Unix command, on a single line.

All of your answers should be in the file 1a.answer. Your answers should be developed using the UW Linux servers.


Each answer should be one line and no pipelining is needed.


Start in your home directory.

Enter the command to:


1a1) List names of all files and directories in the current working directory, including hidden files and directories. 


1a2) In your home directory, create an empty file called 'empty'


1a3) Clear the terminal screen.


1a4) While working in your home directory, create a directory called Documents and inside that directory create a directory called CS246.


1a5) Move directory CS246 to the home directory, so that its absolute path is becomes ~/CS246.


1a6) Change your current directory to the CS246 directory.


1a7) In CS246, create a directory called "a2"


1a8) Rename the "a2" directory to "a1".


Submit a1.answer (containing your answers) to Marmoset.


1a.answer should look like the following:





Asgn 1b: Debugging shell scripts.  Shell scripts tend to be short and yet difficult to write.  It is recommended that you initially include lots of debugging statements (use “echo” to print out things) in your scripts.  In the final version, you can remove or comment out these debugging statements using “#”.  Here is a simple example script called testing. This contains several illustrative debugging statements.


echo BEGIN testing     # Check that script gets started

echo  \$1 =  \"$1\"    # if $1 is whoami, this prints out:

                       #     $1 is "whoami"

echo RUN 'echo $1'

echo $1


echo RUN 'eval $1'

eval $1


echo END testing       # Check that script finishes 


Create an ASCII file called testing that contains the above script.  Make it executable by typing:

chmod u+x testing

Try executing this script as follows. Don’t forget the “./”

./testing uname     

./testing date



Asgn 1c: Create a directory structure.  In your Linux file directory, create the following directory structure (the leaves are ASCII files) in your home directory. Use the commands mkdir and cd to do this.














You are to create the contents of the above files as follows:


morning: echo Good morning

evening: echo Good evening

dummy.h:  // Just a comment

msgHi.h: const string msg = "HI";

msgBye.h: const string msg = "BYE";


Note: You can create simple files this way:

echo Stuff to go in file > fileName


Make the files called morning and evening executable and try executing them, using commands such as

chmod u+x morning



The file mainHi.cpp should contain:

#include <iostream>

#include <string>

using namespace std;

#include "msgHi.h"

int main ()


    cout << msg << "\n";    // Print message


The file mainBye.cpp should be like the file mainHi.cpp except in the “#include” line, “msgHi.h” should be changed to “msgBye.h”.  In the directory cppsource, compile mainHi.cpp using the command

g++ mainHi.cpp

and then run the compiled program by giving the command


In a similar way, compile and run mainBye.cpp

The file funny.cpp should contain this script:

// This C++ program is incomplete on purpose

/* Here is an example of a nasty surprise in C/C++ */

    if (x = 10)   // Should be:  if (x == 10)

        // etc etc etc  This is the last line of the script


You are to “zip” your directory structure using this command (give the command in your home directory):

zip -r cs246.zip cs246

The –r argument means to recursively zip.


This places an encoded version of your directory structure in the file called cs246.zip.  Note that cs246 is the top directory in your directory structure.  Submit the zip to Marmoset.


Asgn 1d: Create an alias and a bin command.  Create an alias for clear called clr. Trying using your clr command to “erase” your terminal’s current output.


In your bin directory (within your home directory), create a file (a shell script) called p (abbreviation for print).  Create p such that it is a command that has the same effect as the "less" command (implement p by calling "less").  Remember to start your script with


and remember to make it executable.  Try using your “p” command to view files in your directory structure.  Inspect the path used by the shell for finding commands such as p by typing

echo $PATH

Check to see that your bin directory is in the search path.


Try giving the following commands to determine the type or location of clr, p and clear.

type clr

type p

type clear

For both clr and p, start up a sub-shell (by typing bash), and see if your commands (clr and p) still work.

Logoff and login again and see if your clr and p commands still works.


Asgn 1e: Search for bad equal sign.  A characteristic error when using C or C++ is to type = (equal) instead of two equal signs ==, for example

if (x = 10)   // if (x == 10) was intended

Write a shell script (and put it in your bin directory) called eqeq (equal equal) which uses recursive egrep to list lines containing the following pattern on a line:  Any number of blanks and tabs, then “if” then zero or more blanks, then “(“, then any characters, then “ = “ (blank, equal, blank), followed by arbitrary characters, then by “)”, then followed by arbitrary characters on the line.  You can read the description of egrep by typing

man egrep   # Manual for egrep

Test eqeq by giving this command in your home directory.


Asgn 1f: My find.  Write a shell script called myfind which is a useful but simplified version of the find command. Put it in your bin directory.  (See “man find” for a description of find.)  The myfind command uses find to recursively look for files whose names contain given strings, starting in the current directory.  For example

myfind .cpp stdio

uses “find” to locate all files recursively that have “.cpp” or “stdio” in their name starting in the current directory.   Your myfind script should print the message

Missing argument(s)

and stop (and should exit with a code of 1) if it is given no arguments. 

If the file has both stdio and .cpp then myfind may list the file twice.


Asgn 1g: My grep.  Write a command called mygrep which is a simplification of the grep or egrep command.  See “man grep” or “man egrep” for a description of grep or egrep.  Your mygrep command is to use egrep. (Note that egrep is same as grep –E). It should recursively locate lines in files that contain given strings (one or more strings), starting in the current directory.  For example

mygrep msg HI

uses “egrep” to locate all lines in files recursively that contain the string msg or the string HI, starting in the current directory.  Test your mygrep command by giving this example command.  Your mygrep script should print this message to standard error

Missing argument(s)

and stop (and should exit with a code of 1) if it is given no arguments.  If the file contains more than one string, mygrep will list the file for every string it contains.


Asgn 1h: Obsolete C++ files.  Write a command called obscpp (obsolete C++) that “marks” files as obsolete if they end in “.C” or “.cc”  For example, a file named test.cc will be marked by being renamed as test.cc.obsolete.  For example, the command

obscpp test active

will look recursively for files (not directories) in the given directories (named test and active in this example) and will mark any of them ending in .C or .cc as obsolete.  Use a for loop inside a for loop to do this.  The outer loop will deal with each of the directories (test and active in this example).  The inner loop will be based on a “find” that locates each file ending in .C or .cc.  See Buhr’s example on slides 61-65 for ideas.  If no files are given (as arguments), your script can simply do nothing.


What to submit for marking.  Submit the following files to Marmoset:



File to submit