\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes,chains,fit,shadows}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes.geometric}
\usepackage{multicol}

\usepackage{algorithm2e}
\usepackage{pgfkeys}

\pgfkeys {%
  /tikz/oval/.style={%
    shape=ellipse,
    inner sep=1pt,
    drop shadow,
    draw = black
  },
  /tikz/redbox/.style={%
    shape=rectangle,%
    minimum size=1.5em,%
    top color = white, %
    bottom color=red!50!black!50,
    inner sep=0pt,
    draw=black
  },
  /tikz/null/.style={%
    shape=circle,
    minimum size=0.5em,
    draw=black,
  },
  /tikz/greybox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black!20,
    inner sep=0pt,
    draw=black
  },
  /tikz/blackbox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black,
    inner sep=0pt,
    draw=black
   },
  /tikz/whitebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    fill=blue!40,
    draw=black
  },
  /tikz/whitetape/.style={
    shape=rectangle,
    minimum width=4em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluearr/.style={
    ->,
    line width=0.25em, 
    draw=blue!40
  },
  /tikz/greenarr/.style={
    ->,
    line width=0.25em, 
    draw=green!40!black!60
  }
}
    
\newcommand{\arraythree}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1, right=of a0#3](a1#3){#2};
    \node[#1, right=of a1#3](a1#3){#2};
  \end{tikzpicture}
}

\newcommand{\arraytwo}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1,right=of a0#3]{#2};
  \end{tikzpicture}
}
  
\newcommand{\Array}[0]{
  \arraythree{box}{}{a}{1}
}

\newcommand{\AArray}[0]{%
  \arraythree{bbox}{\Array}{b}{0.5}}
\newcommand{\AAArray}[0]{%
  \arraytwo{bbbox}{\AArray}{c}{0.25}}

    
    
\newcommand{\T}[1]{\ensuremath{T\left(#1\right)}}  
\newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}}
\title{Fundamentals of Computing: Lecture 38}
\date{November 11, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}


\begin{frame}
  \frametitle{Summary of last class}
  \begin{itemize}
  \item Simple shell program.
    \begin{tabbing}\tt
~\char36{}~cmd~arg1~arg2~arg3
\end{tabbing}
    \pause
  \item \text{\tt stdin}/\text{\tt stdout}/\text{\tt stderr} redirection 
    \pause
    \begin{tabbing}\tt
~\char36{}~cmd~arg1~arg2~\char60{}foo~~~\char35{}~get~input~from~foo\\
\tt ~\char36{}~cmd~arg~\char62{}bar
\end{tabbing}
    \pause
    \begin{tabbing}\tt
~\char36{}~cmd~2\char62{}\char38{}1~\char124{}~less~~~~~~\char35{}~0\char45{}stdin~1\char45{}stdout~2\char45{}stderr~\\
\tt ~\char36{}~cmd~\char62{}\char62{}foo~~~~~~~~~~~~\char35{}~append~to~the~file~foo~instead~of~write
\end{tabbing}
    \pause
  \item Pipes 
    \begin{tabbing}\tt
~\char36{}~cmd1~\char124{}~cmd2~\char124{}~cmd3~\char124{}~cmd~
\end{tabbing}
\end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Shell variables}
  
  To assign a shell variable
  \begin{tabbing}\tt
~\char36{}~foo\char61{}bar~~~~\char35{}~no~space~between~foo~and~bar
\end{tabbing}
  \pause
  To get the value of foo use \text{\tt \char36{}foo}
  \begin{tabbing}\tt
~\char36{}~echo~the~value~of~variable~foo~is~\char36{}foo
\end{tabbing}
  \pause
  \begin{block}
    {Some important shell variables}
    \begin{itemize}
    \item \text{\tt PATH} The directories where an executable is searched
      \begin{tabbing}\tt
