\documentclass{ictlab}

\RCS $Revision: 1.2 $

\usepackage{alltt,key,xr,cols}
\externaldocument[lt-]%
{../../linux_training-plus-config-files-ossi/build/masterfile}

\ifx\pdftexversion\undefined \else
  \usepackage[pdfpagemode=None,pdfauthor={Nick Urbanik}]{hyperref}
\fi

\newcommand*{\labTitle}{Basic Shell Programming Exercises}
\renewcommand*{\subject}{Operating Systems and Systems Integration}

\providecommand*{\RPM}{\acro{RPM}\xspace}
\providecommand*{\CD}{\acro{CD}\xspace}

\begin{document}
%\Large
\section{Questions}
\label{sec:questions}

Make all these scripts executable programs on your \texttt{PATH}.
\begin{enumerate}
\item Write a simple shell script that takes \emph{any} number of
  arguments on the command line, and prints the arguments with ``Hello
  '' in front.  For example, if the name of the script is
  \texttt{hello}, then you should be able to run it like this:
\begin{verbatim}
$ hello Nick Urbanik
Hello Nick Urbanik
$ hello Edmund
Hello Edmund
\end{verbatim}
  
\item \label{que:jot}Write a simple shell script that takes two
  numbers as parameters and uses a \texttt{while} loop to print all
  the numbers from the first to the second inclusive, each number
  separated only by a space from the previous number.  Example, if the
  script is called \texttt{jot}, then
\begin{verbatim}
$ jot 2 8
2 3 4 5 6 7 8
\end{verbatim}%$

\item Write a script which displays ``\texttt{Good morning}'',
  ``\texttt{Good afternoon}'' or ``\texttt{Good evening}'', on the
  monitor, depending on the time of running the script.

\item Write a script which reads a number in units of seconds and
  converts it to the units hours:minutes:seconds and prints the result
  to standard output.
  
  Your script must prompt for re-input if a negative value is input
  \begin{alltt}
Enter number of seconds: 12345
Result:
12345 seconds in hours:minutes:seconds is 3:25:45
  \end{alltt}
  
\item Suppose that the script you wrote for question~\ref{que:jot} is
  called \texttt{jot}.  Then run it calling \texttt{sh} yourself.
  Notice the difference:
\begin{verbatim}
sh jot 2 5
sh -v jot 2 5
sh -x jot 2 5
\end{verbatim}
Do you notice any difference in the output from last two?
  
\item Write a script \texttt{calculate}, which accepts 4 arguments \emph{a},
  \emph{b}, \emph{c}, \emph{d} and prints the value of $a \times
  20-b\times 2+c\div d$ to standard output.

  An example of executing the script:
  \begin{alltt}
$ \textbf{calculate 2 12 5 2}
The value of "2*20 - 12*2 + 5/2" is 18
  \end{alltt}%$

\item Write a shell script that, for each \texttt{.rpm} file in the
  current directory, prints the name of the package on a line by
  itself, then runs \texttt{rpm -K} on the package, then prints a
  blank line, using a \texttt{for} loop.
  
  Test your script on the files in
  \texttt{/home\allowbreak/nfs\allowbreak/rh-7.2-updated\allowbreak%
    /RedHat\allowbreak/RPMS}\@.
  
  \begin{explanation}
    The option \texttt{rpm -K} chec\textbf{k}s that the software
    package is not corrupted, and is signed by the author, if you have
    imported the author's public key with the command:
    \begin{alltt}
$ \textbf{cd /home/nfs/redhat-8.0}
$ \textbf{sudo rpm --import RPM-GPG-KEY}
    \end{alltt}
  \end{explanation}
  
\item Modify the script you wrote for the previous question to print
  the output of \texttt{rpm -K} \emph{only} for \emph{all} the files
  that fail the test.  In particular, if the package's \acro{GPG}
  signature fails, then your script should display the output of
  \texttt{rpm -K}\@.  There are at least two packages in this
  directory which do not have a valid \acro{GPG} signature; one of
  them is \texttt{redhat-release-7.2-1.noarch.rpm}; what is the other?

  Here is output from \texttt{rpm -K} for two packages, one with no
  \acro{GPG} signature, the other with:
\begin{verbatim}
$ rpm -K redhat-release-7.2-1.noarch.rpm bash-2.05-8.i386.rpm
redhat-release-7.2-1.noarch.rpm: md5 OK
bash-2.05-8.i386.rpm: md5 gpg OK
\end{verbatim}%$

  Test it in the same network directory as for
  the previous question.

\item Write a shell script to add a local group called \texttt{administrator}
  if it does not already exist.  Do not execute any external program
  if the \texttt{administrator} group already exists.

\item Download a copy of the bogus student registration data from
  \url{http://ictlab.tyict.vtc.edu.hk/snm/lab/regular-expressions/artificial-student-data.txt}.
  Use this for the following exercises, together with the
  \texttt{grep} program:
  \begin{enumerate}
  \item Search for all students with the name ``CHAN''

  \item Search for all students whose student number begins and ends
    with 9, and with any other digits in between.

  \item Search for all student records where the Hong Kong ID has a
    letter, not a number, in the parentheses.
    
  \item Do the same exercises, but display only the students' names,
    or student number.  You will need a program such as \texttt{awk}
    (or even \texttt{cut}) to select the appropriate columns from the
    output of \texttt{grep}.
  \end{enumerate}
  
\item Write a shell script to take a file name on its command line,
  and edit it with \texttt{sed} so that every instance of
  ``\texttt{/usr/local/bin}'' is changed to ``\texttt{/usr/bin}''
  
\item Write a shell script to take a file name on its command line,
  and edit it using \texttt{sed} so that every line that begins with
  the string \texttt{server}:
\begin{alltt}
server \emph{other text}
\end{alltt}
is edited so that averything after ``\texttt{server~}'' (i.e., the
``\texttt{\emph{other text}}'') is replaced with the string
``\texttt{clock.tyict.vtc.edu.hk}'', so that the line above looks like this:
\begin{alltt}
server clock.tyict.vtc.edu.hk
\end{alltt}
Test this on a copy of the file \texttt{/etc/ntp.conf} that is on your
computer.  (Install the package \texttt{ntp} if it is not there).
\end{enumerate}
\end{document}
