Overview
- Terminal
- Docker
- Hello, world
- Standard input and standard output
- Make
- Reading specifications
Terminal and command prompt
- Welcome to the 1970s
What’s a terminal?
- A terminal is a program with a simple textual display and textual input.
- Input is directed to a program called the shell.
- The shell’s job is to run other programs!
- It runs the programs you name with the arguments you supply.
- It is also programmable and different shells have different useful programming features.
Common shell utilities
Here are some commonly-installed, commonly-used programs that you can call
directly from the shell. You may find them useful for testing. Documentation
for these programs can be accessed via the man
page, e.g. man cat
, or
often also through a help switch, e.g. cat --help
.
Shell Program | Description |
---|---|
cat |
Write named files (or standard input) to standard output. |
wc |
Count lines, words, and characters in named files (or standard input). |
head -n N |
Print first N lines of standard input. |
head -c N |
Print first N characters of standard input. |
tail -n N |
Print last N lines of standard input. |
echo ARG1 ARG2... |
Print arguments to standard output. |
printf FORMAT ARG... |
Print arguments with printf-style formatting. |
true |
Always succeed (exit with status 0 ). |
false |
Always fail (exit with status 1 ). |
sort |
Sort lines in input. |
uniq |
Drop duplicate lines in input (or print only duplicate lines). |
tr |
Change characters; e.g., tr a-z A-Z makes all letters uppercase. |
ps |
List processes. |
curl URL |
Download URL and write result to standard output. |
sleep N |
Pause for N seconds, then exit with status 0 . |
cut |
Cut selected portions of each line of a file. |
grep PATTERN |
Print lines in named files (or standard input) that match a regular expression PATTERN . |
tac |
Write lines in named files (or standard input) in reverse order to standard output. |
Editing code in the terminal
nano
- Also
vi
,emacs
Useful terminal keystrokes
- Control-C kills the program currently running in the terminal
- Most of the time
- If a program is waiting for input, Control-D signals end of file
- Control-Z puts a program to sleep
jobs
lists sleeping programsfg
(for “foreground”) wakes up the most recent programfg %N
wakes up a specific program
Docker
Where does software run?
Another analogy
Virtualization
- One of the great insights of theoretical computer science is that code is data
- The “universal Turing machine” can simulate the operation of any Turing machine
- Machine code is also data
- Bytes of data represent instructions
- Software can interpret those bytes as instructions—and completely simulate the operation of a physical processor and its peripherals!
- Virtualization is so useful than modern processors have special support for it
So what’s Docker?
- Docker provides a flexible virtual environment that runs a client operating system (called a container) inside a host operating system
- The container is mostly isolated from the host, but not completely
- It shares files with the host
- So you can edit files in the host and the changes show up in Docker, and vice versa!
- Our containers are “privileged”, so don’t get too crazy in there—you can mess up your computer if you try hard enough
Hello, world
- Let’s write a program that prints
hello, world
to the terminal!
Hello, world
#include <cstdio>
int main() {
fprintf(stdout, "hello, world\n");
}
Standard input, standard output, and standard error
- C++ programs have access to three standard streams (open files)
- Standard input (
stdin
) is for reading- For instance, from the terminal
- Note: When a program is reading from the terminal, typing Control-D in the terminal signals end-of-file
- Standard output (
stdout
) is for writing- For instance, to the terminal
- Standard error (
stderr
) is for writing information about errors- Usually to the terminal
Redirecting output
- Shells can change where standard input, output, and error point
<
redirects standard input>
redirects standard output2>
redirects standard error
Pipes
|
connects two programs together by their standard streams- For instance,
echo Hello | wc
- The standard output of the left-hand program becomes the standard input of the right-hand program
Pipe to pager
- A pager program lets you navigate through long output with simple keystrokes
- Our favorite pager is called
less
less
keystrokes to know:- h: See a summary of commands
- q: Exit
- Space: Go to next page
- >: Go to end of input
- <: Go to start of input
- / + pattern: Search input for a pattern
- To pipe output to a pager, run, for example,
program | less
- This will pipe standard output not standard error
- To pipe both standard output and standard error, run
program 2>&1 | less
2>&1
: Send standard error (2) to the same place as standard output (1)| less
: Send standard output [and thus also standard error] toless
- Example: for long compiler errors,
make 2>&1 | less
- To pipe both standard output and standard error, run
Make
- A Makefile (in this class, a
GNUmakefile
) specifies a recipe for building programs or otherwise preparing an environment - Use
make V=1
to see which commands run
Count bytes
“Write a program bc61
that counts the number of bytes in its standard input
and prints the result to standard output. Use the fgetc
and fprintf
library functions for C-style I/O. Make sure you test your bc61 program with
different inputs.”
Count words and lines
“Write a program wc61
that counts words and lines as well as bytes. A word
is a sequence of one or more non-whitespace characters, and a line is zero or
more bytes terminated by the newline character \n
. Use the isspace
library
function, which is declared in the <cctype>
header, to detect whitespace.”