~\char36{}~export~PATH\char61{}\char47{}bin\char47{}\char58{}\char47{}usr\char47{}bin\char58{}\char47{}usr\char47{}local\char47{}bin\char58{}
\end{tabbing}
    \item \text{\tt PS1} The first prompt
    \item \text{\tt PS2} the second prompt etc
    \end{itemize}
  \end{block}
\end{frame}
\begin{frame}
  \frametitle{Funny characters}
  
  Shell interpretes some characters differently 
  We have already seen some \text{\tt \char60{}}, \text{\tt \char62{}}, \text{\tt \char38{}}, \text{\tt ~\char124{}~} etc.
  \pause
  The character '*' means any sequece of characters
  \pause
  \begin{tabbing}\tt
~\char36{}~ls~\char42{}\char46{}c\\
\tt ~\char36{}~rm~\char42{}\char46{}o
\end{tabbing}
\end{frame}
\begin{frame}
  \begin{block}{Protecting characters}
    \begin{itemize}
    \item Use \text{\tt \char92{}} to protect funny characters
      \pause
      \begin{tabbing}\tt
~\char36{}~ls~filename\char92{}~with\char92{}~spaces\\
\tt ~\char36{}~echo~I~am~an~invisible~file~\char62{}~\char92{}~
\end{tabbing}
      \pause
    \item Quoting with \text{\tt \char39{}} 
      \pause
      \begin{tabbing}\tt
~\char36{}~ls~\char39{}filename~with~spaces\char39{}\\
\tt ~\char36{}~rm~\char39{}\char42{}\char39{}~~~\char35{}~actually~removes~a~file~called~\char42{}\\
\tt ~\char36{}~echo~I~am~an~invisible~file~\char62{}~\char39{}~\char39{}
\end{tabbing}
 \pause     
    \item Double quoting \text{\tt \char34{}}. Similar to \text{\tt \char39{}} but shell variables expand
      \begin{tabbing}\tt
~\char36{}~foo\char61{}bar\\
\tt ~\char36{}~echo~\char39{}\char36{}foo~is~the~value~of~foo\char39{}\\
\tt ~\char36{}~ech~\char34{}\char36{}foo~is~the~value~of~foo\char34{}
\end{tabbing}
    \end{itemize}
    
  \end{block}
    
\end{frame}

\begin{frame}
  \frametitle{The program \text{\tt grep}}
  grep is a \emph{filter}. It sends only those lines where a
  given pattern matches.
  \pause
  \begin{tabbing}\tt
~~~~~grep~PATTERN~file1~file2~file3\\
\tt ~~~~~grep~PATTERN
\end{tabbing}
  
  The pattern can be what is called a regular expression
  \pause
  eg. Print all the hidden files (files with name staring with a .)
  \pause
  \begin{tabbing}\tt
~~~ls~\char124{}~grep~\char39{}\char94{}\char92{}\char46{}\char46{}\char42{}\char39{}~\char124{}~less~\char35{}~
\end{tabbing}
  \pause
  \begin{itemize}
  \item \text{\tt \char94{}} means start of the line
  \item \text{\tt \char46{}} means any character
  \item \text{\tt r~\char42{}} means many \text{\tt r}'s
  \item I have written the \text{\tt \char92{}\char46{}} to \emph{escape} the special meaning
  \end{itemize}
\end{frame}

\end{document}





\begin{frame}
  \frametitle{Philosophy of shell programming} 

  There any many small but efficient programs.  Combine them.
  
  You need to know many of these common programs.
  
  \begin{itemize}
  \item \text{\tt echo} prints its command line arguments
    \pause
  \item \text{\tt less} shows int \text{\tt stdin} in pages
    \begin{tabbing}\tt
~~~~~~~\char36{}~cmd~\char124{}~less\\
\tt ~~~~~~~\char36{}~less~foo~bar~biz
\end{tabbing}
    \pause
  \item \text{\tt grep~pattern} shows only those lines of its stdin that
    matches the pattern.
  \end{itemize}
  \pause
  There are many more. 
  \pause
  To learn more, use command prompt always and read manpages.
\end{frame}
