Week 2 Flashcards
What is the shell?
A shell is a program that presents a prompt and waits for text commands
Commands are typically the names of executable programs
Most commonly, the shell finds the program indicated by the command, executes it, and displays its output
Note: every time you see a line beginning with “$” in my slides, it means what follows is a shell command
Define these terms:
binaries
process
process ID (PID)
command line applications
environment variables
These files are compiled programs (typically). They are often called binaries.
Through a complicated set of actions, the OS loads the binary into memory and executes it.
An executing program/binary is called a process.
The OS stores metadata about each process, e.g., the process ID (PID).
Command-line applications often interact with each other and the user through files, e.g., file
redirection, pipes, stdin, stdout, etc.
Shells use environment variables to store configuration information, e.g., $PATH denotes where to look for binaries.
12
What are these common shells? Which is the one that we are going to use in this class?
sh:
bash:
zsh:
sh: Bourne Shell: the original UNIX shell, developed at Bell Labs in the 1970s
bash: Bourne Again Shell: GNU project extension of sh (default in Linux)
zsh: Z Shell: extended version of bash w/ different syntax (default in MacOS)
In this class we will use bash.
What is an environment variable?
A shell maintaining some metadata that alter its functioning or that of the programs that are executed.
Each key-value pair represents a variable and its value
Variables can be read and/or modified by the shell, programs, or the user.
How is metadata stored?
This metadata is stored as key-value pairs:
VAR1_NAME = VAR1_VALUE
VAR2_NAME = VAR2_VALUE
What is the variable PATH?
PATH tells the shell in which folders it should look for programs.
We can check it’s content with:
$echo $PATH
What does this command do
$echo $PATH
echo: command line utility
that prints a string to the
terminal
$VARNAME: when the shell
encouters “$”, it replaces it
with the value of the
variable that follows
Consider this scenario. Your program is not in one of the folders in PATH. Therefore you run this:
$PATH=$PATH:/path/to/my/folder
Why is this bad
Confusing: you end up not knowing from where you are executing
Bad for security: you are not executing the program you think you are
What is a working directory.
By default the location that your program will look for data.
What do the following commands do?
$./mycmd
$pwd
$cd path/to/new/working/directory
$ ./mycmd will look for a program called mycmd in the current directory and run it
Print current working directory: $ pwd
Change working directory: $ cd path/to/new/working/directory
What do the following commands do?
$ cd ../
$ ../myprog
$ cd ../ → change the current working directory to the parent
$ ../myprog → executes myprog in the parent directory
What does this command do?
$ ls -1 /dev | grep tty | wc -l
ls -1 /dev lists all files in /dev in a single column of text
grep tty lists all the lines that contain the string “tty”
wc -l counts the number of lines in the output
What does the | mean?
What’s this “|” business?
The “|” symbol represents a pipe operand
* An expression of the form “cmd1 | cmd2” means:
* Execute the cmd1 program
* Take its output
* Feed it as input to the cmd2 program
In a UNIX like system when a program starts it opens three file like objects. What are they?
Standard input (stdin): an input stream which receives input from the terminal
Standard output (stdout): an output stream which by is printed on the terminal
Standard error (stderr): like standard output, but used for error messages
What do these commands do?
grep
wc
grep: search for a string in each line of input
wc: counts the number of characters/words/lines in the input
What does this command do?
command > filename
command 2> filename
What is the difference between > and»_space;?
command > filename: save standard output to a new file filename
command 2> filename: save standard error to a new file filename
The > will overwrite the file.»_space; will append
What is the difference between:
2>&1
1>&2
You can redirect the standard error to the standard output with “2>&1”
You can redirect the standard output to the standard error with “1>&2”
Remember, stdout is file descriptor 1, and stderr is file descriptor 2
What does this command do?
command > output.txt 2>&1
Redirects the standard error to the standard output, then redirects the
standard output to a file named “output.txt”
What do the following commands do:
man
echo <string>
cat <file></file></string>
man: manual.
echo <string>: print <string> on the terminal. Actually helpful! For example, to
print the value of an environment variable (echo $PATH)
cat <file>: print the content of <file> on the terminal</file></file></string></string>
What do these grep options do?
i
v
c
l
i: makes matching case insensitive
v: return all lines that do not match the pattern
c: print number of matching lines per file
l: print names of files with matches
What do these command do:
$ grep hello *.c:
$ grep hello /home/lorenzo/prog*c:
$ grep hello *.c: find the string “hello” in all files ending in “.c” in the current directory
$ grep hello /home/lorenzo/prog*c: find the string “hello” in all files starting in
“prog” and ending in “c” in the directory /home/lorenzo
What do these regexp syntax do?
.
+
?
[<set>]</set>
[a-b]
(<pattern>|<another>)</another></pattern>
. : matches any character
- : matches a sequence of any length (including 0) of the previous character
+ : matches a sequence of length 1 or above of the previous character
? : matches 0 or 1 occurrences of the previous character
[<set>] : matches any character in a set</set>
(<pattern>|<another>) : OR between patterns</another></pattern>
What do these regexp syntax do? ^ : $ : {no} : {min,} : {,max} : {min,max} :
^: must appear immediately after the beginning of a line $ : must appear right before the end of a line {no} : matches preceding pattern exactly no times (e.g., (ab){10}) {min,} : matches preceding pattern at least min times (e.g., (a[0-9]+){4,}) {,max} : matches preceding pattern at least max times {min,max} : matches preceding pattern between min and max times
What does sed do?
sed: typically used for string replacement in files or standard input (receives a
string or a regular expression describing the text to be modified)