Friday, May 27, 2011

Chapter 9. Working with the POSIX Shell and Job Control

Chapter Syllabus

9.1 POSIX Shell Capabilities

9.2 File Name Completion

9.3 History and Reentering Commands

9.4 Command Line Editing

9.5 Substitution

9.6 Setting Terminal Parameters

9.7 Job Control

The shell is an interactive program that gets user input, interprets it, and then takes necessary action accordingly. It acts like a command interpreter for the user. As soon as a command is received from a user, the shell looks for a matching intrinsic command and executes it. If it does not find an intrinsic command, it searches for a disk file corresponding to the command and executes it. During this process it substitutes any variables and other commands listed in the command line. It also handles I/O redirection and can execute batch files containing other UNIX commands and control structures.

You are already familiar with many of the shell features, such as environment variables, intrinsic and extrinsic commands, shell history, and shell configuration files. In this chapter, more features of the POSIX shell are presented. These include file name completion, setting terminal options, and job control. There is more information in this chapter on some of the features you are already familiar with. These include shell history, command line editing, and substitution. We will start with a comparison of the capabilities of different UNIX shells and see what the subshells are. Then there is a brief introduction to the ulimit command that

is used for limiting system resources. File name completion and shell history will be discussed next. Then you will find some more information on command line editing and substitution. In the substitution process, you will see how command substitution, variable substitution, and tilde substitution work. While working on different terminals, you may need to set some of the terminals' parameters, and you will learn the stty command that is used for this purpose. In the last part of the chapter, job control features of the POSIX shell are discussed.

9.1 POSIX Shell Capabilities

The POSIX shell is the default shell for HP-UX users. Other shells are also available on HP-UX. Important shells and their corresponding executable files are listed in Table 9-1.

Table 9-1. Important UNIX Shells

Name of Shell

Path to Shell File

POSIX shell

/usr/bin/sh

Bourne shell

/usr/old/bin/sh

Korn shell

/usr/bin/ksh

C shell

/usr/bin/csh

There are two other shells used in HP-UX. One of these is called the restricted shell (/usr/bin/rsh), which is used to provide restricted access to some users. The key shell (/usr/bin/keysh) is a context-sensitive shell that may be handy to use with HP terminals.

The capabilities of POSIX and other important shells available on HP-UX systems are presented in Table 9-2.

Table 9-2. Features of Different UNIX Shells

Feature

Description

POSIX

Bourne

Korn

C

History

Allows previous commands to be viewed and reused

Yes

No

Yes

Yes

Line editing

Allows a command line to be edited

Yes

No

Yes

No

File name li

Enables the user to enter partial file dlh

Yes

No

Yes

Yes

completion

names and complete these automatically by pressing certain keys

Command aliases

Allows creating aliases for standard commands

Yes

No

Yes

Yes

Job control

Allows for running jobs in background and moving jobs from background to foreground and vice versa

Yes

No

Yes

Yes

In the rest of this chapter, you will learn more about some of the capabilities of the POSIX shell.

Shell and Subshell

A subshell can be invoked inside a shell at any time by using the shell path as the command line. A subshell inherits all environment variables from the parent shell but doesn't inherit any shell variables. Sometimes a user may need to invoke a subshell to have a particular shell environment. The current shell also invokes subshells if required by a command. Shell scripts are usually executed in subshells. You can exit from a subshell using the exit command at any time.

Controlling Resources (ulimit)

The ulimit command is an intrinsic command of the POSIX shell and is used to limit user resources. If you specify the -a switch with this command, it lists current resource limits. A typical output of the command is as follows.

$ ulimit -a

time(seconds) unlimited

file(blocks) unlimited

data(kbytes) 135168

stack(kbytes) 8192

memory(kbytes) unlimited

coredump(blocks) 4194303

nofiles(descriptors) 256

$

This command is very useful if you want to specify a limit for resources. For example, if you don't want the shell to create a core file larger than 1024 blocks, you can use following command.

$ ulimit -c 1024

$ ulimit -a

time(seconds) unlimited

file(blocks) unlimited

data(kbytes) 135168

stack(kbytes) 8192

memory(kbytes) unlimited

coredump(blocks) 1024

nofiles(descriptors) 256

$

9.2 File Name Completion

File name completion is used when you are typing commands with long file names. Using this feature of the shell, you type in a few starting characters of the name of a file and then press the key twice, and the shell completes the name of the file. Consider a directory that contains three files, and you want to see the contents of a file beta.

$ ls

alpha-334 alpha-434 beta

$ cat b Esc Esc

$ cat beta

As soon as you press the key twice, the shell completes the file name as shown in the last line.

If there is more than one file that starts with the same characters you have typed, the shell completes the file name to the extent that all files have the same characters. After that you can press the and keys to list the available choices and then complete the file name by typing additional characters. Please see the next example where two files start with the letter a. By pressing the key twice, the shell completes the file name up to alpha-. After that you can press the keys to see the choices, and then complete the command using the usual command line editing method or by typing additional characters and again pressing the key twice.

$ ls

alpha334 alpha-434 beta

$ cat a Esc Esc

$ cat alpha- Esc =

1) alpha-334

2) alpha-434

$

After displaying the matching files, you can type a few more characters on the command line to distinguish files and again press the key twice to complete the file name.

The file name completion mechanism may be very useful when typing long commands with less keystrokes and without any error.

9.3 History and Reentering Commands

You have already seen that the shell keeps the history of the commands you type. The history command can be used to list previously used commands. You can use an option to list a particular number of previous commands. To list the three previous commands, you use:

$ history -3

457 cat /etc/profile

458 more /home/boota/.profile

459 ls -l /etc|more

$

To list a range of commands, you provide the starting and ending numbers of the commands in the history.

$ history 451 453

451 date

452 ls

453 mkdir tmp

To reexecute a previous command, you use the r command with the command number. The r command executes a numbered command from the history and prints the command as well as its result on stdout. In the next example, the command executed is the date command.

$ r 451

date

Wed Oct 13 22:33:17 EDT 1999

$

9.4 Command Line Editing

As you saw earlier, previous commands can be reused with the help of the history feature. But the history feature executes commands without any modification. If you want to execute a previous command with some modification, you can use the vi editing features for command editing. If you press the

keystroke combination, the last command appears on your command prompt. If you continue pressing , you can go back as far as the history supports. To go to the next command, you can press the key during this process. If you want to edit a particular command, first bring that command on your command prompt using this procedure and then use to move right and to move left. As you can see, all of these are vi commands. You can use other vi commands to insert or delete any text on the command line.

Use of vi commands with the command line editing feature is controlled by the EDITOR environment variable. The value of this variable must be set to vi to use vi commands.

Study Break

Practicing POSIX Shell Capabilities

Login with your name and go to directory /usr/bin. Type the partial command ll la and then press the key twice. You will find that the shell does not complete the command. Now use the keystroke combination and you will see a list similar to that shown here.

1. landiag

2. last

3. lastb

4. lastcomm

The shell did not complete the command when you pressed the key twice because there are four files that start with the combination la. Now add three more characters to your command, making it ll lastc, and then press twice. This time the shell completes the command.

Use the history command to list the last 10 commands in

the history. Try to run the fifth-to-last command using the r command. Press the keystroke combination to bring the last executed command to the command prompt and edit it to run the second-to-last command.

9.5 Substitution

There are three types of substitutions used in the POSIX shell. These will be explained next.

Variable Substitution

In a variable substitution, a variable name is used with the $ sign in the command line. The value of the variable is then substituted in the command line before it is executed. For example, the echo command prints whatever it receives on the command line.

$ echo HOME

HOME

$ echo $HOME

/home/boota

$

In the first case, the echo command printed what you typed at the command line. In the second case, echo substituted the variable $HOME and printed the value of the HOME variable.

Command Substitution

The result of another command can also be substituted in a command at the command line. Here I have used the same example of the echo command.

$ echo date

date

$ echo $(date)

Wed Oct 13 22:53:19 EDT 1999

$ echo `date`

Wed Oct 13 22:53:29 EDT 1999

$

In the first case, the echo command prints the word "date." In the second and third cases, the date command is executed and its result is substituted in the echo command.

Tilde Substitution

Tilde substitution is governed by the following rules.

A ~/ is replaced by the HOME variable.

A ~+ is replaced by the PWD variable.

A ~- is replace by the OLDPWD variable.

Where HOME points to your home directory, PWD has the value of the current directory and OLDPWD has the value of the previous working directory. A good use of tilde substitution is to go to your home directory from any other directory. See the following where you move from the /etc directory to your home directory (/home/boota) using tilde substitution.

$ pwd

/etc

$ cd ~/

$ pwd

/home/boota

$

9.6 Setting Terminal Parameters

A variety of terminals having different capabilities are used with HP-UX systems. Many of these terminals have different keyboards and key mappings. As an example, different keys may be used for the delete and backspace characters on different types of terminals. HP-UX provides the facility to change and modify control key mappings with the Set Terminal Type (stty) command. This command can be used to map a certain control character to a key sequence. The stty command with no arguments shows current control commands and their respective key mappings. It also shows current baud rate, start/stop bits, parity, flow control, and other information when invoked with the -a option.

$ stty -a

speed 9600 baud; line = 0;

rows = 24; columns = 132

min = 4; time = 0;

intr = ^C; quit = ^\; erase = ^H; kill = ^U

eof = ^D; eol = ^@; eol2 = ; swtch =

stop = ^S; start = ^Q; susp = ; dsusp =

werase = ; lnext =

parenb -parodd cs7 -cstopb hupcl -cread -clocal -loblk -

crts

-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr

icrnl -iuclc

ixon -ixany ixoff -imaxbel -rtsxoff -ctsxon ienqak

isig icanon -iexten -xcase echo echoe echok -echonl -noflsh

-echoctl -echoprt -echoke -flusho -pendin

opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -

tostop

$

If you want to set the underscore character (_) as the backspace key, you can use the following command. After that, you can use the underscore key to work as the backspace key.

$ stty erase _

$ stty -a

speed 9600 baud; line = 0;

rows = 24; columns = 132

min = 4; time = 0;

intr = ^C; quit = ^\; erase = _; kill = ^U

eof = ^D; eol = ^@; eol2 = ; swtch =

stop = ^S; start = ^Q; susp = ; dsusp =

werase = ; lnext =

parenb -parodd cs7 -cstopb hupcl -cread -clocal -loblk -

crts

-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr

icrnl -iuclc

ixon -ixany ixoff -imaxbel -rtsxoff -ctsxon -ienqak

isig icanon -iexten -xcase echo echoe echok -echonl -noflsh

-echoctl -echoprt -echoke -flusho -pendin

opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -

tostop

$

As you can see in the fourth line of the output, the new setting shown by the stty command, the value of erase is changed. Usually, people establish such settings in the startup files. If a setting is needed for all system users, the system administrator sets it as /etc/profile.

9.7 Job Control

A job is a running process in memory. Job control is a shell feature that moves jobs from the foreground to background, suspends running jobs, and restarts suspended jobs. You can also put a job into a wait state, where it waits for

another job to finish and then restarts. The shell provides a mechanism to carry out these tasks with the help of some intrinsic shell commands. The process of job control is explained next.

Foreground and Background Jobs

Whenever a user starts a program or command, in UNIX terminology a job is initiated. To start another job, you have to wait for the job to finish and get the shell command prompt again. All these jobs are foreground jobs, as the user can interact with the running program. The user can't issue another command until the job is finished and the command prompt is back. This simply means that when a job is running in the foreground, no other job can be started. If you want to start more than one job simultaneously, you need to start them in the background. A background job is one that gives you back the shell command prompt after it starts running. For example, a big number-crunching program may need hours to finish and you just don't want to wait for it to finish. Instead, you would like do some other tasks. Background jobs are run by the shell but at the same time the shell lets you submit other jobs.

To start a job in the background you can put the & symbol at the end of your command prompt. Usually, the jobs that don't need any interactive input are started in the background. You may also run programs in the background that need input by redirecting stdin to some file on the disk. If a background job sends its output to stdout, it is displayed on your terminal screen unless you have redirected it. Redirection and pipes can be used with background jobs. Next is a typical output of a job started in the background.

$ ll /usr >mylist &

[1] 11633

$

Once a job is started in the background, the shell prints the job ID and process ID (PID) numbers of the command that started the job on the terminal display. You can then use this job ID to bring a job into the foreground at any time. You can also list background-running jobs with the jobs command.

$ jobs

[1] + Stopped vi myfile

[2] - Running vi file2

$

The jobs command lists all background jobs, job IDs, and whether these jobs are running or stopped. A plus symbol (+) in the output of this command shows that this is the current job; a minus sign (-) shows which job is scheduled to be run next after the current job. A -l switch with the jobs command shows the PID of all jobs.

Jobs may be in a running or stopped state. A job may be stopped if it needs input that is not available or is suspended by the user. When a job is completed, it goes into the Done state. If you run a job in the background and it tries to read something from stdin, it is automatically stopped by the shell until brought into the foreground by the user.

Suspending a Foreground Job

Many times you start a job in the foreground and then you want to do something else without abandoning the program running in the foreground. For example, you may have started the vi editor and you need to copy files without abandoning the editor. The POSIX shell provides a mechanism to suspend a current job temporarily. To suspend a running job, you can use the key sequence represented by the susp value in the stty -a output, often defined as . Pressing that key sequence suspends the current job and gives you the command prompt. The job is suspended, and if you use the jobs command, you will see the job is stopped. You will also see a plus (+) symbol showing that this is the current job.

$ jobs

[1] + Stopped vi myfile

[2] - Stopped vi file2

$

To find the value of the susp sequence, use the stty -a command. If this value is not set, you can use the stty command to set its value as follows.

$ stty susp ^z

$

The (^) symbol shows the control key. After this command, you can use the key sequence to suspend a job.

Resuming Suspended Jobs and Bringing Background Jobs to the Foreground

All suspended jobs can be resumed with the foreground (fg) command. The same command is used to bring background jobs to the foreground. To bring a job to the foreground, you need to know the job id with the jobs command. If you don't specify any job ID, the current job is brought into the foreground. For example, when you suspend the vi editor to do some other work, the fg command will bring the vi screen back, and you can use it again in the normal way.

$ jobs

[1] + Stopped vi myfile

[2] - Stopped vi file2

$ fg %2

Job numbers are used with the percent (%) symbol with the fg command. You can also use the command name with the fg command, which is sometimes more convenient when writing shell scripts.

Study Break

Job Control

Use the stty command to set the value of susp equal to . Start vi editor to edit a file file1. Suspend this vi session using the combination. Use the jobs command to list background jobs. Start another vi session with file file2. Also suspend this session. Bring the first vi session to the foreground using the fg command. Close the editor and then bring the second vi session to the foreground and close it.

Moving Jobs to the Background

As soon as you suspend a foreground job, it goes into a stopped state. You can start the job, keeping it in background with the bg (background) command. To send a foreground job into the background, first suspend it and then use the bg command. If there is more than one suspended job, you need to provide a job ID to the bg command to bring a particular job into running state from stopped state. The following sequence of commands lists background jobs and then changes the state of job number 2 to running.

$ jobs

[1] + Stopped vi myfile

[2] - Stopped vi file2

$ bg %2

[1] + Stopped vi myfile

[2] - Running vi file2

$

Stopping a Running Job

There is no direct way to stop a running job. We can, however, adopt an alternate method to stop a running job. We can bring a background job into the foreground and then suspend it. You may need to stop a running job temporarily when it is taking a lot of system resources and you want to run a more important job.

Waiting for Background Jobs to Finish

At any point, if you want to wait for background jobs to be finished, just use the wait command. This command stops the command prompt until all background jobs are finished. If you want to stop the wait command, just press the key on your keyboard. We can also wait for a particular job to finish by specifying the job number as an argument to the wait command.

$ wait %2

Test Your Knowledge

1:

The default HP-UX shell is:

A. Bourne Shell

B. Korn Shell

C. POSIX Shell

D. C Shell

2:

You are in a directory having three files, file1, file2, and afile. You type a command ls f and then press the key followed by the key. What happens?

A. The shell completes the command.

B. The typed command is erased.

C. You get a list of files in the directory.

D. You get a list of files in the directory starting with f.

3:

You use the date command to see the current system time. Just after that, you press the key followed by the key. What happens?

A. The previous command is displayed on the command prompt.

B. The date command is executed.

C. The current shell is suspended.

D. Nothing happens.

4:

What does the command r 3 do?

A. It reads three lines of user input and then displays them on the screen.

B. It repeats the last three commands.

C. It returns three steps back in the command history.

D. It executes command number 3 in the command history.

5:

For what purpose is the stty command used?

A. setting baud rate

B. setting terminal control characters

C. setting flow control

D. all of the above

6:

Your home directory is /home/boota. You moved from your home directory to the /etc directory. How can you go back to your home directory?

A. using the cd HOME command

B. using the cd ~/ command

C. using the cd OLDPWD command

D. all of the above

7:

A job running in the foreground can be suspended by:

A. the bg command

B. the fg command

C. using the susp control character

D. using the suspend command

8:

Background jobs are always in:

A. a suspended state

B. a stopped state

C. a running state

D. none of the above

No comments:

Post a Comment