BeamerReveal
view release on metacpan or search on metacpan
test/2026-02-04-DRM/the_shell.tex view on Meta::CPAN
You have a CSV (\texttt{Name,Age,Score}) called \texttt{class.csv}.
\begin{enumerate}
\item Print only the names of students older than 20.
\item Calculate the total number of words in a text file.
\end{enumerate}
\end{block}
\begin{alertblock}<2->{Answers}
\begin{enumerate}[<+->]
\item \texttt{awk -F',' '\$2 > 20 \{print \$1\}' class.csv}
\item \texttt{awk '\{total +} NF\} END \{print total\}' file.txt=
\end{enumerate}
\end{alertblock}
\end{frame}
\begin{frame}[label={sec:orga55adc4},fragile]{More awk Examples}
\begin{block}{Advanced Features}
\begin{verbatim}
# Formatted printing (printf)
awk '{printf "Name: %-10s ID: %03d\n", $1, $2}' students.txt
# Length of string
awk 'length($0) > 80' file.txt # Print lines > 80 chars
# Logical conditions (AND/OR)
awk '$1 == "POST" && $9 == 200' access.log
# Range pattern (start, end)
awk '/START/,/END/' data.txt
# Filter by timestamp range and format output
awk '$1 >= "2026-01-20" && $1 <= "2026-01-25" \
{printf "[%s %s] %s\n", $1, $2, substr($0, index($0,$3))}' system.log
\end{verbatim}
\end{block}
\end{frame}
\section*{Shell Scripting}
\label{sec:orgd9b136e}
\begin{frame}[label={sec:org0714907},fragile]{Scripting Basics}
\begin{columns}
\begin{column}{0.45\columnwidth}
\begin{block}{}
\begin{itemize}
\item Automate repetitive tasks
\item Combine multiple commands
\item Add logic (if/loops)
\item Reproducibility
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{verbatim}
#!/bin/bash
# hello.sh
echo "Hello, $USER!"
echo "Date: $(date)"
\end{verbatim}
\end{column}
\end{columns}
\begin{exampleblock}{Making it Executable}\label{sec:orgaab8643}
\begin{verbatim}
chmod +x hello.sh
./hello.sh
\end{verbatim}
\end{exampleblock}
\end{frame}
\begin{frame}[label={sec:orgfdf37ff},fragile]{Variables in Scripts}
\begin{block}{Definition and Scope}
\begin{verbatim}
# Definition: No spaces around =
name="STIC Class"
count=42
# Access: Use the $ sign
echo "Welcome to $name"
# Command Substitution: Use $()
current_user=$(whoami)
\end{verbatim}
\end{block}
\begin{alertblock}{Important Rules}
\begin{itemize}
\item \alert{Quotes Matter:} Use \texttt{"\$var"} to prevent word splitting if your variable contains spaces.
\item \alert{Braces:} Use \texttt{\$\{var\}} for clarity or when appending text: \texttt{echo "\$\{name\}\_backup"}.
\end{itemize}
\end{alertblock}
\end{frame}
\begin{frame}[label={sec:orgea5b845},fragile]{Positional Arguments}
\begin{block}{Communicating with Scripts}
\begin{center}
\begin{tabular}{ll}
Variable & Description\\
\hline
\texttt{\$0} & The script name itself\\
\texttt{\$1, \$2..} & First, second arguments\\
\texttt{\$\#} & Number of arguments passed\\
\texttt{\$@} & All arguments as a list\\
\texttt{\$?} & Exit status of the \alert{last} command\\
\end{tabular}
\end{center}
\end{block}
\begin{exampleblock}{Practice: Arg Grep}\label{sec:org8b32bdb}
\begin{enumerate}
\item Create a script that takes a word as \texttt{\$1} and a file as \texttt{\$2} and greps for it.
\item \alert{Answer:} \texttt{grep "\$1" "\$2"}
\item Use \texttt{\$@} for looping over arguments.
\end{enumerate}
\end{exampleblock}
\end{frame}
\begin{frame}[label={sec:org3b60504},fragile]{Logic and Conditionals}
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{verbatim}
if [ "$1" -gt 10 ]; then
echo "Large"
elif [ -f "$1" ]; then
echo "It's a file"
else
echo "Small/Unknown"
fi
\end{verbatim}
\end{column}
( run in 0.956 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )