\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=0.75in]{geometry}

% set up hyperlink coloring
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan
}

\setlength{\parindent}{0pt} % no indentation

% checkbox list: https://tex.stackexchange.com/questions/247681/how-to-create-checkbox-todo-list
\usepackage{enumitem,amssymb}
\newlist{boxlist}{itemize}{2}
\setlist[boxlist]{label=$\square$}

% code support: listings and fancy verbatim
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize}
\usepackage{fancyvrb}

\renewcommand{\familydefault}{\sfdefault} % default to sans serif

\usepackage{xcolor}
\newcommand{\code}[1]{\colorbox{lightgray}{\texttt{#1}}}
\newcommand{\sh}[1]{\code{\$ #1}}

%%%%%%%%%%%%%%%%
\begin{document}

\title{Basic 3\\Unix and the Shell}
\author{EECS 201 Fall 2020}
\date{}
\maketitle

\section*{Submission Instructions}
Answer the bolded text that begins with a ``\textbf{Q}''.
This assignment is an ``online assignment'' on \href{https://gradescope.com}{Gradescope},
where you fill out your answers directly.


\section*{Preface}
This assignment should be fine on Linux and macOS. That being said, if you
run into issues on macOS, like a command completely not running as expected,
try using your Ubuntu 20.04 VM.


\section{Redirection}
Recall from lecture that we can \emph{redirect} the inputs and outputs of a command.
\code{echo} is a command that will print out a string (taking into account any
expansions you embed in it). For example, I could use \code{echo "Welcome \$HOME"}
to print out \code{Welcome /home/brandon}. Try modifying this command so that
it saves that output to a file.
\\\\
Suppose I had an application \code{app} that read data from standard
input, and produces its output on standard output and reports error messages on
standard error. Let's say we have a file called \code{input.dat} in the current directory.
Assume that \code{app} is in the \code{PATH}.
\\\\
\textbf{Q: Write a command that feeds \code{app} data from \code{input.dat}, saves
        the output to \code{output.dat}, and saves the error messages to \code{log.txt}}


\section{Pipeline}
Recall from lecture that commands can be chained together to form a pipeline,
where one process passes its output to the input of the next.
For example, I could create this pipeline \code{cat file1 file2 file3 | rev}
to concatenate three files and output the result with \code{cat} and then reverse the
output of \code{cat} with \code{rev}.
\\\\
\textbf{Q: Using only the commands below, create a pipeline that creates a sorted list of the user's
        currently running \emph{commands} with no repeats. As you do this question, try testing out your pipeline
        by running it.}
\begin{itemize}
  \item \code{awk '\{print \$11\}'}: This will print out the 11th column of each line of input
  \item \code{ps ux}: This will list out all of the user's processes. The 11th column is the command column. The first line of output is the header line, providing labels for each column. \textbf{You probably wouldn't care about this header line}.
  \item \code{sort}: This will sort inputted lines and output them in a sorted order
  \item \code{tail -n +2}: This will print the input from the second line onwards (\code{tail} prints the ends of file; with these arguments we can specify where to start printing from)
  \item \code{uniq}: This will take inputted lines and omit repeated lines: for example if ``hello world'' occurs on three \textbf{adjacent} lines, only one ``hello world'' is outputted.
\end{itemize}


\newpage
\section{Variables and expansion}
Recall that there's two kinds of variables: environment variables, part of the ``metadata'' for each process,
and shell variables, variables that live inside the data for the shell program itself.

To use them, we use ``expansion'' to replace instances of \code{\$VARNAME} with the value of the variable
(environment or shell) in a command before it gets executed.
There's also ``command substitution'' which allows us to do a similar thing, except with commands instead of variables.
For example, \code{echo \$(ls)} could result in the following expansion \code{echo file1 file2 file3}.
\subsection{}
\textbf{Q: Suppose you wanted to print out the value of the variable \code{PATH}. What command would you type out and run?}
\subsection{}
\textbf{Q: Write a command that saves the output of \code{git status} to \emph{shell} variable \code{gs}}



\section{Control flow}
I want to re-emphasize that the conditionals for the if-elif-else blocks, while loops, and until loops
are \emph{commands}: they aren't any special kind of syntax. Don't believe me?
Try running this Bash conditional command: \sh{[[ 100 -gt 200 ]]}.
This should have a exit status of 1, as 100 is actually less than 200; try checking this exit status.
\\
\textbf{As you do this part, remember that there's no difference between writing
these blocks in a script or entering them in line-by-line: you can test things
out at the command-line.}

\subsection{}
Suppose that a command has just run, thus setting the exit status \code{?} variable.
\\\\
\textbf{Q: Set a shell variable to save the exit status \code{?}:
        keep in mind that when commands are run, they update \code{?}, so directly using \code{?} can have some
        subtle effects...After this, write an if-elif-else block that \code{echo}es ``PASS'' if the exit status of the command was 0, ``FAIL'' if the
        exit status was 1, and ``OTHER'' otherwise.}

\subsection{}
The \code{seq} utility prints out a list of numbers.
\\\\
\textbf{Q: Write a for-loop using a \emph{command substitution} with \code{seq} that
        uses \code{touch} to creates 100 files, starting with \code{file1.txt}
        and ending with \code{file100.txt}}


\section*{Controlling your environment}
\emph{This section is ungraded and is more for your personal edification.}
Many shells have a configuration file that gets sourced when an instance
of the shell starts (i.e. when the instance starts up, it executes all commands in that file).
For example Bash has its \code{.bashrc} and Zsh has its \code{.zshrc}.
\\\\
Recall that the \code{PATH} variable is a colon-delimited list of
directories to look for executables to run.
\\\\
\textbf{Q: How would you add a directory \code{\$HOME/scripts} to your \code{PATH} so that
           each new instance of Bash has this updated \code{PATH}?}
\\\\
Another utility of shells is \code{alias}, which allows you to create aliases for commands.
Such aliases could serve as shorthand for some commonly used commands and their arguments.
For example, I have an alias on my WSL that \code{cd}s to my Windows user directory.
Try playing around with it!

\end{document}
