Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew...

21
Unix Shells: Unix Shells: C, Bourne, Bourne Again, Korn, and C, Bourne, Bourne Again, Korn, and Tenex Shells Tenex Shells Presentation by: Presentation by: Katie Harris Katie Harris Andrew Murray Andrew Murray

Transcript of Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew...

Page 1: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Unix Shells: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex C, Bourne, Bourne Again, Korn, and Tenex

ShellsShells

Presentation by:Presentation by:Katie HarrisKatie Harris

Andrew MurrayAndrew Murray

Page 2: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Introduction Introduction

Numerous types of shells offeredNumerous types of shells offered

Why so many shells?Why so many shells?

Some are original shells and others Some are original shells and others combine features of the original shellscombine features of the original shells

5 shells: Bourne Shell, Bourne Again 5 shells: Bourne Shell, Bourne Again Shell, C Shell, Korn Shell, and Tenex ShellShell, C Shell, Korn Shell, and Tenex Shell

Page 3: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Bourne ShellBourne ShellDefault Unix Shell created in 1977 by Default Unix Shell created in 1977 by Stephen BourneStephen BourneThe UNIX Programming EnvironmentThe UNIX Programming EnvironmentFoundation for future shellsFoundation for future shellsVery strong and powerful syntactical Very strong and powerful syntactical languagelanguageControlling input and output – File Controlling input and output – File Descriptor 2Descriptor 2Larger argument list size and the idea of Larger argument list size and the idea of having environment variableshaving environment variables

Page 4: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

C ShellC ShellDeveloped by Bill Joy at the University of Developed by Bill Joy at the University of California at BerkeleyCalifornia at Berkeley

Extensive command language with syntax Extensive command language with syntax similar to the C programming languagesimilar to the C programming language

Job control features of the BSD kernelJob control features of the BSD kernel

Never very popularNever very popular

Aliases Aliases

Command HistoryCommand History

Bourne = Programming, C-Shell = Command Bourne = Programming, C-Shell = Command Interpretation Interpretation

Page 5: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Bash ShellBash ShellBBourne-ourne-aagain gain shshell developed as a part of ell developed as a part of the GNU project by Brian Fox in 1987the GNU project by Brian Fox in 1987

Default shell on most GNU/Linux and Mac Default shell on most GNU/Linux and Mac OS X Panther systemsOS X Panther systems

Freely distributed and combines C-Shell Freely distributed and combines C-Shell and Bourne Shell and Bourne Shell

File completion File completion

Easier History UseEasier History Use

Page 6: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Korn ShellKorn Shell

Created by David Korn in AT&T Labs in 1983Created by David Korn in AT&T Labs in 1983Compatible with Bourne ShellCompatible with Bourne ShellUtilizes both features from the Bourne Shell and Utilizes both features from the Bourne Shell and the C Shellthe C ShellSome features include: coprocesses and one-Some features include: coprocesses and one-dimensional arraysdimensional arraysThe Korn shell is an interactive command The Korn shell is an interactive command interpreter and command programming interpreter and command programming language. The shell carries out commands language. The shell carries out commands specified at the terminal or from a file. specified at the terminal or from a file.

Page 7: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Tenex ShellTenex Shell

Created by Ken GreerCreated by Ken GreerTcsh is an enhanced, but completely compatible Tcsh is an enhanced, but completely compatible version of the Berkeley UNIX C shell (csh). version of the Berkeley UNIX C shell (csh). It is a command language interpreter usable It is a command language interpreter usable both as an interactive login shell and a shell both as an interactive login shell and a shell script command processor. script command processor. It includes a command-line editor, It includes a command-line editor, programmable word completion, spelling programmable word completion, spelling correction, a history mechanism, job control and correction, a history mechanism, job control and a C-like syntax. a C-like syntax.

Page 8: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Shell FeaturesShell Features shsh cshcsh kshksh bashbash tcshtcsh

Job control Job control N Y Y Y Y N Y Y Y Y

Aliases Aliases N Y Y Y Y N Y Y Y Y

Shell functions Shell functions Y(1) N Y Y N Y(1) N Y Y N

"Sensible" Input/Output redirectionY N Y Y N "Sensible" Input/Output redirectionY N Y Y N

