Week 2 Flashcards

1
Q

What is the shell?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define these terms:

binaries

process

process ID (PID)

command line applications

environment variables

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are these common shells? Which is the one that we are going to use in this class?

sh:
bash:
zsh:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an environment variable?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is metadata stored?

A

This metadata is stored as key-value pairs:
VAR1_NAME = VAR1_VALUE
VAR2_NAME = VAR2_VALUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the variable PATH?

A

PATH tells the shell in which folders it should look for programs.

We can check it’s content with:
$echo $PATH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does this command do
$echo $PATH

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

Confusing: you end up not knowing from where you are executing

Bad for security: you are not executing the program you think you are

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a working directory.

A

By default the location that your program will look for data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do the following commands do?

$./mycmd
$pwd
$cd path/to/new/working/directory

A

$ ./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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do the following commands do?

$ cd ../

$ ../myprog

A

$ cd ../ → change the current working directory to the parent

$ ../myprog → executes myprog in the parent directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does this command do?
$ ls -1 /dev | grep tty | wc -l

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the | mean?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In a UNIX like system when a program starts it opens three file like objects. What are they?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do these commands do?

grep
wc

A

grep: search for a string in each line of input

wc: counts the number of characters/words/lines in the input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does this command do?
command > filename
command 2> filename

What is the difference between > and&raquo_space;?

A

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.&raquo_space; will append

17
Q

What is the difference between:
2>&1
1>&2

A

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

18
Q

What does this command do?
command > output.txt 2>&1

A

Redirects the standard error to the standard output, then redirects the
standard output to a file named “output.txt”

19
Q

What do the following commands do:
man
echo <string>
cat <file></file></string>

A

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>

20
Q

What do these grep options do?
i
v
c
l

A

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

21
Q

What do these command do:
$ grep hello *.c:

$ grep hello /home/lorenzo/prog*c:

A

$ 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

22
Q

What do these regexp syntax do?

.

+

?

[<set>]</set>

[a-b]

(<pattern>|<another>)</another></pattern>

A

. : 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>

23
Q
What do these regexp syntax do?
^ : 

$ : 

{no} : 

{min,} : 

{,max} : 

{min,max} :
A
^: 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
24
Q

What does sed do?

A

sed: typically used for string replacement in files or standard input (receives a
string or a regular expression describing the text to be modified)

25
Q

What do these do:
cut:

rev:

awk:

sort:

uniq:

A

cut: extract fields from content with separators

rev: inverts text

awk: parse/process text (quite a powerful tool; will not discuss it here)

sort: sort all input lines numerically or alphabetically

uniq: remove all duplicate lines

26
Q

What do these operations do?

ls:

type:

cd:

pwd:

cp:

mv:

rm:
touch:

du:

A

ls: list files in a directory

type: print path of program

cd: change working directory

pwd: print working directory

cp: copy file

mv: move/rename file

rm: delete file/directory (can also use rmdir for the latter)

touch: create empty file (why)?

du: display size of files

27
Q

Why do we use shell programming?

A

Most shells support simple programming syntax.

Typically used to write shell scripts to automate various tasks.

28
Q

How do we tell grep that we want to use . as a literal character?

A

Use \ which is the escape syntax.

29
Q

What does #! tell the interpreter?

What does #!/bin/bash do?

A

It tells bash this is a script.
/bin/bash tells bash where to find the interpreter.

30
Q
A