writeup.tex (15344B)
1 \documentclass[a4paper,12pt]{article} 2 3 \usepackage{geometry} 4 \usepackage{titling} 5 \usepackage{titlesec} 6 \usepackage[english]{babel} 7 \usepackage[hidelinks]{hyperref} 8 \usepackage{listings} 9 \usepackage{xcolor} 10 \usepackage{graphicx} 11 \usepackage[export]{adjustbox} 12 \usepackage{forest} 13 \usepackage{tikz-qtree} 14 \usepackage{bchart} 15 \usepackage[T1]{fontenc} 16 17 \titleformat{\section}{\Huge}{}{0em}{}[\titlerule] 18 \geometry{a4paper, total={170mm,257mm}, left=30mm, right=30mm} 19 20 \author{Lucas Standen} 21 \title{A parting gift of my knowledge} 22 23 \begin{document} 24 25 \maketitle 26 \newpage 27 \tableofcontents 28 29 \setlength{\parskip}{1em} 30 {\setlength{\parindent}{0cm} 31 32 \section{Headnote} 33 I leave this as a gift and thanks for all the things I learnt in your 34 class, hopefully you can learn something from this. This document 35 contains almost all the things I know about computer science; please 36 enjoy! Not much of this will be stuff needed to teach the course but I 37 still thought it would be nice to give you a wealth knowledge for the 38 fun of it; If knowing me for 2 years wasn't enough tell, doing things 39 for the fun of it is the best way to learn (in my opinion). 40 41 I will be laying this document out into sections on general 42 topics and then subsections of specifics, see the table of 43 contents to do further reading. 44 45 \section{The Unix wizard handbook} 46 \subsection{The history} 47 Unix was originally made at AT and T's bell labs, their research centre 48 at the time (It belongs to Nokia now). It was designed as a small and 49 lightweight OS for the 'weaker' computer at the site although it still 50 took up and entire room. It was made by Ken Thompson and Denis Richie in 51 the early 70's. They built everything from the ground up for the system, 52 it was written in C, which they made specifically to write the system. 53 54 It is often cited, but we will never truly know, that Ken was 55 the person with all the ideas, and Denis was very good at 56 implementing them, so most of the code in the original Unix was 57 written by Denis. I don't know if there is anything relevant in 58 that, but I thought it was interesting. 59 60 Unix was sold to many university's in the late 70's and early 61 80's, and it was through this that it really started to gain 62 some traction with the students. Many developed Unix further, 63 because unlike modern paid software, Unix was distributed in 64 source form, meaning you could tinker it as much as you liked, 65 assuming you had brought a license. One of the main outcome of 66 this would be BSD (The Bearkly software distribution) version of 67 Unix. The people who worked on this system later went on to 68 develop Java (All of computer science is so interlinked, its a 69 small world!). 70 71 In the 80/90's Unix was de facto in Uni's but the free software 72 movement was starting to gain traction, and many re-writes or 73 clones of Unix were starting to show up, like GNU (fun fact, GNU 74 is a recursive acronym for GNU's not Unix!) 75 76 \subsection{The philosophy} 77 The main idea's behind Unix were that it needed to be small to run on 78 the 'weaker' computer at bell labs. One of the leading limitations with 79 this system was its small storage size of about 20Mb. To avoid this 80 being an issue they came up with the following philosophy to ensure all 81 software remained small, and simple even on the weaker computer. 82 83 \begin{description} \item[Small reusable tools] The idea was 84 that if 85 you made one small program that did one thing and provided the 86 ability to combine programs easily, you could let the user put 87 together it together to make the tools they needed to their own 88 needs. 89 \item[Everything is a file] This was the idea that if everything 90 is a file, from talking between systems, to talking to kernel, 91 you could avoid many different APIs that would repetitive, 92 instead all you needed was one single file and you could work 93 with the entire system. 94 \end{description} 95 96 Something that was more unintentional but still worth noting is the Unix 97 was designed for Teletypes, (paper printers that talked to computers), 98 that didn't have the ability to overwrite sections of the page, and 99 printed at a slow 10 characters per second. This effects a large amount 100 of the user interfaces for Unix. 101 102 \subsubsection{GNU/Linux} 103 One of the many outcomes of the free software movement, it quickly 104 became the easiest way for anyone to use Unix, releasing in 1991. There 105 were open and free versions available at the time, such as BSD, however 106 you still needed to be in a university to get a copy, as the internet 107 wasn't really active at this time. It is ironic that Linux became a core 108 part of this system, as it was in fact a small hobby project that the 109 creator Linus Torvalds said 'this is just a small thing, it won't go 110 anywhere'. 111 112 The GNU part of GNU/Linux is the user land utility's of the 113 system. Linux is only the kernel of the system, handling the low 114 level system, such as the file system, memory allocation and 115 process scheduler, the rest of the GNU/Linux system is the GNU 116 core utils, such as the GNU bash shell. 117 118 \subsection{The shell} 119 \subsubsection{The interactive shell} 120 The shell is the part of the system that you time commands into, it's a 121 whole programming language that can run line by line (if 122 it helps you think about it, its like using the python 123 shell in IDLE). 124 125 The shell was designed to be a helpful interface to take the 126 user in and out of programs, it is fast and simple. So here is a 127 whistle stop tour of all the things it does. 128 129 The shell can store variables that contain strings, these 130 strings can then be accessed by any program that is run in the 131 same shell that the variable is in (think of it like a 132 parent-child thing, the child process knows all the same things 133 as the parent shell). The can be created like so 'myvar="hello"' 134 and then can be accessed using a '\$' like so '\$myvar', this 135 syntax is also seen in Perl and php, which are often compared to 136 the Unix shell as they are all interpreted scripting languages. 137 138 On a Unix system applications are stored in /bin, /usr/bin, 139 /usr/local/bin, and \url{~/.local/bin} (and sometime other 140 places too). The shell will allow you to type a commands name, 141 like 'cp' (copy paste), and it will search through all the known 142 locations of programs and run the first one it finds. The shell 143 stores all the known locations of programs in a variable called 144 \$PATH. 145 146 Programs can be started in parallel using a '\&' sign, which can 147 be very helpful when testing networked software. 148 149 Modern shells such as Bash and Zsh provide autocompletion of 150 possible commands when tab is pressed, just like modern code 151 IDE's allowing you to auto complete code. 152 153 Commands are always processed in the following structure 154 COMMAND-NAME -ARG -ARG -ARG [FILE] [...] Some commands may take 155 a fixed amount of arguments, while others maybe more variable, 156 some may take in none. Most commands will perform some 157 operations on a file. 158 159 Almost every command can be run like so COMMAND-NAME -h or 160 COMMAND-NAME --help to get a list of available options and how 161 to use it. 162 163 Arguments that are only 1 letter long will usually only use a 164 single - and arguments that are words will usually use a double 165 --. 166 167 The shell has a current working directory, this is a folder that 168 it is currently navigated too. When typing commands, file 169 arguments can be given as a relative path to the current working 170 directory. For example 'cp file1 file2' will copy file1 to 171 file2, where both files are in the same directory. You can also 172 use absolute paths which always start with '/'. 173 174 There are 3 magic shell paths, ../ which is equivalent to the 175 parent folder of the current working directory, ./ which is the 176 current working directory and \url{~/} which is the currently 177 logged in users home director (where their documents, downloads, 178 videos, etc folders are stored). 179 180 \subsubsection{The common commands} 181 Items given in brackets are optional arguments 182 \begin{description} 183 \item{cp file1 file2} \\ cp will copy file 1 to the file 2, if file 2 184 already exists it is overwritten, if it doesn't exist created. The -r 185 option is often used to allow it to co instead of individual files. 186 187 \item{rm file1 [fileN]} \\ rm will delete any files provided, it 188 is often used with the -r flag like cp is removed folders as 189 well as files. The -f option is also used to avoid prompts of 190 weather you want to delete the just deleting it straight away 191 192 \item{mv file1 [fileN] file2} \\ mv moves 1 file to another 193 location, it is equivalent to copying a file then deleting 194 original. For some reason unlike rm and cp, it doesn't the -r 195 option to work with folders. If file2 is afolder (something 196 that ends in /) multiple files can be given and they will all 197 copy to the folder. 198 199 \item{cd [folder]} \\ cd will set the shell's current working 200 directory to the folder specified. You can use this wi magic 201 paths, for example cd .. will move you back one directory, and 202 cd \url{~} will move you to you home directory. Running cd 203 without any arguments is the same as cd \url{~} 204 205 \item{ls [folder]} \\ ls will show the folders and files that are 206 contained with in the folder provided. If no folder provide it will 207 default to the current working directory can use the -l option to get 208 file permissions and the -a show hidden files. (you can think of cd and 209 ls equivalent clicking on a folder in file explorer and observing th 210 contents). 211 212 \item{cat file1 [fileN]} \\ Cat is a simple command that will 213 print the contents o files given to it as arguments. 214 215 \item{grep regex [file]} \\ Grep is one of the larger commands 216 and has 217 many abilities however its main one is to search through a file 218 for a 219 expression. If no file is given to the command, it will read 220 from standard input (typing input) 221 \item {sed expression [file]} \\ sed is a find and replace of a 222 file (or stdin if no file is given), the expression should look 223 something like this. "s/word/newword/". the 's' at the start 224 tells you that it is in subsitution mode (other modes exist), 225 the word is the expression to search for, and the newword is wat 226 to replace it with. By default this will only substiute 1 word 227 per line, if you want to do it more times the g (global) flag 228 can be given like so, "s/word/newword/g". No matter how the file 229 is give, via stdin or a file name, the output will be printed to 230 stdout. 231 \end{description} 232 233 \subsubsection{Pipes and redirections} 234 This is where the real magic Unix starts, as previously mentioned, Unix 235 was designed with the id of making a small reusable tools, and this is 236 how the tools can work together. 237 238 One can take the output of one command pass it to another, or 239 copy the outputs to a file. (A commands input is what is typed 240 in by the user, and the commands output is what is printed to 241 the screen) This can be done like so. ls > files.txt This will 242 take the output of the 'ls' command and copy it to the file 243 'files.txt' (which is in the current working directory, we can 244 tell this because it doesn't have any '/' in its name). You can 245 also do: 246 247 ls >> files.txt 248 249 Which will do the same thing, however if the file already exists 250 it will append to it instead of truncating it. 251 252 This on its own is quite helpful, as many commands take files as 253 arguments, and this will allow to use one command to help 254 another (the '>' and '>>' is called a redirection in Unix). 255 256 One can also use a '|' to take the output of one command and use 257 it as the input to another. 258 259 Here is an example: 260 261 cat file.txt | grep [0-9]+ 262 263 This has taken the output of the cat command, and put put it 264 into the input of the grep command (as if you had typed what the 265 cat command printed), and has append a special EOF character to 266 the end of the cat command output, to specify the end of file. 267 268 Grep has then read this input, and performs an operation on it, 269 based on its arguments. 270 271 \subsubsection{Shell scripting} 272 This is where you take all the things you've used in the shell, and wr 273 ite them into a file, as simple as it gets really. The only notable 274 differences is the fact that you have to prefix a file with a special 275 string to say it is a shell script and make it executable. 276 277 This can be done like so: 278 \begin{lstlisting}[language=bash, caption=example.sh] 279 #!/bin/sh 280 sed "s/word/newword/g" myfile > tmp 281 myvar=$(grep "[0-9]+" tmp) 282 echo $myvar 283 \end{lstlisting} 284 285 This simple example will run sed, and put its output in tmp, then run 286 grep on the output and then echo the output of grep. The \$() syntax is 287 used t o specify run the following value as a command and put its output 288 here. A mixture of sed and grep can create some rather insane programs, 289 in 2023 i use these two tools to do almost all of advent of code, by 290 taking the inpu t and then manipulating into valid python code, which 291 then solved the task for me . Sadly i dont have the scripts I used 292 anymore, but there are some great videos of Russ Cox (co creator of the 293 GO programming language) doing the same thing 294 \url{https://www.youtube.com/watch?v=hmq6veCFo0Y}. 295 296 \subsection{Device files} 297 As mentioned prior, everything in unix is a file, this went all the wa y 298 down to devices and hardware attached to the system. Here is a quick 299 list of a ll the major files that you can use inside of programs to 300 communicate with th e hardware on a device. 301 \begin{description} 302 \item{/dev/random (/dev/urandom)} \\ These are both random text 303 generators that can be found on most unix systems. You can use commands 304 like head (another small unix tool that prints the first 10 lines of a 305 file), to read it like so 306 \begin{lstlisting}[language=bash, caption=/dev/random example] 307 key=\$(head /dev/random) 308 \end{lstlisting} 309 urandom works the same, although it uses a slower, more random 310 algorithum to generate text. 311 \item{/dev/null} \\ this is just a bottemless pit file, it 312 contains nothing, it is often used to compare if something is 313 empty, saying if the contents of a file is equal to /dev/null 314 then its empty. It can also be used to delete files if you 315 wanted, via mv file /dev/null (although rm does the same 316 thing). 317 318 \item{/dev/sdXN} \\ this is how devices are listed, your 319 harddrive (or whatever the kernel detected first) is equal to 320 /dev/sda and then each partion on the drive is equal to 321 /dev/sda1, /dev/sda2, etc, etc. These file can be used for many 322 things, for example if you want to burn an ISO or IMG file to a 323 dvd/cd, the dvd will show up as /dev/sdb (or somethimes /dev/sr0 324 depending on weather you are using linux, bsd or something 325 else), and you can use the dd command to copy it to the disk. 326 You can also use the mount command to access the files on a 327 drive. You can say mount /dev/sdb1 /mnt (which could be the 328 first partion of a usb drive currently connected). This will 329 mean that if you cd into /mnt, you will see the contents of the 330 usb drive, and you can copy or edit files on /mnt to copy or 331 edit the content of the drive. 332 \end{description} 333 334 \subsection{Some examples of things and how to try them} 335 336 \section{The C wizard handbook} 337 \subsection{Why is C the way it is?} 338 } 339 \end{document}