Directory stack Directory stack N Y Y Y Y N Y Y Y Y

Command history Command history N Y Y Y Y N Y Y Y Y

Command line editing Command line editing N N Y Y Y N N Y Y Y

Vi Command line editing Vi Command line editing N N Y Y Y(3) N N Y Y Y(3)

Emacs Command line editing Emacs Command line editing N N Y Y Y N N Y Y Y

Rebindable Command line editing N N N Y YRebindable Command line editing N N N Y Y User name look up User name look up N Y Y Y Y N Y Y Y Y

Page 9: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Shell Features(2)Shell Features(2) sh csh ksh bash tcshsh csh ksh bash tcsh

Login/Logout watching Login/Logout watching N N N N Y N N N N YFilename completion Filename completion N Y(1) Y Y Y N Y(1) Y Y YUsername completion Username completion N Y(2) Y Y Y N Y(2) Y Y YHostname completion Hostname completion N Y(2) Y Y Y N Y(2) Y Y YHistory completion History completion N N N Y Y N N N Y YFully programmable Completion N N N N YFully programmable Completion N N N N YMh Mailbox completion Mh Mailbox completion N N N N(4) N(6) N N N N(4) N(6)Co Processes Co Processes N N Y N N N N Y N N Builtin artithmetic evaluation Builtin artithmetic evaluation N Y Y Y Y N Y Y Y Y Can follow symbolic links invisibly N Y Y Y YCan follow symbolic links invisibly N Y Y Y YPeriodic command execution N N N N YPeriodic command execution N N N N YCustom Prompt (easily) Custom Prompt (easily) N N Y Y Y N N Y Y Y Sun Keyboard Hack Sun Keyboard Hack N N N N N N N N N NSpelling Correction Spelling Correction N N N N Y N N N N Y

Page 10: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Shell Features(3)Shell Features(3) sh csh ksh bash tcshsh csh ksh bash tcsh

Process Substitution Process Substitution N N N Y(2) N N N N Y(2) NUnderlying Syntax Underlying Syntax sh csh sh sh csh sh csh sh sh csh Freely Available Freely Available N N N(5) Y Y N N N(5) Y Y Checks Mailbox Checks Mailbox N Y Y Y Y N Y Y Y Y Tty Sanity Checking Tty Sanity Checking N N N N Y N N N N YCan cope with large argument lists Y N Y Y YCan cope with large argument lists Y N Y Y YHas non-interactive startup file N Y Y(7) Y(7) Y Has non-interactive startup file N Y Y(7) Y(7) Y Has non-login startup file Has non-login startup file N Y Y(7) Y Y N Y Y(7) Y YCan avoid user startup files Can avoid user startup files N Y N Y N N Y N Y NCan specify startup file Can specify startup file N N Y Y N N N Y Y NLow level command redefinition Low level command redefinition N N N N N N N N N NHas anonymous functions Has anonymous functions N N N N N N N N N N List Variables List Variables N Y Y N Y N Y Y N Y

Full signal trap handling Full signal trap handling Y N Y Y N Y N Y Y N

Page 11: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Shell Features(4)Shell Features(4) sh csh ksh bash tcshsh csh ksh bash tcsh

File no clobber ability File no clobber ability N Y Y Y Y N Y Y Y Y Local variables Local variables N N Y Y N N N Y Y N Lexically scoped variables Lexically scoped variables N N N N N N N N N NExceptions Exceptions N N N N N N N N N N

Y Feature can be done using this shell.Y Feature can be done using this shell.N Feature is not present in the shell.N Feature is not present in the shell.F Feature can only be done by using the shells function mechanism.F Feature can only be done by using the shells function mechanism.L The readline library must be linked into the shell to enable this Feature. L The readline library must be linked into the shell to enable this Feature. 1.1. This feature was not in the orginal version, but has since become almost This feature was not in the orginal version, but has since become almost

standard.standard.2.2. This feature is fairly new and so is often not found on many versions of the shell, This feature is fairly new and so is often not found on many versions of the shell,

