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

UNIX Tools G22.2245-001, Fall 2000

  • Upload
    cachet

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

UNIX Tools G22.2245-001, Fall 2000. Danielle S. Lahmani email: [email protected] Lecture 4. Overview. shell core functionality /bin/sh /bin/ksh /bin/csh. Shell Core Features. Simple and complex commands redirection of input/output pipes wildcards command substitution - PowerPoint PPT Presentation

Citation preview

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 4

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

2000 Copyrigths Danielle S. Lahmani

Overview

• shell core functionality

• /bin/sh

• /bin/ksh

• /bin/csh

Page 3: 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 4: 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 5: 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 6: 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 7: 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 8: 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 9: 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 10: 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 foreground job

• stop to suspend a background job

Page 11: 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.

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

2000 Copyrigths Danielle S. Lahmani

The C Shell: /bin/csh• Supports all features described in the Bourne shell

• simple and list variables

• Alias mechanism with arguments

• History mechanism for access of previous commands

• Enhanced job control

• Arithmetic

• directory stack

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: STARTUP FILES

• $HOME/.cshrc

• /etc/.login

• $HOME/.login

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: simple variables

• set { name [=word]} *

• access–$(name)–${name}–${?name} replaced by 1 if name is set, 0 otherwise 

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: list variables• Variables to which you can assign a list of values• set { name = ( {word}*) } *

• Access:• $name[selector]• ${name[selector]} selector can be a range

• $#name• ${#name} number of elements in name

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: predefined local variables

• $< the next line of standard input fully quoted

• $noclobber prevents existing files from being overridden by >, and non-existent files from being appended to by >>

• $noglob prevents wildcard expansion

 

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: methods for accessing C shell arguments

• $argv a list that contains all of the positional parameters, same as $*

• $argv[1] is equal to $1

• $#argv number of arguments

• $argv[1-n] arguments 1 through n

• $argv[0] illegal, must use $0

• $argv[$#argv] last argument 

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh:file inquiry operator• -option filename

operator meaning r read access

w write access

x execute access

e existence

o ownership

z zero length

f plain file

d directory

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: ARITHMETIC EXPRESSION

• Assignment of results of an expression:

• Set variable to expression

@ variable op expression

@ variable[index] op expression

• example:

@ x = 2 + 3

%echo $x

5

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh:ALIAS• alias [word[string]]• unalias

• to share an alias with other subshells, place it under .cshrc

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

2000 Copyrigths Danielle S. Lahmani

ARGUMENTS IN ALIASES• C shell assumes that all arguments come at the

end of the alias definition, unless you specify otherwise– Example

• % alias cx chmod +x–% cx * # makes files in all current directory executable

– Two common arguments designations to pass arguments to aliases

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh alias examplereference: The Unix C shell field

Guide G, Anderson P. Anderson• find requires a single argument, we must

escape ! to suppress interpretation from the C shell

%alias loc 'find ~ -name \!^ -print'

%loc ttydoc77

% alias ldir 'ls -l \!* | grep "^d"'

% ldir ../utils /usr/bin

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: history mechanism

• For numbered commands–set prompt = '\! %’

• storage of commands

– set history = 40 size of history file

– set savehist=32 save 32 commands across sessions

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: hist mech (continued)• command reexecution

– !! reexecute previous command

– !number

– !prefix

– !?substring

• command editing– %!even :s/pat1/pat2 substitute pat1 with pat2

– %^pat1^pat2 reexecution of previous command substituting pat1 with pat2

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

2000 Copyrigths Danielle S. Lahmani

ACCESS PORTION OF A FILENAME

• Modifier part of file portion of filename returned

• :h head filename minus trailing path

• :r root filename minus trailing *.suffix

• :e extension trailing .* suffix

• :t tail filename minus leading dir path

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: CONTROL STRUCTURES

• foreach ……. end

• if then else endif

• switch case … endsw

• while … end

• onintr [-l label] allows to specify a label to jump to when shell receives a SIGINT

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: PIPING

• Process1 |& process2

• Redirects standard output and standard error from process 1 to process 2

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh:MULTIPLE REDIRECTION

• Cmd >& file send both standard output and standard error to file

• Cmd >&! file same as above, even in noclobber is set

• Cmd >>& file append standard error and standard output to end of file

• Cmd1 | & cmd2 pipe standard error together with standard output

• (cmd > file1) >& file2 send standard output to file1; send standard error to file2 

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: JOB CONTROL

• Same as ksh, plus• stop suspend background process

• suspend suspend foreground process

• notify notify when current job changes status

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

2000 Copyrigths Danielle S. Lahmani

/bin/csh: DIRECTORY STACK

• pushd [+number|name]

pushd pushes the directory onto the directory stack

• popd [+number]popd pops a directory from the directory

stack

• dirs: lists the current directory stack .

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

2000 Copyrigths Danielle S. Lahmani

Effective and efficient shell progreference: Unix shell Tutorial G.Snyder J.R. Mashey

Bell Labs• Minimize number of processes generated

• Find out which cmds are built-in and which are not.

Number of data bytes accessed

Minimize Directory searches

Optimize Directory search order and the PATH variable

Minimize arithmetic and character processing

Avoid command substitution when you can

Move loop invariant outside of loop, especially for command substitution