CS 390 Quiz 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Like the backquote, the _____ is also evaluated by the shell when it is double-quoted.

$

variable assignment is of the form variable=value (no spaces around =), but its evaluation requires the ____ as prefix to the variable name:

$

Sometimes, we need to manipulate a group of commands collectively: redirect them, run them in the background, and so on. The _____ and _______ handle a command group. We can use the first set to redirect the standard output of a command group using a single >

(), {}

When a command takes input from multiple sources—say, a file and standard input—the ____ symbol must be used to indicate the sequence of taking input.

-

Even though no one may be using the system, a number of system processes keep running all the time. They are spawned during system startup by init (PID 1), the parent of the login shell. The ps ____ command lists them all

-e

Since knowing the parentage is often important, the ____ option displays a fuller listing that includes the PPID.

-f

The ps ___ command (ps aux in Linux) provides an informative listing of processes. Apart from the usual attributes that we are familiar with, the command also displays the state, priority, and size of the process in memory

-l

Commands usually have limits on the number of arguments they can handle. xargs uses the _____ option to provide the specified number of arguments for a single invocation of the command: find / -name core -size +1024 -print | xargs -n20 rm -f If find locates 100 files, rm will be invoked five times—each time with 20 filenames as arguments. A useful tool indeed!

-n

____ followed by a user-id displays the processes owned by the user-id. The ____ option displays processes run by all users, irrespective of their ownership.

-u, -a

Quite often, and especially in shell programming, you'll want to check whether a program runs successfully without seeing its output or saving it in a file. You have a special file that accepts any stream without growing in size—the file ___________-

/dev/null

The second special file in the UNIX system is the one indicating one's terminal—_________. But make no mistake: This is not the file that represents standard output or standard error. Commands generally don't write to this file, but you'll be required to redirect some statements in shell scripts to this file.

/dev/tty

What is the file size of /dev/null?

0

The PPID of every login shell is always ___

1

The concepts related to redirection are pretty simple. A command like ls writes to file descriptor ____, and this remains true even when you use ls > foo. To save the ls output in foo, the shell has to manipulate this file descriptor before running ls. It closes the standard output and then opens foo. Since the kernel allocates the lowest unallocated integer in the file descriptor table, foo is assigned the value 1. The ls output is thus captured in foo.

1

What does $ wc < /etc/passwd do?

1) On seeing the <, the shell opens the disk file, /etc/passwd, for reading. 2) It unplugs the standard input file from its default source and assigns it to /etc/passwd. 3) wc reads from standard input that has previously been reassigned by the shell to /etc/passwd.

What does $ wc /etc/passwd > newfile do?

1) On seeing the >, the shell opens the disk file, newfile, for writing. 2) It unplugs the standard output file from its default destination and assigns it to newfile. 3) wc (and not the shell) opens the file /etc/passwd for reading. 4) wc writes to standard output, which was previously reassigned by the shell to newfile.

Shell processing order

1) Parsing 2) Variable evaluation 3) Command substitution 4) Redirection 5) Wild-card interpretation 6) Path evaluation

What are the three possible destinations for the standard output stream?

1) Terminal (default) 2) A file using the redirection symbols > and >> 3) as input to another program using a pipeline

When a process is forked and exec'd, the new program has a different PID and PPID than its parent. However, it inherits most of the environment of its parent. The important attributes that are inherited are:

1) The real UID and real GID of the process 2) The effective UID and effective GID of the process 3) The current directory from where the process was run 4) The descriptors of all files opened by the parent process 5) Environment variables (like HOME and PATH)

Send the standard output to the destination of the standard error.

1>&2

Send the standard error to the destination of the standard output.

2>&1

The shell can easily unhook a stream from its default device and connect it to a disk file (or to any command) the moment it sees some special characters in the command line. You, as user, have to instruct the shell to do that by using symbols like ___ and ___ in the command line. This means that instead of input and output coming from and to the terminal, they can be redirected to come from or go to any disk file.

