\documentclass{ictlab} \RCS $Revision: 1.1 $ \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}{SOLUTIONS\\Shell Programming---an Introduction} \renewcommand*{\subject}{Operating Systems and Systems Integration} \providecommand*{\RPM}{\acro{RPM}\xspace} \providecommand*{\CD}{\acro{CD}\xspace} \begin{document} \begin{enumerate} \item This is \texttt{hello1}: \begin{alltt} #! /bin/sh echo Hello $@ \end{alltt}%$ or (here is \texttt{hello2}): \begin{alltt} #! /bin/sh echo -n "Hello " for i do echo -n "$i " done echo \end{alltt}%$ \item Here is some typical output: \begin{alltt} $ \textbf{sh hello1 Nick Urbanik} Hello Nick Urbanik $ \textbf{sh -x hello1 Nick Urbanik} + echo Hello Nick Urbanik Hello Nick Urbanik $ \textbf{sh -x hello2 Nick Urbanik} + echo -n 'Hello ' Hello + echo -n 'Nick ' Nick + echo -n 'Urbanik ' Urbanik + echo $ \textbf{sh -v hello1 Nick Urbanik} #! /bin/sh echo -n "Hello " $ \textbf{sh -v ./hello2 Nick Urbanik} #! /bin/sh echo -n "Hello " Hello for i do echo -n "$i " done Nick Urbanik echo \end{alltt} From the manual page of \texttt{bash}: \begin{description} \item[\texttt{-v}] Print shell input lines as they are read. \item[\texttt{-x}] After expanding each simple command, display the expanded value of \texttt{PS4}, followed by the command and its expanded arguments. \end{description} \texttt{PS4} is a prompt used in tracing execution, which as you can see above, is normally set to ``\texttt{+\ }''. \item \begin{alltt} #! /bin/sh [ $# -ne 2 ] && echo $0 start finish && exit start=$1 end=$2 i=$start while [ $i -le $end ] do echo -n "$i " i=$(expr $i + 1) done echo \end{alltt} The question asks for a \texttt{while} loop, so let's use easier arithmetic evaluation notation: \begin{alltt} #! /bin/sh [ $# -ne 2 ] && echo $0 start finish && exit start=$1 end=$2 i=$start while (( i <= end )) do echo -n "$i " (( ++i )) done echo \end{alltt} Let's see an answer using the alternative \texttt{for} loop syntax with arithmetic evaluation: \begin{alltt} #! /bin/sh [ $# -ne 2 ] && echo $0 start finish && exit start=$1 end=$2 for (( i = start; i <= end; ++i )) do echo -n "$i " done echo \end{alltt} \item \begin{alltt} #! /bin/sh for file in *.rpm do rpm -K $file echo done \end{alltt}%$ \item \begin{alltt} #! /bin/sh # for file in r*.rpm emacs*.rpm for file in *.rpm do OUT=$(rpm -K $file) > /dev/null 2>&1 if echo $OUT | grep -v 'md5 gpg OK' > /dev/null 2>&1 then echo BAD $OUT else : # echo GOOD $OUT fi done \end{alltt} \item \begin{alltt} #! /bin/sh if ! grep admins /etc/group > /dev/null 2>&1 then groupadd admins done \end{alltt} % cut -b6-28 artificial-student-data.txt | grep -i 'chan[^a-z]' | sed 's/ *//' \end{enumerate} \end{document}