π§πΎβπ»
prep
Learn about shell tools, and how computers work
π Binary and hexadecimal numbers
Learning Objectives
Read the learning objectives listed on this page: Bear in mind what you’re trying to achieve while reading this text. If a topic isn’t making much sense, and isn’t in the objectives, you can probably skip over it. If a topic is listed in the objectives, you should keep studying it until you are confident you’ve met the objective.
Reading
Read chapters 1 and 2 of How Computers Really Work. You can skip the Binary Logic section (but feel free to read it if you’re curious).
Also read the “Binary Addition”, “Signed Numbers” and “Unsigned Numbers” sections from chapter 5. You can skip the “Half Adders”, “Full Adders”, and “A 4-bit Adder” sections.
Do every exercise listed in the chapters. You can skip the projects.
Check you have achieved each learning objective listed on this page.
π CPU and memory
Learning Objectives
Read the learning objectives listed on this page: Bear in mind what you’re trying to achieve while reading this text. If a topic isn’t making much sense, and isn’t in the objectives, you can probably skip over it. If a topic is listed in the objectives, you should keep studying it until you are confident you’ve met the objective.
Read chapter 7 of How Computers Really Work.
Do every exercise listed in the chapters.
Check you have achieved each learning objective listed on this page.
π» Shell tools
Learning Objectives
Through this course, we are going to write lots of programs.
But a lot of programs have already been written by other people. It’s often quicker and easier to use an existing program than to write a new one.
We are going to learn about some commonly used programs. We will learn some common patterns that make them easier to learn them and to combine them.
We run these programs from a terminal. We can either call the programs directly from a terminal, or put instructions to call the programs in a script and run the script.
Conventions
To describe running a program, we will use this syntax:
% ls
file1 file2
Here we are saying: At a terminal prompt (signified by the %
), run the command ls
.
Below the command is the output (file1 file2
) you should expect to see after you run the command. Sometimes this is the exact output you should expect. Sometimes example outputs are given: these could vary according to your filesystem, username, or other variables.
To call a program, we type its name and press enter. Open a terminal and run:
% ls
file1 file2
Most of the programs which take a file as an input take it as an argument to the command.
We list arguments after the command name, separated with a space, e.g. we can pass /tmp
as an argument to ls
:
% ls /tmp
some-temporary-file some-other-temporary file
Flags and arguments
Some programs take extra arguments. These may be optional or required. Extra arguments generally start with a -
or --
then their name. If these extra arguments take a value, expect the value to come after the name.
% grep -r -B 1 "hello" .
./comparison/index.md-```js {linenos=table,linenostart=1}
./comparison/index.md:"hello Mhairi" === `hello ${mhairiName}`;
--
./errors/index.md-```js
./errors/index.md:const result = console.log("hello world");
Here we passed the optional argument -r
and the optional argument -B
. -r
takes no value. We gave -B
the value 1.
We call arguments that start with a -
or --
flags. We could give flags in any order - grep -B 1 -r "hello" .
works the same as grep -r -B 1 "hello" .
.
Flags that start with a -
not a --
can also be squashed together. grep -rc "Hello" .
works the same as grep -r -c "Hello" .
. If you are looking up a flag you don’t know, like -rc
, remember it may actaully be two flags (-r
and -c
). You can also specify a value for the last flag in a squashed together list: grep -rcB 1 "hello" .
.
"hello"
and .
are called positional arguments because they don’t have a named flag before them. The program decides how to interpret them based on their position (order) in the command line, rather than a named flag coming before them. Their order matters.
π» cat
Learning Objectives
cat
is a tool for getting the contents of files.
If you have a file at the path /tmp/about-me.txt
, you can output its contents to the terminal by running:
% cat /tmp/about-me.txt
My name is Serina.
I live in Glasgow, and I like the theatre.
π» man pages
Learning Objectives
man pages are manuals that help us understand tools. Many (but not all) tools on your computer have a man page.
Reading
Key take-aways:
- Open a man page for a tool by running
man tool-name
(e.g.man cat
). - Quit a man page by pressing
q
(forq
uit). - Scroll around a man page by using the up and down arrows.
- The synopsis section has a sample command line. Optional arguments are in
[]
s. Arguments you can repeat more than once have a...
after them. All of the possible single-letter flags are smooshed together into one. - The description section lists all of the possible flags and arguments in detail.
- Man pages tend to contain lots of information. You probably don’t want to read them top-to-bottom. Read the introduction, but then be searching with specific questions in mind like “How do I reverse the output” or “What does the
-r
flag do?”.
Exercise
Look at the man page for the cat
tool.
Questions to answer:
- If you want to include line numbers in the output, what flag would you pass?
- What does the
-b
flag do? - What does
cat
do if you pass more than one filename as positional arguments?
π» More cat
Learning Objectives
(Source, including text-only transcript: https://wizardzines.com/comics/cat/)
The comic and the man page contain some of the same information.
The man page contains a lot more information too. The man page lists everything cat
can do. The comic has targeted information for specific use-cases.
Both can be useful.
Check you have achieved each learning objective listed on this page.
π» ls
Learning Objectives
ls
is a tool for listing the files in a directory.
It is named ls
because it is used to list files.
Learn about ls
from its man page (and the backlog exercises).
Some commonly used flags you should understand:
-R
-l
-t
and-r
-a
-1
-h
π¬ Right! -l includes metadata, and -t sorts by modification time (mtime).
π¬ Not quite - check the meaning of -l.
π¬ Not quite - check the meaning of -t.
Exercise
Write down answer to the following question:
If you want to list the files in the directory /tmp
, one file per line, what command would you run?
π» wc
Learning Objectives
wc
is a tool for counting the bytes, words, or lines in a file.
It is named wc
because it is used to find a word count.
Learn about wc
from its man page (and the backlog exercises).
Some commonly used flags you should understand:
-c
-l
-w
π¬ Not quite - check the meaning of -w.
π¬ Not quite - check the meaning of -w.
π¬ Right! -w means count words.
Exercise
Write down answers to the following question:
If you want to get the number of lines in the file /some/other/file
, what command would you run?
π» grep
Learning Objectives
grep
is a tool for searching files for text.
(Source, including text-only transcript: https://wizardzines.com/comics/grep/)
grep
is designed to search using regular expressions, which are patterns that describe text.
For instance, the regular expression ^[0-9]*$
searches for a line which has only numbers in it. The ^
means “from the start of the line”, the $
means “until the end of the line”, the [0-9]
expresses the characters we’re looking for, and the *
means “0 or more of the characters”. ^[0-9]
searches for a line which starts with a number (but doesn’t care about what comes after).
You can learn more about Regular Expressions at RegexOne. Many engineers just know the basic concepts, and Google for help when they need something more complicated, and this is fine.
Learn about grep
from its man page (and the backlog exercises).
Some commonly used flags you should understand:
-c
-F
-r
-v
-l
-o
-A
,-B
,-C
π¬ Not quite - check the meaning of -i.
π¬ Not quite - if we wanted to ignore binary files we’d need to pass -I (capital i) as well
π¬ Right! -r means all files under the directory, -i means case insensitive, and -l means just list the file names.
Exercise
Write down answer to the following question:
If you want to find all of the lines in a file which end with a number, what command would you run?
π» sed
Learning Objectives
sed
is a tool for replacing text in files with other text.
(Source, including text-only transcript: https://wizardzines.com/comics/sed/)
sed
executes a list of commands on a file. The most common commands are to replace some text with some other text, or to show or hide or delete lines that meet some criteria.
Sed commands feature both a matcher (called an “address”) specifying which lines to operate on, and an operation (called a command) to perform on those lines.
Example
sed '1,3 s/cat/dog/' some-file
Address: 1,3
- lines 1-3 of some-file
Command: s/cat/dog/
- replace the first occurence of “cat” on each line with “dog”
If you don’t specify an address, your command applies to the whole file:
Example
sed 's/cat/dog/' some-file
Address: Not specified, so every line of some-file
Command: s/cat/dog/
- replace the first occurence of “cat” on each line with “dog”
Learn about sed
from its man page (and the backlog exercises).
Some sed commands you should understand and be able to write:
s/cat/dog/
s/cat/dog/g
s/\([0-9]\)/only \1/g
/dog/d
6d
π¬ Not quite - take a look at the flags passed to the s command.
π¬ Right! sed operates on lines, and without the g flag it just does one replacement. We can use any character (like #) between the sections of an s command, not just /.
π¬ Not quite, think about what unit sed operates on - characters/words/lines/files?
π¬ Not quite - what does an - inside []s mean?
π¬ Right - we match all numbers, and re-insert it with a back-reference.
π¬ Not quite - what does the \ before the 1 mean?
Exercise
Write down answers to the following question:
If you want to output the lines of the file named /tmp/animals
which don’t include any numbers, what command would you run?
π» awk
Learning Objectives
awk
is a tiny programming language for manipulating columns of data.
(Source, including text-only transcript: https://wizardzines.com/comics/awk/)
Learn about awk
from its man page (and the backlog exercises).
Some awk programs you should understand and be able to write:
{print $2}
/Ibrahim/ {print $NF}
{sum += $2} END {print sum}
π¬ Right - awk runs per-line, and the default field separator is spaces.
π¬ Not quite - look at the default value if the -F flag.
π¬ Not quite - check what conditions apply to actions.
π¬ Not quite - check what conditions are.
π¬ Right, the condition filters which lines the command applies to.
π¬ Not quite - check the difference between conditions and actions.
Exercise
Write down answers to the following questions:
- What does
NF
in anawk
program mean? What does$NF
mean? - Imagine you have a file named
scores
where each line contains a name, then a space, then a numeric score. If you want to useawk
to output the biggest score, what command would you run?