%% $Header: /cvsroot/lcdp/lpic/general-linux-1/slides/gl1.103.4.slides.tex,v 1.2 2003/05/29 14:10:18 geoffr Exp $ \input{gl1.slide-header.tex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %----10->|-----20->|-----30->|-----40->|-----50->|-----60->|-----70->|-----80-> \begin{slide} %================================================================ \begin{center} \LARGE \textsf{-- General Linux 1 -- \\ Use Streams, Pipes, and Redirects} \LARGE \textsf{[3] } \\[2mm] \large \textsf{(Linux Professional Institute Certification)}\\[1mm] \normalsize\end{center} \footnote{Copyright \copyright\ 2002,2003 Geoffrey Robertson. Permission is granted to make and distribute verbatim copies or modified versions of this document provided that this copyright notice and this permission notice are preserved on all copies under the terms of the GNU General Public License as published by the Free Software Foundation---either version 2 of the License or (at your option) any later version.} \scriptsize \begin{verbatim} .~. Prepared by Andrew Eager /V\ // \\ geoffrey robertson @._.@ geoffrey@zip.com.au \end{verbatim} \tiny \begin{verbatim} $Id: gl1.103.4.slides.tex,v 1.2 2003/05/29 14:10:18 geoffr Exp $ \end{verbatim} \normalsize \vfill \end{slide} %----------------------------------------------------------- %----10->|-----20->|-----30->|-----40->|-----50->|-----60->|-----70->|-----80-> %============================================================================== \begin{slide}{} %ghr \Slidecontents \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide} %ghr \slideheading{(1.3) GNU and UNIX Commands [30]} \begin{description} \item[1.103.1] Work on the command line [4] \item[1.103.2] Process text streams using filters [7] \item[1.103.3] Perform basic file management [2] \item[1.103.4] {\bf \underline{Use streams, pipes, and redirects [3]}} \item[1.103.5] Create, monitor, and kill processes [7] \item[1.103.6] Modify process execution priorities [2] \item[1.103.7] Search text files using regular expressions [3] \item[1.103.8] Perform basic file editing operations using vi [2] \end{description} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide} %ghr \Slideheading{} \slidesubheading{Objective} Candidate should be able to redirect streams and connect them in order to efficiently process textual data. Tasks include redirecting standard input, standard output, and standard error, piping the output of one command to the input of another command, using the output of one command as arguments to another command and sending output to both stdout and a file. \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide} %ghr \Slideheading{} \slidesubheading{Key files, terms, and utilities} \begin{alltt} tee xargs < << > >> | `` \end{alltt} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide} %ghr \Slideheading{} \slidesubheading{Resources of interest} \begin{alltt} \end{alltt} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{STDIN, STDOUT \& STDERR} \begin{itemize} \item When a process is run it needs 3 things: \begin{itemize} \item An input device (ie a keyboard) \item An output device (ie a screen) \item An error device - somewhere to send critical errors (normally the screen) \end{itemize} \item Every process has 3 \textit{file descriptors} \begin{itemize} \item fd 0 is for input \item fd 1 is for normal output \item fd 2 is for error/abnormal output \end{itemize} \item By default these devices all default to your current tty \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Default File Descriptor Assignments} \fbox{\epsfig{file=../images/redir-a.eps,height=.5\textheight}} \begin{itemize} \item fd 0 == stdin (keyboard) \item fd 1 == stdout (screen) \item fd 2 == stderr (screen) \end{itemize} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Redirection \& Duplication Operators} \begin{itemize} \item There are 3 operators used for redirection: \begin{itemize} \item File redirects: (\textless, \textgreater and \textgreater\textgreater operators) \item Pipelines (\textbar operator) \item File descriptor duplication (\textgreater\& operator) \end{itemize} \end{itemize} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{File Redirect Operators} Each of the 3 file descriptors can be redirected to/from files as follows: \fbox{\epsfig{file=../images/redir-b.eps,height=.5\textheight}} Note that the redirect operators work with the file descriptors (0, 1 or 2) and \textit{not} with the physical device itself (/dev/tty). \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Pipeline Redirect Operator} Consider the command \texttt{cmd1 | cmd2}. The pipe operator takes data sent to stdout by cmd1 and sends it to the input file descriptor (fd 0) of cmd2: \fbox{\epsfig{file=../images/redir-c.eps,width=.9\textwidth}} Note that the pipe connects stdout (which may or may not be associated with fd 1) to fd 0 of the next command. \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Example of the Pipeline Operator} \fbox{\epsfig{file=../images/redir-d.eps,width=.9\textwidth}} \begin{center} \textit{Piping output of cmd1 into input of cmd2} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{File Descriptor Duplication Operator} A file descriptor can be made to be a copy of another descriptor. Consider the command \texttt{cmd 2\textgreater\&1}. This will make fd 2 become a copy of fd 1. \fbox{\epsfig{file=../images/redir-e.eps,width=.9\textwidth}} \begin{center} \textit{Duplicating a file descriptor} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Examples of File Output Redirection} For the following examples, we use the two example files \texttt{good} and \texttt{nofile} created thus: \cmd{echo "This is good" > good} \cmd{rm nofile} And to test what output is going where, we use the following command line: \cmd{cat good nofile} which will produce: \begin{verbatim} This is good (stdout) cat: nofile: No such file or directory (stderr) \end{verbatim} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Standard File Output Redirection} \begin{center} \fbox{\epsfig{file=../images/redir-f.eps,width=.8\textwidth}} \textit{Redirecting stdout \& stderr to different files} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Output Redirection - Two at once} \begin{center} \fbox{\epsfig{file=../images/redir-g.eps,width=.8\textwidth}} \textit{Redirecting stdout \& stderr at the same time} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Redirecting stdout \& stderr} \begin{center} \fbox{\epsfig{file=../images/redir-h.eps,width=.8\textwidth}} \textit{Redirecting stdout \& stderr to the same file} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Duplicating before \& after redirection} \begin{center} \fbox{\epsfig{file=../images/redir-i.eps,width=.65\textwidth}} \textit{Just when you duplicate the fd is significant} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Piping stdout to stdin} \begin{center} \fbox{\epsfig{file=../images/redir-j.eps,width=1.0\textwidth}} \textit{Normal pipe from cat to sed} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Piping stdout \& stderr to stdin} \begin{center} \fbox{\epsfig{file=../images/redir-k.eps,height=0.85\textheight}} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Piping stderr to stdin} \begin{center} \fbox{\epsfig{file=../images/redir-l.eps,height=0.85\textheight}} \end{center} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{File Redirection Summary} \begin{itemize} \item Redirect stdin from file: \begin{itemize} \item \cmd{cat < input.txt} \end{itemize} \item Redirect stdout to file: \begin{itemize} \item \cmd{cat good nofile > out.txt} \end{itemize} \item Redirect stderr to file: \begin{itemize} \item \cmd{cat good nofile 2> err.txt} \end{itemize} \item Redirect stdout \& stderr to file: \begin{itemize} \item \cmd{cat good nofile > out-err.txt 2>\&1} \begin{center} \textit{OR} \end{center} \item \cmd{cat good nofile 2> out-err.txt 1>\&2} \end{itemize} \end{itemize} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{File Redirection Summary} To append to a file instead of overwriting, simply replace \textgreater with \textgreater\textgreater \begin{itemize} \item Redirect stdout to file (append): \begin{itemize} \item \cmd{cat good nofile >> out.txt} \end{itemize} \item Redirect stderr to file (append): \begin{itemize} \item \cmd{cat good nofile 2>> err.txt} \end{itemize} \item Redirect stdout \& stderr to file (append): \begin{itemize} \item \cmd{cat good nofile >> out-err.txt 2>\&1} \begin{center} \textit{OR} \end{center} \item \cmd{cat good nofile 2>> out-err.txt 1>\&2} \end{itemize} \end{itemize} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{Pipe Redirection Summary} \begin{itemize} \item Pipe stdout to stdin: \begin{itemize} \item \cmd{cat good nofile | sed -n p} \end{itemize} \item Pipe stdout \& stderr to stdin \begin{itemize} \item \cmd{cat good nofile 2>\&1 | sed -n p} \end{itemize} \item Pipe stderr to stdin \begin{itemize} \item \cmd{cat good nofile 2>\&1 >/dev/null | sed -n p} \end{itemize} \end{itemize} \vfill \end{slide} %============================================================================== \begin{slide}{} %Andrew Eager \slideheading{A cool example - Swap stdout \& stderr} In this example, we are going to swap stdout \& stderr by using a temporary fd as a holding buffer: If we execute the command using a normal pipe: \cmd{cat good nofile | sed -n -p > stdout.txt} \texttt{cat: nofile: No such file or directory} Now if we swap stdout \& stderr: \cmd{cat good nofile 255>\&1 1>\&2 2>\&255 | sed -n -p > stdout.txt} \texttt{This is good} \vfill \end{slide} %============================================================================== \begin{slide}{} %ghr \heading{The End} \Slidecontents \vfill \end{slide} %----------------------------------------------------------- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %============================================================================== %============================================================================== \begin{slide}{} \slideheading{} \vfill \end{slide} %----------------------------------------------------------- ============================================================================= \begin{slide}{} \end{slide} %------------------------------------------------------------------