it is gradually making its way into standard distribution.it is gradually making its way into standard distribution.3.3. The Vi emulation of this shell is thought by many to be incomplete.The Vi emulation of this shell is thought by many to be incomplete.4.4. This feature is not standard but unoffical patches exist to perform this.This feature is not standard but unoffical patches exist to perform this.5.5. A version called 'pdksh' is freely available, but does not have the full functionality A version called 'pdksh' is freely available, but does not have the full functionality

of the AT&T version.of the AT&T version.6.6. This can be done via the shells programmable completion mechanism.This can be done via the shells programmable completion mechanism.7.7. Only by specifing a file via the ENV environment variable. Only by specifing a file via the ENV environment variable.

Page 12: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Redirection and PipesRedirection and Pipes

Page 13: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Common Features to Bourne, Common Features to Bourne, Korn, and C ShellsKorn, and C Shells

Symbol/CommandSymbol/Command Meaning/ActionMeaning/Action>> Redirect outputRedirect output>>>> Append to fileAppend to file<< Redirect inputRedirect input<<<< “Here” document (redirect input)“Here” document (redirect input)|| Pipe outputPipe output|&|& Start a coprocess (Korn shell only)Start a coprocess (Korn shell only)&& Run process in backgroundRun process in background;; Separate commands on same lineSeparate commands on same line** Match any character(s) in filenameMatch any character(s) in filename?? Match single character in filenameMatch single character in filename[ ][ ] Match any characters enclosedMatch any characters enclosed( )( ) Execute in subshellExecute in subshell` `` ` Substitute output of enclosed commandSubstitute output of enclosed command“ ““ “ Partial quote(allows var. and command Partial quote(allows var. and command

expansionexpansion‘ ‘‘ ‘ Full quote(no expansion)Full quote(no expansion)

Page 14: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Common Features to Bourne, Common Features to Bourne, Korn, and C Shells(2)Korn, and C Shells(2)

Symbol/CommandSymbol/Command Meaning/ActionMeaning/Action\\ Quote following characterQuote following character$$ varUse value for variablevarUse value for variable$$$$ Process IDProcess ID$0$0 Command nameCommand name$n$n nth argument (0<=n<=9)nth argument (0<=n<=9)$*$* All arguments as simple wordsAll arguments as simple words## Begin commentBegin commentbgbg Background executionBackground executionbreakbreak Break from loop statementsBreak from loop statementscdcd Change directoryChange directorycontinuecontinue Resume a program loopResume a program loopechoecho Display outputDisplay outputevaleval Evaluate argumentsEvaluate argumentsexecexec Execute a new shellExecute a new shellfgfg Foreground executionForeground execution

Page 15: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Common Features to Bourne, Common Features to Bourne, Korn, and C Shells(3)Korn, and C Shells(3)

Symbol/CommandSymbol/Command Meaning/ActionMeaning/Actionjobsjobs Show active jobsShow active jobs

killkill Terminate running jobsTerminate running jobs

shiftshift Shift positional parametersShift positional parameters

stopstop Suspend a background jobSuspend a background job

suspendsuspend Suspend a foreground jobSuspend a foreground job

timetime Time a commandTime a command

umaskumask Set default file permissions for new filesSet default file permissions for new files

unsetunset Erase variable or function definitionsErase variable or function definitions

waitwait Wait for a background job to finishWait for a background job to finish

Page 16: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Differing Features of Bourne, Differing Features of Bourne, Korn, and C ShellsKorn, and C Shells

shsh kshksh cshcsh Meaning/ActionMeaning/Action$$ $$ %% PromptPrompt

>|>| >!>! Force redirectionForce redirection>file 2>&1>file 2>&1 >file 2>&1>file 2>&1 >& file>& file Combine stdout and Combine stdout and

stderrstderr{ }{ } Expand elements in Expand elements in

listlist` `` ` ` `` ` ` `` ` Substitute output of Substitute output of

enclosed commandenclosed command$( )$( ) Substitute output of Substitute output of

enclosed commandenclosed command$HOME$HOME $HOME$HOME $home$home Home directoryHome directory

~~ ~~ Home directory Home directory symbolsymbol

var=valuevar=value var=valuevar=value set var=valueset var=value Variable assignmentVariable assignmentexport varexport var export var=valexport var=val setenv var valsetenv var val Set environment Set environment

variablevariable${nn}${nn} More than nine args More than nine args

can be referencedcan be referenced““$@”“$@”“ $@”$@” All args as separate All args as separate

wordswords$#$# $#$# $#argv$#argv Number of argumentsNumber of arguments

Page 17: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Differing Features of Bourne, Differing Features of Bourne, Korn, and C Shells(2)Korn, and C Shells(2)

shsh kshksh cshcsh Meaning/ActionMeaning/Action$?$? $?$? $status$status Exit statusExit status$!$! $!$! Background exit statusBackground exit status$-$- $-$- Current optionsCurrent options. file. file . file. file source filesource file Read commands in fileRead commands in file

alias x=yalias x=y alias x yalias x y Name x stands for yName x stands for ycasecase casecase switch/caseswitch/case Choose alternativesChoose alternatives

cd ~-cd ~- popd/pushdpopd/pushd Switch directoriesSwitch directoriesdonedone donedone endend End a loop statementEnd a loop statementesacesac esacesac endswendsw End case or switchEnd case or switchexit [n]exit [n] exit [n]exit [n] exit [(expr)]exit [(expr)] Exit with a statusExit with a statusfor/dofor/do for/dofor/do foreachforeach Loop through variablesLoop through variables

print –rprint –r globglob Ignore echo escapesIgnore echo escapeshashhash alias –talias –t hashstathashstat Display hashed Display hashed

commentscommentshash cmdshash cmds alias –t cmdsalias –t cmds rehashrehash Remember command Remember command

locationslocationshash –rhash –r PATH=$PATHPATH=$PATH unhashunhash Forget command Forget command

locationslocationshistoryhistory historyhistory List previous List previous

commandscommands

Page 18: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Differing Features of Bourne, Differing Features of Bourne, Korn, and C Shells(3)Korn, and C Shells(3)

shsh kshksh cshcsh Meaning/ActionMeaning/Actionrr !!!! Redo previous Redo previous

commandcommand

r strr str !str!str Redo command that Redo command that starts with str\starts with str\

r x=cmdr x=cmd !cmd:s/x/y!cmd:s/x/y Edit command, then Edit command, then executeexecute

if [ $i –eq 5]if [ $i –eq 5] if ((i==5))if ((i==5)) if ($i==5)if ($i==5) Sample if statementSample if statement

fifi fifi endifendif End if statementEnd if statement

ulimitulimit ulimitulimit limitlimit Set resource limitsSet resource limits

pwdpwd pwdpwd dirsdirs Print working directoryPrint working directory

readread readread $<$< Read from standard Read from standard inputinput

trap 2trap 2 trap 2trap 2 onintronintr Ignore interruptsIgnore interrupts

unaliasunalias unaliasunalias Remove aliasesRemove aliases

until/dountil/do until/dountil/do Begin until loopBegin until loop

while/dowhile/do while/dowhile/do whilewhile Begin while loopBegin while loop

Page 19: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Shell control structuresShell control structuresStructureStructure BourneBourne KornKorn C ShellC ShellIFIF if[…]if[…] if[…]if[…] if(…) then if(…) then THENTHEN thenthen thenthenELSE-IFELSE-IF elifelif elifelif Else ifElse ifELSEELSE elseelse elseelse elseelseENDIFENDIF fifi fifi endifendifCASECASE casecase casecase switchswitch value) value) value) value) case value:case value: ;; ;; ;; ;; breakswbreaksw * ) * ) * ) * ) default:default:

esacesac esacesac endswendswFORFOR forfor forfor foreachforeach

DoDo dododonedone donedone endend

REPEATREPEAT xargs -1xargs -1 xargs -1xargs -1 xargs -1xargs -1repeat repeat

UNTILUNTIL untiluntil untiluntilDoDo dododonedone donedone

WHILEWHILE whilewhile whilewhile whilewhileDoDo dododonedone donedone endend

Page 20: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

ConclusionConclusion

Numerous shells but all essential to the Numerous shells but all essential to the evolution of the Unix system and the shellevolution of the Unix system and the shell

Build off of each otherBuild off of each other

Main building block for all shells in one Main building block for all shells in one form or another is the Bourne Shellform or another is the Bourne Shell

Page 21: Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.

Any Questions?Any Questions?