unix tools g22.2245-001, fall 2000

35
2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani email: [email protected] Lecture 3

Upload: reece-moon

Post on 03-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

UNIX Tools G22.2245-001, Fall 2000. Danielle S. Lahmani email: [email protected] Lecture 3. Overview. Review of process creation shell core functionality /bin/sh /bin/ksh /bin/csh project discussion. Example : Program that creates a new process to copy files. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

UNIX ToolsG22.2245-001, Fall 2000

Danielle S. Lahmani

email: [email protected]

Lecture 3

Page 2: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Overview

• Review of process creation

• shell core functionality

• /bin/sh

• /bin/ksh

• /bin/csh

• project discussion

Page 3: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Example: Program that creates a new process to copy files

• Reference: M.Bach, "The Unix Operating system", p 11.main(argc,argv)

int(argcl

char *argv[];

{/* assumes 2 args, source and target files */

if ( fork() == 0) {

/* child process */

execl("cp"."cp",argv[1],argv[2],0);

}

/* parent process */

wait(int *) 0);

printf("copy done\n");

}

Page 4: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Fork operationreference Unix Network programming - W. R. Stevens

A fte r fo rk op era tion

p aren t p rocess d a ta ch ild p rocess d a ta

sh ared text

Page 5: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

After exec of prog2 in child

A fte r exec "p rog 2 " in ch ild

p rog text d a ta u n ch an g ed

p aren t p rocess d a ta

p rog 2 text d a ta

ch ild p rocess d a ta

(prog2 is cp in example)

Page 6: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Shell Core Features• Simple and complex commands

• redirection of input/output

• pipes

• wildcards

• command substitution

• background processes

• shell variables

• here documents

• built-in cmds

• programming constructs

Page 7: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Complex commands

• Multiple commands – example: $ls ; pwd

• background processes– example: sleep 40&

• Command groupings– (cmd1; cmd2; cmd3)

• Conditional command execution

Page 8: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

File name expansion

• Wildcards

* matches any string of characters

? matches any single character

[list] matches any character in list

[lower-upper] matches any character in range lower-upper inclusive

Page 9: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Command substitution

• A command can be placed with grave accents ` ` to capture the output of command

• often used with shell variables

Page 10: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Shell Scripts• A shell script is a regular text file that contains

shell or UNIX commands

• Before running it , it must have execute permissions ( see chmod +x filename)

• Very useful for automating repetitive task and administrative tools and for storing commands for later execution

Page 11: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Shell Scripts (continued)

• When a script is run , kernel determines which shell it is written for by examining the first line of the script

– If 1st line is just #, then it is interpreted by a C shell

– If 1st line is of the form #!pathname, then the executable

– Pathname is used to interpret the script

– If neither rule 1 nor rule 2 applies, the script is interpreted by a Bourne shell.

Page 12: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Here Documents• Shell provides alternative ways of supplying standard input to

commands

• Shell allows in-line input redirection using << called here documents

• format

command [arg(s)] << arbitrary-delimiter

command input

:

:

arbitrary-delimiter

• arbitrary-delimiter should be a string that does not appear in text

Page 13: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Shell Variables• Shell has several mechanisms for creating variables. A

variable is a name

• Representing a string value– Shell variables can save time and reduce typing errors, variables

• Allow you to store and manipulate information

• two types: local and environmental– local are set by the user of by the shell itself

– Positional parameters variables are normally set only on a command line

Page 14: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Environmental Variables

NAME MEANING

$HOME absolute pathname of your home directory

$PATH a list of directories to search for

$MAIL absolute pathname to mailbox

$USER your user id

$SHELL absolute pathname of login shell

$TERM type of your terminal

Page 15: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Positional parameters• when a shell procedure is invoked, the shell

implicitly creates positional parameters. The name for a positional parameter is a number.

• Positional parameters are used mainly in scripts.– $0 is the argument in position zero on the command line

– $1 is the first argument

– $1.. $9 $n refers to the nth argument on the command line if applicable

– $# the number of positional parameters, not counting 0

– $* the list of all arguments

Page 16: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

QUOTING• Quoting restores the literal meaning to characters

that are processed specially by the shell. The literal quotes are not passed on to the command