>, <

The shell also provides the _____ symbol (the right chevron used twice) to append to a file: wc sample.txt >>newfile Doesn't disturb existing contents

>>

$ cat foo cat: cannot open foo cat fails to open the file and writes to the standard error. If you are not using the _____ shell, you can redirect this stream to a file. Using the symbol for standard output obviously won't do; you need to use the 2> symbols

C

Any command surrounded by backquotes is executed by the shell, which then replaces the standard output of the command in the com- mand line.

Command substitution

Forking creates a process, but it is not enough to run a new program. To do that, the forked child needs to overwrite its own image with the code and data of the new program. This mechanism is called exec, and the child process is said to exec a new program. No new process is created here; the PID and PPID of the exec'd process remain unchanged.

Exec

____________ permission is usually necessary for any shell script to run, and by default, a file doesn't have this permission on creation. Use chmod to first accord executable status to the file before executing it

Executable

___________ creates a process by creating a copy of the existing process. The new process has a different PID, and the process that created it becomes its parent. Otherwise, parent and child have the same process image. If the child doesn't do an exec, both parent and child continue to execute the same code from the point forking was invoked.

Forking

The shell finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.

PATH evaluation

The PID of your login shell obviously can't change as long as you are logged in. When you log out and log in again, your login shell will be assigned a different _________.

PID

the PID of the parent is also available in the process table. When several processes have the same PPID, it often makes sense to kill the parent rather than all of its children separately.

Parent PID (PPID)

The shell first breaks up the command line into words using spaces and tabs as delimiters, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space.

Parsing

Each process is identified by a unique integer called the Process-id (PID). We need the PID to control a process, for instance, to kill it. The first process has the PID 0.

Process-id (PID)

What process attributes are allocated by the kernel when a process is born?

Process-id (PID) Parent PID (PPID)

The shell then looks for the characters >, <, and >> to open the files they point to.

Redirection

What are the three destinations of standard output?

Refer to image

The shell's own pathname is stored in _________, but its PID is stored in a special "variable", $$. To know the PID of your current shell, type $ echo $$

SHELL

What does cat - foo do?

Takes input first from standard input and then from foo

What does cat foo - bar?

Takes input from foo then standard input then bar

These are attributes that we relate to a file, but here they represent the UID and GID of the user running the program (and not of the file that is executed). These parameters are stored in the entry for the user in /etc/passwd.

The real UID and real GID of the process

All words preceded by a $ are evaluated as variables, unless quoted or escaped.

Variable evaluation

While the child is executing a new program, the parent normally waits for the child to die. It then picks up the exit status of the child (explained shortly) before it does something else.

Wait

The shell finally scans the command line for wild cards (the characters *, ?, [ and ]). Any word containing a wild card is replaced with a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to the command.

Wild-card interpretation

Even though standard output and standard error use the terminal as the default destination, the shell possesses a mechanism for capturing them individually. You can also append standard error in the same way you ____________ standard output

append

The two backquotes (``) denote ___________ substitution.

command

The shell enables the connection of two commands in yet another way. While a pipe enables a command to obtain its standard input from the standard output of another command, the shell enables one or more command arguments to be obtained from the standard output of another command. This feature is called ______________ ____________.

command substitution

Variable __________________ is simple; no special operators or symbols are needed. Simply place the variables side by side

concatenation

System processes that have no __________ ___________ are easily identified by the ? in the TTY column. A process that is disassociated from the terminal can neither write to the terminal nor read from it.

controlling terminal

Inheritance here implies that the child has its own ______ of these parameters and can thus alter the operating environment it has inherited. This also means that the modified environment is not available to the parent process.

copy

