Week 6: Unix Command I/O and Redirection, Processes and Job Control Flashcards
When a Unix process starts, how many streams are opened?
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
What is the default standard input/output/error?
Input: keyboard
Output: display
Error: display
For file redirection, what is the symbol for redirecting standard input?
<
or
0
For file redirection, what is the symbol for redirecting standard output?
>
or
1>
EX: cat file1 file2 > file3
For file redirection, what is the symbol for redirecting standard error?
2>
For file redirection, what do the following do?
cat file1 file2 > file3
cat file1 file2 >| file3
cat file1 file2»_space; file3
cat > file3
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»_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
What would the format be to redirect standard from a file named myfile:
Standard output to “yourfile” and standard error to “yourerrorfile”?
cat myfile > yourfile 2> yourerrorfile
What does the following do?
cat myfile &> yourfile
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
What does the following do?
cat myfile > yourfile 2>&1
stdout goes to yourfile and stderr goes to where stdout goes. The same thing as cat myfile &> yourfile
What does tr string1 string2 do?
character n of string1 translated to
character n of string2
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
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
What is /dev/null
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
What are tr, grep, wc, and sort examples of?
Filters
Grep, by default, writes the ____ containing the phrase to stdout
LINES
What does the following do?
grep “unix is easy” < myfile1 > myfile2
Write all lines of myfile1 containing phrase unix is easy
to myfile2
What does wc do?
Count the number of chars/words/lines on stdin and write the resulting statistics to stdout
What does the sort filer do?
Sort all the input lines in alphabetical order and write to
the standard output.
What does a “pipe” do in essence?
Connects stdout of one program with stdin of another
What is an alternative way, using pipes, of writing the following?
compute[2] > grep unix < readme.txt > tmp
compute[3] > wc -l < tmp
cat readme.txt | grep unix | wc -l
To include standard error in a pipe, what is the syntax?
command1 |& command2
What will the shell do when given the following command?
readme.txt > grep unix | wc -l
Your shell will go looking for a program named
readme.txt
What does the “tee” command do?
replicate the standard output:
cat readme.txt | tee myfile
When you execute a task in Unix, it runs in the ______ of your shell
Foreground
What is the syntax to run a program in the background?
a_heavy_task &
When a task is put in the background, any output….
Appears on the screen
When running a program in the background, what special command stops the job?
Ctrl-z
What command displays what is running and/or stopped?
“jobs”
EX:
compute[6] > jobs
1 Stopped make_noise
2 Stopped vi readme
What command moves a job into the foreground?
“fg”
Ex: fg %2
What command moves a job into the background?
“bg”
Ex: bg %1
What is the unix command to terminate a job?
“kill”
Ex: kill %1
What options can be used with the “kill” command to terminate no matter what?
kill -9 %1
or
kill -KILL %1
What command lists Unix processes?
“ps”
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
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.
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)
What does the following do:
grep unix file
Matches all appearances of unix in file
What does the following do:
grep ‘[Uu]nix’ file
Matches Unix and unix
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
What does a dot “.” mean in wildcarding?
Match any single character
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, …
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, …
What does “?” mean in wildcarding?
? matches zero or one occurrence of the
previous single character pattern
a\?b matches b and ab
”+” and “?” have to be escaped with _ to have the special meaning
\
ex: \? or +
To match a set or range of characters, ___ is used
[…] is used:
[wxyz] or [w-z]
What does the following do for regular expressions?
[aeiou]*
Matches any number of vowels
Wildcards lose their specialness inside of…
[…]
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
What can you use to match something at the end of a line in regular expressions?
$
ex: FINI$ matches any line ending with FINI
A ‘word’ is…
a pattern containing only letters,
digits, and underscores (_)
Match the beginning of a word with…
<
\
Match the end of a word with…
>
ox> matches ox if it appears at the end of a
word
Matching the complement of a set by using the…
– [^aeiou] - matches any non-vowel
– ^[^a-z]*$ - matches any line containing no lower
case letters
To escape the ‘specialness’ of a wildcard, use…
the backslash \ symbol
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
The -c option with grep does what?
Count the number of matches
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