• Single quotes ( ' ) inhibit wildcard replacement, variable substitution, and command substitution

• Double quotes ( " ) inhibit wildcard replacement only

• When quotes are nested, only the outer quotes have any effect

Page 17: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

BUILT-IN commands• commands that are internal to the shell

• Faster to execute and more efficient than other commands– Shell does not have to fork to execute the

command

– Trade-off: redirection of input/output not allowed for most of these

Page 18: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Built-in commands (continued)

• built-in commands common to the 3 shells:echo exec

cd shift

wait umask

exit eval

 

Page 19: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

Subshells

• When a parent shell forks a child to execute a command, the new child shell is sometimes called a subshell. This happens when:

– a group command is executed ( $(cmd1; cmd2; cmd3) )

– a shell script is executed( $myscript )

– a background job is executed ( cmd1&)

• A shell inherits the parent's environment but not the parent's local variables.

Page 20: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

The Bourne Shell: /bin/sh

• Startup file: .profile

• Variables:

– Assignment: var = value;

– Access: $var–Exporting variable:

$export variable

Page 21: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh:BUILT-IN VARIABLES

• $# number of cmd lines args

• $- options currently in effect

• $? exit value of last executed cmd

• $$ process num of current process

• $! Proc num of last background proc

• $* all arguments on command line

• "$@"all arguments on command line individually quoted "$1" "$2" ….

Page 22: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh: Arithmetic• No arithmetic support in /bin/sh• expr expression

– Evaluates expression and sends the result to standard output

– yield a numeric or string result• test expression for conditional

expression

Page 23: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh: CONTROL STRUCTURES

• Case . . . in . . . esac

• For … do … done

• If … then … fi

• Until … do … done

• While … done

Page 24: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh: trap command

• trap specifies command that should be executed when the shell receives a signal of a particular value.

• Trap [ [command] {signal}+]

– If command is omitted, signals are ignored

Page 25: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh: Other commands

• Debbuging options for scripts:–set -vx

– -v : echo shell commands as they are read

– -x : echo shell commands as they are executed 

• sequenced commands

– { cmd1; cmd2; cmd3 …cmdn} executed by the parent, can redirect output, etc… 

Page 26: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh:Redirection using file Descriptors

• cmd >&n send cmd output to fd n

• cmd <&n take input for cmd from fd n

• cmd >&- close standard output

• cmd <&- close standard input

Page 27: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/sh:multiple redirection

• cmd 2>file send standard error to file

standard output remains the same

• cmd > file 2>&1 send both standard error and standard output to

file

• (cmd > file1) 2>file2 send standard output to file1, send standard error to file2

Page 28: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

The Korn Shell: /bin/ksh• Supports all features described in the Bourne shell

(/bin/sh)

• Alias mechanism

• History mechanism for access of previous commands

• Functions

• Enhanced job control

• Arithmetic

• Tilde substitution

Page 29: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

The Korn shell: /bin/ksh STARTUP FILES:

– /etc/profile

– $HOME/.profile

• ALIAS:– alias [-tx] [word[=string]]

– alias -x : to export alias to child shell

– unalias aliasname: to remove an alias

Page 30: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh: History Mechanism• Numbered commands

$ PS1='! $’ /* set prompt to contains a ! */

$HISTSIZE default is 128

• using the built-in vi editor– declare VISUAL=vi or EDITOR=vi– to edit current line, press ESC key to enter the editor

– vi cmds to edit line, when done, press ESC key again,

– additional movement: cursor up (k or - ) cursor down (j or +)

– additional searching /string or ?string : searches backward and forward through history, respectively.

Page 31: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh (continued)

• ARITHMETIC: Using let expression

• TILDE SUBSTITUTION– ~ $HOME

– ~user home directory of user

– ~/pathname $HOME/pathname

– ~+ $PWD

– ~- $OLDPWD

Page 32: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh: FUNCTIONS• Allows functions that may be invoked as shell

commands

function name {

list of commands

}

or

name() {

list of commands

}

Page 33: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh: Functions (continued)

• can use parameters

• returning from a function

• local variable using typeset

• functions can be recursive

Page 34: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh: ENHANCED JOB CONTROL

• jobs list your jobs

• bg places a specified job in the background

• fg places a specified job in the foreground

• kill sends an arbitrary signal to a process or job

• ^z to stop a job

Page 35: UNIX Tools G22.2245-001, Fall 2000

2000 Copyrigths Danielle S. Lahmani

/bin/ksh: coprocess

PIPES

• |& operator supports a simple form of concurrent processing

• cmd |&

cmd run as a background process whose standard input and output channels are connected to the original parent shell via a two way pipe.