Week 6: Unix Command I/O and Redirection,  Processes and Job Control Flashcards

1
Q

When a Unix process starts, how many streams are opened?

A

Three streams:

  • Standard Input (stdin): default place from which programs read
  • Standard Output (stdout): default place to which programs write
  • Standard Error (stderr): default place where errors are reported
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the default standard input/output/error?

A

Input: keyboard

Output: display

Error: display

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

For file redirection, what is the symbol for redirecting standard input?

A

<

or

0

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

For file redirection, what is the symbol for redirecting standard output?

A

>

or

1>

EX: cat file1 file2 > file3

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

For file redirection, what is the symbol for redirecting standard error?

A

2>

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

For file redirection, what do the following do?

cat file1 file2 > file3

cat file1 file2 >| file3

cat file1 file2&raquo_space; file3

cat > file3

A

cat file1 file2 > file3
– concatenates file1 and file2 into file3
– file3 is created if not there

cat file1 file2 >| file3
– file3 is clobbered if there

cat file1 file2&raquo_space; file3
– warning if file3 is not there
– file3 is appended to if it is there

cat > file3
– file3 is created from whatever user provides from
standard input

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

What would the format be to redirect standard from a file named myfile:

Standard output to “yourfile” and standard error to “yourerrorfile”?

A

cat myfile > yourfile 2> yourerrorfile

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

What does the following do?

cat myfile &> yourfile

A

If myfile exists, it is copied into yourfile

If myfile does not exist, an error message cat: myfile: No such file or directory is copied into yourfile

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

What does the following do?

cat myfile > yourfile 2>&1

A

stdout goes to yourfile and stderr goes to where stdout goes. The same thing as cat myfile &> yourfile

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

What does tr string1 string2 do?

A

character n of string1 translated to

character n of string2

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

What do the following do?

compute[3] > tr aeoiu eoiua file2
compute[4] > tr eoiua aeoiu file2
compute[5] > tr a-z A-Z < file1 > file2

A

Replace every occurrence of the characters in string1 with those in string2

file1 as input, redirect it to the translation, and place the output in file2

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

What is /dev/null

A

A virtual file that is always empty.

Copy a file to here and they disappear

Copy a file from here and get an empty file

Redirect error messages here and they disappear as well

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

What are tr, grep, wc, and sort examples of?

A

Filters

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

Grep, by default, writes the ____ containing the phrase to stdout

A

LINES

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

What does the following do?

grep “unix is easy” < myfile1 > myfile2

A

Write all lines of myfile1 containing phrase unix is easy

to myfile2

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

What does wc do?

A

Count the number of chars/words/lines on stdin and write the resulting statistics to stdout

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

What does the sort filer do?

A

Sort all the input lines in alphabetical order and write to

the standard output.

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

What does a “pipe” do in essence?

A

Connects stdout of one program with stdin of another

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

What is an alternative way, using pipes, of writing the following?

compute[2] > grep unix < readme.txt > tmp
compute[3] > wc -l < tmp

A

cat readme.txt | grep unix | wc -l

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

To include standard error in a pipe, what is the syntax?

A

command1 |& command2

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

What will the shell do when given the following command?

readme.txt > grep unix | wc -l

A

Your shell will go looking for a program named

readme.txt

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

What does the “tee” command do?

A

replicate the standard output:

cat readme.txt | tee myfile

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

When you execute a task in Unix, it runs in the ______ of your shell

A

Foreground

24
Q

What is the syntax to run a program in the background?

A

a_heavy_task &

25
When a task is put in the background, any output....
Appears on the screen
26
When running a program in the background, what special command stops the job?
Ctrl-z
27
What command displays what is running and/or stopped?
"jobs" EX: compute[6] > jobs 1 Stopped make_noise 2 Stopped vi readme
28
What command moves a job into the foreground?
"fg" Ex: fg %2
29
What command moves a job into the background?
"bg" Ex: bg %1
30
What is the unix command to terminate a job?
"kill" Ex: kill %1
31
What options can be used with the "kill" command to terminate no matter what?
kill -9 %1 or kill -KILL %1
32
What command lists Unix processes?
"ps"
33
What are the options that can be used with ps command?
–l gives you a long listing of what is going on –u loginid tells you about loginid's processes
34
What does the "ulimit" utility do?
Sets or reports the file-size writing limit imposed on files written by the shell and its child processes (files of any size may be read). User can decrease limit. Only a process with appropriate privileges can increase the limit.
35
What are some "ulimit" options?
ulimit –a (prints all limits) ulimit -n (maximum number of open file descriptors) ulimit -u (maximum number of processes available)
36
What does the following do: grep unix file
Matches all appearances of unix in file
37
What does the following do: grep '[Uu]nix' file
Matches Unix and unix
38
How are regular expressions different from file name expansion?
– Regular expressions are interpreted and matched by special utilities (such as grep). – File name expansions are interpreted and matched by shells
39
What does a dot "." mean in wildcarding?
Match any single character
40
What does an asterisk "*" mean in wildcarding?
Match zero or more of the previous single character pattern Ex: a*b matches b, ab, aab, aaab, aaaab, …
41
What does a plus "+" mean in wildcarding?
+ matches one or more occurrences of the previous single character pattern a\+b matches ab, aab, aaab, aaaab, …
42
What does "?" mean in wildcarding?
? matches zero or one occurrence of the previous single character pattern a\?b matches b and ab
43
"+" and "?" have to be escaped with _ to have the special meaning
\ ex: \? or \+
44
To match a set or range of characters, ___ is used
[...] is used: [wxyz] or [w-z]
45
What does the following do for regular expressions? [aeiou]*
Matches any number of vowels
46
Wildcards lose their specialness inside of...
[...]
47
What can you use to match something at the beginning of a line in regular expressions?
^ ex: ^TITLE matches any line with TITLE at the beginning
48
What can you use to match something at the end of a line in regular expressions?
$ ex: FINI$ matches any line ending with FINI
49
A 'word' is...
a pattern containing only letters, | digits, and underscores (_)
50
Match the beginning of a word with...
\< \
51
Match the end of a word with...
\> ox\> matches ox if it appears at the end of a word
52
Matching the complement of a set by using the...
^ – [^aeiou] - matches any non-vowel – ^[^a-z]*$ - matches any line containing no lower case letters
53
To escape the 'specialness' of a wildcard, use...
the backslash \ symbol
54
To remember portions of a regular expression, use
Surround them with \(...\): '^\([a-z]\)\1' matches lines beginning with a pair of duplicate (identical) letters '^.*\([a-z][a-z]*\).*\1.*\1' matches lines containing at least three copies of something which consists of lower case letters
55
The -c option with grep does what?
Count the number of matches
56
What does the | symbol do for regular expressions? Ex: r1|r2
r1|r2 matches regular expression r1 or r2 (| acts like a logical “or” operator). – red|blue will match either red or blue – Unix|UNIX will match either Unix or UNIX