Commit 00b94ca6 authored by  Joe  Rackham's avatar Joe Rackham
Browse files

Shell scripting section

parent b7a14040
......@@ -13,3 +13,10 @@
\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.2.1}Special Characters}{3}}
\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.2.2}Character Classes}{3}}
\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.2.3}Line and Word Anchors}{3}}
\@writefile{toc}{\contentsline {section}{\numberline {6}Shell Scripting}{4}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.1}Variables}{4}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.2}Choice}{4}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.3}Iteration}{5}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.4}Exit Codes}{5}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.5}Test}{5}}
\@writefile{toc}{\contentsline {subsection}{\numberline {6.6}Functions}{5}}
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=pdflatex 2020.3.23) 30 MAR 2020 09:50
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=pdflatex 2020.3.23) 30 MAR 2020 12:28
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
......@@ -229,30 +229,44 @@ LaTeX Font Info: Font shape `OMS/cmr/m/n' in size <10> not available
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] [2]
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line 144.
(Font) <7> on input line 145.
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line 144.
[3] (./main.aux) )
(Font) <5> on input line 145.
[3]
LaTeX Font Info: Try loading font information for OMS+cmtt on input line 171
.
LaTeX Font Info: No file OMScmtt.fd. on input line 171.
LaTeX Font Warning: Font shape `OMS/cmtt/m/n' undefined
(Font) using `OMS/cmsy/m/n' instead
(Font) for symbol `textbraceleft' on input line 171.
[4] [5] (./main.aux)
LaTeX Font Warning: Some font shapes were not available, defaults substituted.
)
Here is how much of TeX's memory you used:
2109 strings out of 494923
30106 string characters out of 6180742
2114 strings out of 494923
30155 string characters out of 6180742
92576 words of memory out of 5000000
5410 multiletter control sequences out of 15000+600000
5413 multiletter control sequences out of 15000+600000
8063 words of font info for 29 fonts, out of 8000000 for 9000
14 hyphenation exceptions out of 8191
41i,6n,32p,271b,236s stack positions out of 5000i,500n,10000p,200000b,80000s
</usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/c
m/cmbx10.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx
12.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi10.pfb
></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/
share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb></usr/share/t
exlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr17.pfb></usr/share/texlive/
texmf-dist/fonts/type1/public/amsfonts/cm/cmsy10.pfb></usr/share/texlive/texmf-
dist/fonts/type1/public/amsfonts/cm/cmtt10.pfb>
Output written on main.pdf (3 pages, 102919 bytes).
41i,6n,32p,271b,212s stack positions out of 5000i,500n,10000p,200000b,80000s
</usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx10.pfb></us
r/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx12.pfb></usr/shar
e/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi10.pfb></usr/share/texl
ive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/share/texlive/tex
mf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb></usr/share/texlive/texmf-dist
/fonts/type1/public/amsfonts/cm/cmr17.pfb></usr/share/texlive/texmf-dist/fonts/
type1/public/amsfonts/cm/cmsy10.pfb></usr/share/texlive/texmf-dist/fonts/type1/
public/amsfonts/cm/cmtt10.pfb>
Output written on main.pdf (5 pages, 114494 bytes).
PDF statistics:
46 PDF objects out of 1000 (max. 8388607)
32 compressed objects within 1 object stream
52 PDF objects out of 1000 (max. 8388607)
36 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 500000)
1 words of extra memory for PDF output out of 10000 (max. 10000000)
No preview for this file type
No preview for this file type
......@@ -22,6 +22,7 @@
\end{itemize}
\item \texttt{man} will show the Linux manual page for any command
\item \texttt{!!} will is an alias for the previous commands
\item \texttt{which} will locate the executable behind a command
\end{itemize}
\section{Using the file-system}
......@@ -145,6 +146,115 @@
\item $/>$ - Anchors to the end of a word
\end{itemize}
\section{Shell Scripting}
\begin{itemize}
\item Shell scripts are made up of command line commands plus some additional syntax
\item Shell scripts should be started with \texttt{\#!/bin/sh}; instructing Unix to execute them with sh
\item Output directed to a null device with \texttt{>/dev/null 2>\&1} is effectively silenced
\item Backticks indicate the enclosed contents should be run as a command
\end{itemize}
\subsection{Variables}
\begin{itemize}
\item Variables are defined \texttt{VAR='Hello World'} and used \texttt{echo \$VAR}
\item \texttt{read VAR} will ask the user for the vars value
\item \texttt{\$0} is the name of the program
\item Variables \texttt{\$1, \$2, ... \$9} are reserved for the cmd line arguments
\begin{itemize}
\item \texttt{shift} moves all parameters down 1 allowing for scripts which take more than 9 cmd line params
\end{itemize}
\item \texttt{\$@} is all parameters and \texttt{\$\#} is the number of parameters
\item \texttt{\$?} is the exit code of the last run command
\item We can also \texttt{export} variables from the interactive terminal to make them accessible
\item By default scripts run in a new terminal; changes to exported variables aren't visible
\item The script can be sourced to the original terminal by running it \texttt{. ./myscript.sh}
\item \texttt{\$\{VAR\}} can be used to use a variable in file paths
\end{itemize}
\subsection{Choice}
\begin{itemize}
\item If statement:
\begin{verbatim}
if [ ... ]
then
# if-code
else
# else-code
fi
\end{verbatim}
\item Case Statement:
\begin{verbatim}
case $INPUT_STRING in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
break
;;
*)
echo "Sorry, I don't understand"
;;
esac
\end{verbatim}
\end{itemize}
\subsection{Iteration}
\begin{itemize}
\item For loop:
\begin{verbatim}
for i in 1 2 3 4 5
do
echo "Looping ... number $i"
done
\end{verbatim}
\item While loop:
\begin{verbatim}
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
\end{verbatim}
\item \texttt{:} always evaluates to true
\end{itemize}
\subsection{Exit Codes}
\begin{itemize}
\item An exit code of 0 is success, anything else is some form of failure
\item This property can be used to run commands in the while condition
\item \texttt{exit *code*} exits the program with a specified exit code
\end{itemize}
\subsection{Test}
\begin{itemize}
\item \texttt{test} is symlinked by the more commonly used \texttt{[} which can be used with a dummy close bracket for readabilty
\item Test is a powerful comparison utility
\item String comparison - \texttt{[ "hello" = "world" ]}
\item Number comparison - \texttt{[ 1 -eq 3 ]}
\item We can do JavaScript style short-circuiting of commands
\end{itemize}
\subsection{Functions}
\begin{itemize}
\item Shell functions cannot change their parameters
\item There is basically no scoping to variables
\item External library functions in another script can be included using sourcing; \texttt{. ./library.sh}
\item \begin{verbatim}
add_a_user()
{
USER=$1
PASSWORD=$2
# ...
}
\end{verbatim}
\end{itemize}
\end{document}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment