32
The Shell Chapter 7

The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)

Embed Size (px)

Citation preview

The Shell

Chapter 7

Overview

The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)

The Command Line

The shell executes a program when you give it a command

The line that contains the command, including any arguments, is called the command line

The Command Line

Syntax Dictates the ordering and separation of the

elements on a command line

e.g. command [arg1] [arg2] … [argn]

Not all commands require arguments Some commands do not allow arguments Some commands require specific arguments

The Command Line

Syntax (cont.) Arguments

A sequence of nonblank characters is called a token or word

An argument is a token, such as a filename, that a command acts upon

e.g. $ cp temp tempcpytemp is arg1

tempcpy is arg2

The Command Line

Syntax (cont.) Options

an argument that modifies the effects of a command more than one option typically be specified options are specific to and interpreted by the program

(command), not the shell most utilities allow the grouping of options after a single

hyphen (-) help option

Many utilities display a help message when the -help option is used

All GNU Project utilities accept --help

The Command Line Syntax (cont.)

Options (cont.)

Remember! The command must be in the search path, or a path must be supplied on the command line

The Command Line

Executing the command line When a command is issued, the shell starts a

new process The process is the execution of a command While the command is executing, the shell waits

for the process to finish. At this point, the shell is in an inactive state called

sleep

Standard Input and Output

Output & Input Standard Output

A place that a program can send output, such as text The program never “knows” where the output it sends to

standard output is going Could be a printer Could be an ordinary file Could be the screen (default)

Standard Input A place that a program gets input from

Could be another program Could be the keyboard (default)

Standard Input and Output

Output & Input Standard Error

A place that a program can send error messages to

Standard Input and Output

The Screen as a File Besides ordinary files, directory files, hard links,

and soft links, Linux has an additional type of file - device files Device files reside in the Linux file structure (usually

under /dev) Represent a peripheral device

Standard Input and Output

The Screen as a File (cont.) The device name that the who utility displays after

your username is the filename of your screen e.g. /dev/pts/4

When working with multiple windows, each window will have its own device name

You can read from and write to this device file as though it were a text file

Standard Input and Output

The Screen as a File (cont.) e.g. Using the keyboard and screen as standard

input and standard output

Standard Input and Output

Redirection Allows you to alter where standard input comes

from Allows you to alter where standard output goes to

Redirecting Standard Output (>) redirect standard output - instructs the shell to

redirect the output of a command to the specified file instead of the screen

e.g. ls -l > dirlisting.txt

Standard Input and Output

Redirection (cont.)

Standard Input and Output

Redirection (cont.) Redirecting Standard Input

(<) redirect standard input - instructs the shell to redirect a command’s input to come from the specified file instead of from the keyboard

Standard Input and Output

Redirection (cont.) Appending standard output to a file

(>>) - append output - causes the shell to add new information to the end of a file, leaving any existing information intact.

e.g.$ cat orangethis is orange$ cat pear >> orange$ cat orangethis is orangethis is pear

Standard Input and Output

Redirection (cont.)

Standard Input and Output

Pipes The shell uses a pipe to connect the standard

output of one command directly to the standard input of another command

The symbol for a pipe is a vertical bar (|) e.g.

command_a [args] | command_b [args]is the same as:command_a > tempcommand_b < temprm temp

Standard Input and Output Pipes (cont.)

Filters A filter is a command that processes an input stream of

data to produce an output stream of data e.g. sort

Running a program in the background

So far, all commands and utilities used have been running in the foreground

When a command is run in the foreground, the shell waits for it to finish before giving you another prompt

When a command is run in the background, you do not have to wait for the command to finish before running another command

Running a program in the background

JOBS A series of one or more commands that can be

connected by pipes Only one foreground job allowed in a window or

on a screen Many background jobs are allowed Running many jobs at a time utilizes multitasking

Running a program in the background

JOBS (cont.) To run a job in the background, type an ampersand

(&) just before [RETURN] The shell will assign a small number to the job (job

number) and displays it between brackets Following the job number, the shell displays the

process id (PID) number E.g.

$ ls –l | lpr &[1] 22092$…[1]+ Done ls –l | lpr

Running a program in the background

Moving a job from the foreground to the background CONTROL-Z

Suspends a job Shell stops the process and disconnects standard

input from the keyboard bg

Command to send a job to the background E.g. move job 1 to background

$ bg 1

Running a program in the background

Moving a job from the foreground to the background (cont.) fg

Brings a job from the background to the foreground Only the foreground job can accept input from the

keyboard E.g.

$ fg 1

Running a program in the background

Killing a job kill

Aborts a background job Uses the PID or job number as an argument

E.g.$tail –f outfile &[1] 18228$ ps | grep tail18228 pts/4 00:00:00 tail$ kill 18228[1]+ Terminated tail –f outfile

Running a program in the background

Killing a job (cont.) E.g.

$tail –f outfile &[1] 18236$ bigjob &[2] 18237$ jobs[1]- Running tail –f outfile &[2]+ Running bigjob &$ kill %1$ RETURN[1]- Terminated tail –f outfile

Filename generation/Pathname expansion

When you give the shell abbreviated filenames that contain special characters (metacharacters or wildcards) , the shell can generate filenames that match the names of existing files

Filenames that contain these characters are called ambiguous file references

The process the shell performs on these filenames is called pathname expansion or globbing

Filename generation/Pathname expansion

The ? Special character Matches any single character in the name of an

existing file E.g. $ lpr memo? E.g.

$ lsmem memo12 memo9 memoalex newmemo5memo memo5 memoa memos$ ls memo?memo5 memo9 memoa memos

Filename generation/Pathname expansion

The * special character Matches any number of characters, including zero

characters, in a filename E.g.

$ lsamemo memo memoalx.0620 memosally user.memomem memo.0612 memoalx.keep sallymemomemalx memoa memorandum typescript$ echo memo*memo memo.0612 memoa memoalx.0620memoalx.keep memorandum memosally$ echo *meamemo memo sallymemo user.memo$ echo *alx*memalx memoalx.0620 memoalx.keep

Filename generation/Pathname expansion

The [] special characters Causes the shell to match filenames containing

the individual characters surrounded by the brackets or a range of characters E.g.

$ lpr part0 part1 part2 part3 part5

$ lpr part[01235]

$ lpr part[1-35]

E.g. print 39 files$ lpr part[0-9] part[12][0-9] part3[0-8]

Hands On Time

See ftp site for lab file: BAI517 – Ch 6-7 Utils and Shell Exercise.doc