How does one copy all the C and Java source programs from another directory? Delimit the patterns with a comma, and then put ________ ________ around them (no spaces please!): cp $HOME/prog_sources/*.{c,java} . Won't work in Bourne shell

curly braces

When you use wc without an argument and have no special symbols like the < and | in the command line, wc obtains its input from the default source. You have to provide this input from the keyboard and mark the end of input with [Ctrl-___]

d

Processes that have no controlling terminal are also known as ___________. Many of these daemons are actually sleeping (a process state) and wake up only when they receive input.

daemons

Note that most filters can also read _________ from files whose names are provided as arguments.

directly

Even though a process originates from a program, a process can't be considered synonymous with a program. There are a number of ways that the two can differ. First, when two users run the same program, there's one program on ______ but two processes in ____________. Second, when you execute a shell script (also a program) containing a pipeline of three commands, you have three processes. Finally, a program can itself split into two or more processes while it is running; that's how processes are created anyway.

disk, memory

Command substitution is enabled when backquotes are used within ____________ quotes. If you use single quotes, it's not.

double

The UNIX system also uses a number of variables to control its behavior. There are variables that tell you the type of terminal you are using, the prompt string that you use, or the directory where incoming mail is kept. These variables are often called ___________ ___________ because they are available in all processes owned by a user.

environment variables

These file descriptors are implicitly prefixed to the redirection symbols. For instance, > and 1> mean the same thing to the shell, while < and 0< also are identical. We need to explicitly use one of these descriptors when handling the standard _________ stream. If your program opens a file, in all probability, the file will be allocated the descriptor 3.

error

The standard error is handled differently by the C shell, so the examples of this section won't work with it. In fact, the C shell merges the standard ___________ with the standard ____________; it has no separate symbol for handling standard error only. The command cat foo >& bar saves both standard output and standard error in bar.

error, output

For instance, this expression !(*.exe) All files without .exe extension matches all __________ the .exe files.

except

By default, a user-defined variable is not inherited by a child process. To make it vis- ible to all child processes, we must use the shell's ________ statement.

export

Since wc is a filter, you can redirect wc's standard input to come from a _______ and save the output in yet another. This can be done in any of these ways: wc < calc.txt > result.txt Using both standard input and output wc > result.txt < calc.txt wc>result.txt<calc.txt No whitespace! > result.txt < calc.txt wc No whitespace! As above, but command at end

file

The advantage of treating the terminal as a ________ is apparent from the preceding example. You couldn't have done so if tee (or, for that matter, any UNIX command) had placed restrictions on the type of file it could handle. Here the terminal is treated in the same way as any disk file.

file

Before we proceed any further, you should know that each of the three standard files is represented by a number, called a ________ __________. A file is opened by referring to its pathname, but subsequent read and write operations identify the file by this file descriptor.

file descriptor

The shell can affect redirection of this stream when it sees the > or >> symbols in the command line. You can replace the default destination (the terminal) with any file by using the > (right chevron) operator, followed by the _____________

filename

Commands that use the features of standard input and output are called ____________

filters

For example, when you execute the grep command, a process named _______ is created. Most UNIX commands that we execute actually run as processes; very few don't.

grep

Any command that uses standard output is also ______________ of the destination of its output.

ignorant

/dev/null simply ___________ all output written to it. This facility is also useful in redirecting error messages.

incinerates

The PPID of every login shell is always 1. This is the ______ process: the second process of the system. init is a very important process and, apart from being the parent of users' shells, it is also responsible for giving birth to every service that's running in the system—like printing, mail, Web, and so on.

init

The kernel maintains a table of file descriptors for every process running in the system. The first three slots are generally allocated to the three standard streams in this manner: 0—Standard _________ 1—Standard _________ 2—Standard ________

input, output, error

This method of running two commands separately has two obvious disadvantages: 1) For long-running commands, this process can be slow. The second command can't act unless the first has completed its job. 2) You require an _____________ file that has to be removed after completion of the job. When you are handling large files, temporary files can build up easily and eat up disk space in no time.

intermediate

Since UNIX is multitasking, hundreds or even thousands of processes can run on a large system. Processes belong to the domain of the _________, which is responsible for their management.

kernel

The shell serves the user, but the ___________ handles processes. It manages memory and schedules processes so that each process has a fair share of the CPU and other resources. It provides a mechanism by which a process is able to execute for a finite period of time and then relinquish control to another process. The kernel has to save the state of the current process (like the instruction it was currently executing) so that when its turn comes up again for execution, the kernel knows where to resume. All of this happens more than once a second, making the user oblivious to the ___________ process.

kernel, switching

The standard input file is indeed special; it can represent three input sources: 1) The __________, the default source. 2) A file using redirection with the < symbol (a metacharacter). 3) Another program using a pipeline (to be taken up later).

keyboard

A variable defined in a process is only _________ to the process and is not available in a child process.

local

Daemons do important work for the system. The ________ daemon controls all printing activity. sendmail handles both your incoming and outgoing mail. Your TCP/IP network won't run FTP and TELNET without the inetd daemon. _______ looks at its control file once a minute to decide what it should do.

lpsched, cron

When scanning the command line, the ` (backquote or backtick) is another ______________ that the shell looks for. There's a special key on your keyboard (generally at the top-left) that generates this character, and it should not be confused with the single quote ('). The shell executes the enclosed command and replaces the enclosed command line with the output of the command. For command substitution to work, the command so "back- quoted" must use standard output. date does; that's why command substitution worked.

metacharacter

If you want to include ___________ expressions in the exception list, then use the | as the delimiter: cp !(*.jpg|*.jpeg|*.gif) ../text This copies all except the graphic files in GIF or JPEG format to the text directory. Note that the parentheses and | can be used to group filenames only if the ! precedes the group.

multiple

Redirection can also be used with ___________ files. The following example saves all C programs: cat *.c > c_progs_all.txt

multiple

This works in the Korn, Bash and C shells. The Bourne shell would require two separate invocations of cp to do this job. Using the curly brace form, you can also access _________ __________: cp /home/romeo/{project,html,scripts}/* . Won't work in Bourne shell This copies all files from three directories (project, html, and scripts) to the current directory.

multiple directories

To assign a ______________ string to a variable, you can escape each space character, but quoting (single or double) is the preferred solution: message=You\ didn't\ enter\ the\ filename message="You didn't enter the filename"

multiword

xargs comes to our rescue here as it lets rm (or, for that matter, any UNIX com- mand) be used just _______ with 200 filenames as arguments.

once

It's also possible for the parent itself to die before the child dies. The child then becomes an ________ and the kernel makes init the parent of all orphans. When this adopted child dies, init waits for its death.

orphan

In a pipeline, the command on the left of the | must use standard _________, and the one on the right must use standard __________.

output, input

Consider this sequence, which displays a message and then attempts to quit: $ ( echo "You have not keyed in 3 arguments" ; exit ) You have not keyed in 3 arguments $_ The sequence meant to terminate the current shell, but it didn't happen. Commands grouped within () are run in a sub-shell. An exit statement in a sub-shell terminates the sub-shell (which in any case will happen here) and thus doesn't affect the _________.

parent

However, when the child alters the value of the variable, the change is not seen in the _____________. This should not surprise us since the child works with its own copy of the environment.

parent

A process is always created by another process, so except for the first process, every process has a __________. Processes also are arranged in a hierarchical structure with the first process occupying the top. It's like the root directory of the file system. Just as a directory can have multiple filenames in it, the multitasking nature of UNIX permits a process to have ___________ children.

parent, multiple

The file /dev/null is actually a pseudo-device because, unlike all other device files, it's not associated with any __________ device.

physical

$ who | wc -l No intermediate files created 4 The output of who has been passed directly to the input of wc, and who is said to be ________ to wc. When a sequence of commands is combined together in this way, a pipeline is formed. The shell sets up this interconnection; the commands have no knowledge of it.

piped

Files have attributes and so do processes. Most process attributes are stored in the __________ __________, a separate structure maintained in memory by the kernel. A process retains an entry in this table until it dies "properly."

process table

In this chapter, we look at some of these files as originators of __________. A process is a UNIX abstraction that enables us to look at files and programs in another way. A file is treated as a simple file when it lies in a dormant state on disk. It can also be understood as a process when it is executed. Like living organisms, processes are born; they give birth to other processes and also die. Processes make things "happen" in UNIX.

processes

Let's now use the ____ (process status) command to display some process attributes. ps fetches these attributes from the process table.

ps

The effective UID and effective GID are generally the same as their "_______" cousins, but some processes behave differently.

real

The ps output shows zombie processes as the string <defunct> in the last column. If too many zombie processes develop on a machine, a system __________ may be required to clear them.

reboot

When you export the variable, its value is available _____________ to all child processes.

recursively

Three sources of standard input

refer to image

A process is simply an instance of a _________ _________. It is said to be born when the program starts execution and remains alive as long as the program is active. After execu- tion is complete, the process is said to die. A process also has a name, usually the name of the program being executed.

running program

Commands like cat and ls run as __________ processes. The shell executes a shell script by creating an extra shell process that runs each of the commands in the script. However, built-in commands of the shell like echo, pwd and cd don't create a process at all.

separate

When you log in, the process representing the shell starts running at your terminal. This process may be ____, ksh, csh, or _______.

sh, bash

When the standard input is redirected to come from a file (with <), it's the _________ that opens the file. The command here is totally ignorant of what the shell is doing to provide it with input. However, when you invoke a command with a filename as argument, it's the command that opens the file and not the shell.

shell

The shell offers the facility of storing a group of commands in a file and then executing the file. All such files are called _______ _________. You'll also find people referring to them as shell programs and shell procedures. The instructions stored in these files are executed in the interpretive mode—much like the batch (.BAT) files of Windows.

shell scripts

So far, we have used the > to handle a ________ stream of a single command. But the shell also supports collective stream handling. This can happen in these two ways: 1) Handle two standard streams as a single one using the 2>&1 and 1>&2 symbols. 2) Form a command group by enclosing multiple commands with the ( and ) symbols or { and } symbols. You can then use a single instruction to control all commands in the group.

single

find /usr/preserve -mtime +30 -print | xargs rm -f xargs here obtains the file list from find and supplies a __________ set of arguments to rm. So even if find selects 30 files, rm is executed only once. You could say that command substitution can also do the same job, but xargs has other advantages.

single

ps presents a __________ of the process table. This picture gets outdated by the time it is displayed. On some systems, you might see ps itself in the output. ps is a highly variant command; its actual output varies across different UNIX flavors.

snapshot

The file (or stream) representing error messages that emanate from the command or shell. This is also connected to the display.

standard error

When a command runs unsuccessfully, diagnostic messages often show up on the screen. This is the ________ ________ stream whose default destination is the terminal.

standard error

The file (or stream) representing input, which is connected to the keyboard.

standard input

The standard output of one command can also be used by another command as its ___________ _________. This is the third destination of standard output and is taken up in the discussion on pipes

standard input

We have used the cat and wc commands to read disk files. These commands have an additional method of taking input. When they are used without arguments, they read the file representing the _________ ________

standard input

When you enter cat > foo, the shell associates cat's standard output with the file foo. Because the command was used without an input filename, cat looks for input from the __________ _________. Enter these two lines as shown above and terminate standard input with [Ctrl-d]. The file foo, which was created by the shell, now contains these two lines. If the output file doesn't exist, the shell creates it before executing the command. If it exists, the shell overwrites it, so use this operator with caution.

standard input

All commands displaying output on the terminal actually write to the _________ ________ file as a stream of characters, and not directly to the terminal as such.

standard output

The file (or stream) representing output, which is connected to the display.

standard output

The shell associates three files with the terminal—two for the display and one for the keyboard. Even though our terminal is also represented by a specific device name, commands don't usually read from or write to this file. They perform all terminal-related activity with the three files that the shell makes available to every command. These special files are actually __________ of characters that many commands see as input and output. A stream is simply a sequence of bytes

streams

Commands that use filenames as arguments can use command ______________ to obtain their arguments from a list: ls `cat filelist`

substitution

You can also use the feature of command ____________ to set variables: $ mydir=`pwd` ; echo $mydir /home/romeo/c_progs

substitution

_____ doesn't perform any filtering action on its input; it gives out exactly what it takes.

tee

______ is an external command and not a feature of the shell. It duplicates its input, saves one copy in a file, and sends the other to the standard output. Since it is also a filter, tee can be placed anywhere in a pipeline.

tee

In the context of redirection, the _______________ is a generic name that represents the screen, display, or keyboard (or even an X window that emulates a terminal).

terminal

Variable names begin with a letter but can contain numerals and the _ as the other characters. Names are case-sensitive; x and X are two different variables. Unlike in programming languages, shell variables are not _________; you don't need to use a char, int, or long prefix when you define them. In fact, you don't even have to declare them before you can use them. All shell variables are of the string type, which means that even a number like 123 is stored as a string rather than in binary. (This may not remain true in the future.)

typed

A variable can be removed with _________ and protected from reassignment by ____________. Both are shell internal commands: unset x x is now undefined readonly x x can't be reassigned

unset, readonly

By convention, variable names used by the UNIX system and software packages are in _______________. You are advised to use lowercase variable names in your shell scripts simply to distinguish them from system variables.

uppercase

If a pathname is used several times in a script, you should assign it to a ___________. You can then use it as an argument to any command. Let's use it with cd in this manner: $ progs='/home/romeo/c_progs' $ cd $progs ; pwd /home/romeo/c_progs

variable

Whether you use double or single quotes depends on whether you want command substitution and ____________ evaluation to be enabled or not. Double quotes permit their interpretation, but single quotes don't.

variable

The shell supports _____________ that are useful both in the command line and shell scripts. You have already encountered some of them like HOME and SHELL. Variable usage in the Bourne family differs from that in the C shell. In this section and elsewhere, we discuss Bourne-type variables.

variables

The following command sequence uses tee to display the output of ______ and save the output in a file as well: $ who | tee user.txt

who

All shell variables are initialized to null strings by default. While explicit assign- ment of null strings with x="" or x='' is possible, you can also use this as a shorthand: __________ A null string

x=

Sometimes, the filenames used by commands can be determined only at runtime. UNIX provides a real dark horse—the ________ command—that can run any command but obtains the file list from standard input.

xargs

At any instant of time, a process is in a particular state. A process after creation is in the runnable state before it actually runs (state running). While the process is running, it may invoke a disk I/O operation. The process then has nothing to do except wait for the I/O to complete. The process then moves to the sleeping state to be woken up when the I/O operation is over. A process can also be suspended by pressing a key (usually, [Ctrl-z]). Processes whose parents don't wait for their death move to the __________ state.

zombie

When a process dies, its parent picks up the child's exit status (the reason for wait- ing) from the process table and frees the process table entry. However, when the parent doesn't wait (but is still alive), the child turns into a _______. A zombie is a harmless dead child that reserves the process table slot. You can't kill a zombie.

zombie

Now, repeat the exercise using the curly braces: $ { echo "You have not keyed in 3 arguments" ; sleep 2 ; exit ; } You have not keyed in 3 arguments ... After two seconds ... login: The message is clear: Commands grouped within ____ are executed in the current shell. Here, the sequence used exit to terminate the login shell.

{}


Kaugnay na mga set ng pag-aaral

American Popular Music - Final Exam -- Chapter 8-14, Athens Tech

View Set

Cell Cycle - Multicellular Life - 5.5

View Set

Security + Full Study Guide Qs (CompTIA )

View Set