• Aucun résultat trouvé

Job Control

Dans le document Register for Free Membership to (Page 130-134)

If a job runs away from you, you can easily terminate it by pressing CTRL-C. For example, this command will cause the shell to sleep for 30 seconds, unless we terminate it with CTRL-C:

j0pb12:~ johnnylong$ sleep 30

^C

j0pb12:~ johnnylong$

UNIX commands (shell scripts included) can also be run as a background process.This enables you to run many different jobs from one Terminal window, each of which can be controlled with special job control commands.

To run a job in the background, append the & symbol to the end of the command:

j0pb12:~ johnnylong$ sleep 10 &

[1] 1459

The shell responds with two numbers.The first lists the number of the job within the current shell. (Our example displays the number one since this is the first job we’ve run in the background in this Terminal.) The second number displays the system process ID of this job. Processes can be listed with the ps command. After ten seconds our job will complete, although there is no indication of this until the Return key is pressed:

j0pb12:~ johnnylong$

[1]+ Done sleep 10

The system informs us that the job has completed successfully. Once a job has been sent to the background, it can be controlled with a variety of com-mands. For example, the jobs command lists the status of the jobs running in the current shell:

To terminate a process from the shell, use the kill command followed by a percent sign (%) and the number of the process:

j0pb12:~ johnnylong$ kill %1 j0pb12:~ johnnylong$

[1]- Terminated sleep 30

After killing the process, you may need to press Enter again to see the termination message. A background process can also be brought to the

fore-Once a job has been brought to the foreground, it can either be termi-nated with CTRL-Cor stopped with CTRL-Z:

j0pb12:~ johnnylong$ sleep 60 &

Once a job has been stopped in this manner, it can be killed with the kill command or sent to the background with the bgcommand. Executing the bg command without parameters sends the most recent job to the background:

j0pb12:~ johnnylong$ bg [1]+ sleep 60 &

It is easy to lose track of what job is running in which state, but using job control can go a long way.Table 2.3 summarizes the basic job control commands.

Table 2.3Basic Job Control Commands

Control Character or Command Description

& When placed at the end of a

com-mand, this character indicates that the job should be run in the background.

CTRL-C Terminate the current command or

shell script.

CTRL-Z Stop (but do not terminate) the

cur-rent command. It can be resumed or killed.

fg Bring the last stopped or background

job to the foreground. Optionally specify a job prefaced with a %.

bg Set the last stopped job to run in the

background. Optionally specify a job prefaced with a %.

jobs List the jobs currently running in this

shell session.

Comments

The pound sign ()(#) designates the beginning of a comment. When bash encounters a pound sign, any text following it is ignored:

#This is a comment in the bash shell

echo "This is written to standard output." #This is a comment

Variables

Variables are set with the equal (=) sign, and recalled with a dollar sign ($).

Consider the following example:

j0pb12:~ johnnylong$ echo $foo

j0pb12:~ johnnylong$ foo=bar j0pb12:~ johnnylong$ echo $foo bar

On the first line, we request the value of the variable foo, and determine that it is not set. On the second line, the value of foo is set to bar. On the third line, the value of foo is requested again, and this time we discover the variable is set to bar.When setting a variable,don’t use the dollar sign. When checking the value of the variable, usethe dollar sign. When assigning strings,spaces matter. Consider the following examples in Tables 2.4 and 2.5:

Table 2.4Setting Variables

Command/Result Description

j0pb12:~ johnnylong$ foo= bar Broken assignment. Foo is set to NULL, -bash: bar: command not found and the shell attempts to run the

command bar, which it can not find.

j0pb12:~ johnnylong$ foo = bar Bad command. The shell attempts to run -bash: foo: command not found foo as a command, which it can not find.

The equal sign and bar are passed to foo as arguments.

j0pb12:~ johnnylong$ foo =bar Bad command. The shell attempts to run -bash: foo: command not found foo as a command, which it can not find.

=bar is passed to foo as an argument.

Table 2.4Recognizable Variables

$0, $1, $2, etc These variables hold the values of any arguments passed to a shell script. $0is reserved for the name of the script itself. $1holds the name of the first argument. For the command ./nappyscript check, $0 would contain “./nappyscript” while $1would con-tain “check.”

$@ This variable holds allof the parameters passed to a script. A loop like this will echo all arguments:

for i in "$@"

do

echo $i done

$# This variable holds the number of arguments that were passed into a script. When no arguments are provided to a script, $#is equal to zero. For each additional argument, $#is increased by one.

Dans le document Register for Free Membership to (Page 130-134)