\documentclass[10pt]{article}

\usepackage{fullpage}
\usepackage{times} 
\usepackage{mathptm}
\usepackage{epsf}

\textwidth 6.5in
\textheight 9.25in
\topmargin +.15in
\oddsidemargin 0in
\evensidemargin 0in

\def\BeginAnswer{\vspace{2ex}\noindent {\bf Answer}:}
\def\AnswerOn{} % dummy; no effect
\def\AnswerOff{\long\def\BeginAnswer##1\EndAnswer{}}
\def\EndAnswer{}
\long\def\BeginComment#1\EndComment{}
\def\EndComment{}

% Specify either \AnswerOn or \AnswerOff
\AnswerOn

\input{misc}

\begin{document}

\parindent 0pt
\parskip 5pt

\begin{center}
\begin{picture}(432,35)(-18,0)
\thicklines
\put(0,0){\framebox(396,36)}
\thinlines
\put(-2,-1.85){\framebox(400,40)}
\large
\put(3,5){\bf CSE 399/004 Spring 2006}
\put(393,5){\makebox(0,0)[br]{\bf DUE: Mon. 27 February 2006}}
\put(3,31){\makebox(0,0)[tl]{\bf Unix/Linux Skills}}
\put(393,31){\makebox(0,0)[tr]{\bf Final Exam (Take-Home) Solutions}}
\end{picture}
\end{center}
\normalsize

\vspace{.25in}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You should write up this exam using latex (doing the editing in emacs).
In each question, you will be asked to write some code either in perl or
latex.  You should copy the code to the latex source file (after the
appropriate question), adding formatting as necessary.  Note, some 
characters (for example, `\$') will need to be handled specially.

When you are finished with the assignment, attach the latex source file
(which should be compilable using the command `pdflatex') to an email
and send to \emph{mcorliss@cis.upenn.edu}.  The subject of the email should 
be ``cse399 - final'' (without the quotes).  The exam is due on
Monday, February 27th, at 1pm.  Total points: 60.

\begin{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

Preliminary.

\begin{enumerate}

\item Change your location to the 'cse399' directory you created in 
homework 1 (or create it again if you deleted it).  Copy the directory
/home1/m/mcorliss/teaching/cse399/final and 
all its subcontents to your current location.  Change your location to
this new directory (final).  In this directory, you will find files `final.tex'
and `misc.tex'.  Use these files to write up your exam in latex.  Use 
the command `pdflatex' to test your source file and to generate a pdf.

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$6 Points$]$ Shell.

\begin{enumerate}

\item Describe what the command `which' does?  Show one use of it.

\BeginAnswer
\\
`which' prints out the path of the executable (or executables) that
would have been executed when this command is entered at the prompt.\\
\begin{verbatim}
bash$ which grep
/bin/grep
\end{verbatim}
\EndAnswer

\item Describe what the command `finger' does?  Use `finger' to find
the last time you logged in and from what machine.

\BeginAnswer
\\
`finger' displays information about local and remote system users.\\
\begin{verbatim}
bash$ finger -l mcorliss
Login: mcorliss                         Name: Marc L. Corliss
Directory: /home1/m/mcorliss            Shell: /pkg/bin/bash
On since Wed Mar 15 15:11 (EST) on pts/18 from lvn614-4.cis.upenn.edu
No Mail.
Project:
Computer and Information Science, University of Pennsylvania.
Plan:
No plan.
\end{verbatim}
I last logged in on Wednesday March 15 from lvn614-4.cis.upenn.edu.\\
\EndAnswer

\item The command `diff' can be used to determine the differences
between two files (unlike `cmp', which can only determine the first
difference).  Describe, in detail, the format `diff' uses to 
indicate any differences between two files.

\BeginAnswer
\\
`diff' prints the differences between two files.  For each difference
found, `diff' prints out the line numbers from the first file, followed
by a letter indicating the type of difference, and the line numbers
from the second file.  The types of differences are `a', meaning
lines were added in the second file, `d', meaning lines were deleted
in the second file, and `c' meaning lines have changed in the second 
file.  Then the different lines from each file are listed below 
(separated by a line of dashes).  Those lines preceded by `$<$' are 
from the first file and those preceded by `$>$' are from the second 
file.\\
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$15 Points$]$ Emacs.

\begin{enumerate}

\item Find the command (only one command) in emacs to highlight the 
entire buffer.

\BeginAnswer
\\
C-x h\\
\EndAnswer

\item Open the file `data.txt'.  Notice that the lines in this file
are split into columns using ``:''.  Use regular expression replacement
in emacs to reverse the order of the columns.  The first column in
`data.txt' should become the last column, the second column should 
become the second-to-last column, ...

\BeginAnswer
\\
Enter the command M-x replace-regexp.  Then enter the following search
expression at the prompt:
\begin{verbatim}
^\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\(.*\)$
\end{verbatim}
At the next prompt, enter the following replacement expression:
\begin{verbatim}
\5:\4:\3:\2:\1
\end{verbatim}
\EndAnswer

\item Use macros to select the second and fourth columns and copy
them to a new file `data-2.txt'.  Use ``:'' in `data-2.txt' to
separate the two columns.  The macro should copy one line at a 
time.  The selection of the second and fourth column should occur
within the macro.  When you finish defining the macro, use the
repeat command to run the macro on the remaining lines.

\BeginAnswer
\\
First, open emacs by typing `emacs' at the command-line.  Open a
buffer for the file `data.txt' by typing C-x C-f and typing in
the filename.  Then split the window by typing C-x 2.  Type C-x o
to move to the next window.  Type C-x C-f and type in `data-2.txt'.
Then move back to the previous window using C-x o.

Now begin the macro using C-x (.
\begin{itemize}
\item Type C-s and `:' to search for the first `:'
\item Type C-$<$space$>$ to start copying column 2
\item Type C-s C-s to search for next `:'
\item Type left arrow to move back one character
\item Type M-w to copy column 2
\item Type C-x o to move to the second buffer (`data-2.txt')
\item Type C-y to paste text into second buffer (`data-2.txt')
\item Type `:' (the column delimiter)
\item Type C-x o to move back to first buffer (`data.txt')
\item Type C-s C-s to search for the next `:'
\item Type C-$<$space$>$ to start copying column 2
\item Type C-s C-s to search for the next `:'
\item Type left arrow to move back one character
\item Type M-w to copy column 4
\item Type C-x o to move to the second buffer (`data-2.txt')
\item Type C-y to paste text into second buffer (`data-2.txt')
\item Type $<$enter$>$ to move the cursor to the start of the next line
\item Type C-x o to move back to first buffer (`data.txt')
\item Type C-n to move to the next line
\item Type C-a to move to the first character
\end{itemize}
Type C-x ) to finish the macro.

To reuse the macro, type C-x e.  To repeat the macro many times, type
C-u $<$num$>$ C-x e.\\
\EndAnswer

\item There is a makefile in the current directory.  Use the emacs
compile mode to run make (not in the shell mode).  In the compile 
command, set the compiler flags to ``-O1 -g''.

\BeginAnswer
\\
Type M-x compile.  At the prompt in the minibuffer, type 
`make CFLAGS=``-O1 -g'''.\\
\EndAnswer

\item Set the keyword color in emacs to yellow.  Do this first
using a command in the current session, then add a command to
your .emacs file so that the change will persist.

\BeginAnswer
\\
To set the keyword color in the current session do the following:
Type M-x font-lock-mode; then type M-x set-face-foreground; at the
prompt, type font-lock-keyword-face; then type yellow.

To set the keyword color permanently in the .emacs file add the
following two lines:\\
(global-font-lock-mode t)\\
(set-face-foreground 'font-lock-keyword-face ``yellow'')\\
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$24 Points$]$ Scripting.

\begin{enumerate}

\item In `convert-iphoto.sh' from homework 4, exercise 4(b), (use the
script in the solutions, if yours had mistakes) add an optional test 
flag.  `./convert-iphoto.sh hpca05 test' should print out the move
and sed commands that will be executed rather than actually executing
them.  Your output, when testing should look like the following.\\
\\
\begin{verbatim}
mv hpca05/hpca05.html hpca05/index.html

cat hpca05/hpca05-Pages/Image0.html |
  sed -e "s/hpca05\.html/index.html/" > tempfile
mv tempfile hpca05/hpca05-Pages/Image0.html
cat hpca05/hpca05-Pages/Image1.html |
  sed -e "s/hpca05\.html/index.html/" > tempfile
mv tempfile hpca05/hpca05-Pages/Image1.html
cat hpca05/hpca05-Pages/Image10.html |
  sed -e "s/hpca05\.html/index.html/" > tempfile
...
\end{verbatim}

\BeginAnswer
\begin{verbatim}
#!/bin/bash

DIRNAME=$1
TEST=$2

if [ -z $TEST ]
then
    TEST="foo"
fi

if [ -z "$DIRNAME" ] || [ $TEST != "foo" -a $TEST != "test" ] || [ -n "$3" ]
then
    echo "Usage: convert-iphoto.sh <dirname> [test]"
    exit 1
fi

for filename in $(find $DIRNAME -name \*.html)
do
    if [ $TEST == "test" ]
    then
        echo "cat $filename|sed -e \"s/$DIRNAME\.html/index.html/\" > tempfile"
        echo "mv tempfile $filename"
    else
        cat $filename|sed -e ``s/$DIRNAME\.html/index.html/'' > tempfile
        mv tempfile $filename
    fi
done

if [ $TEST == "test" ]
then
    echo "mv $DIRNAME/$DIRNAME.html $DIRNAME/index.html"
else
    mv $DIRNAME/$DIRNAME.html $DIRNAME/index.html
fi

exit 0
\end{verbatim}
\EndAnswer

\item Write a script `add.sh' that takes two arguments.  Each argument
can be either a number or a script.  If both arguments are numbers then
`add.sh' outputs the sum of the two numbers.  If one or both arguments
is a string then `add.sh' concatenates the two numbers and outputs the
resulting string.  For the numeric addition, your script should use
the `expr' command (`man expr' for more information).  Below are some 
examples of using `add.sh'.\\
\\
bash\$ ./add.sh 1 2\\
3\\
bash\$ ./add.sh 1 foo\\
1foo\\
bash\$ ./add.sh foo goo\\
foogoo\\

\BeginAnswer
\begin{verbatim}
#!/bin/bash

if [ -z "$1" ] || [ -z "$2" ] || [ -n "$3" ]
then
    echo "Usage: add.sh <number or string> <number or string>"
    exit 1
fi

if [ $1 -eq $1 2> /dev/null ] && [ $2 -eq $2 2> /dev/null ]
then
    expr $1 + $2
else
    echo "$1$2"
fi

exit 0
\end{verbatim}

\item Write the script from the previous exercise using perl.

\BeginAnswer
\begin{verbatim}
#!/usr/bin/perl

sub is_num {
  $arg = shift(@_);
  if ($arg =~ /^[-]?[0-9]+$/) {
    return 1;
  }
  return 0;
}

if (!@ARGV[0] || !@ARGV[1] || @ARGV[2]) {
  print "Usage: add.pl <number or string> <number or string>\n";
  exit 1;
}

if (is_num(@ARGV[0]) && is_num(@ARGV[1])) {
  $sum = @ARGV[0] + @ARGV[1];
  print "$sum\n";
}
else {
  print "@ARGV[0]@ARGV[1]\n";
}

exit 0;
\end{verbatim}
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$15 Points$]$ Important Applications.

\begin{enumerate}

\item Write up the exam in latex, using the files `final.tex' and
`misc.tex'.

\BeginAnswer
\\
See the file `final.tex'.\\
\EndAnswer

\item Create a new CVS repository that contains the files `final.tex'
and `misc.tex'.  Check out a new copy and work from these copies.

\BeginAnswer
\begin{verbatim}
bash$ CVSROOT=/home1/m/mcorliss/cvs/
bash$ cvs import -m "Importing sources" classes/cse399-final mcorliss start
bash$ cvs checkout classes/cse399-final
\end{verbatim}
\EndAnswer

\item Commit some new changes to `final.tex'.  

\BeginAnswer
\begin{verbatim}
bash$ cvs commit -m "Changed one line" final.tex
\end{verbatim}
\EndAnswer

\item CVS (like the shell) contains a diff command.
Use CVS diff to compare the new version and the previous version 
of the file `final.tex'.

\BeginAnswer
\begin{verbatim}
bash$ cvs diff final.tex
\end{verbatim}
\EndAnswer


\item CVS logs all operations.  Print out the log for the file `final.tex'.

\BeginAnswer
\begin{verbatim}
bash$ cvs log final.tex
\end{verbatim}
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

About this assignment.

\begin{enumerate}

\item Approximately, how long did it take you to complete this homework?

\item Would you classify this assignment as easy, straight-forward,
or difficult?

\end{enumerate}



\end{enumerate}

\end{document}

