Linux shell scripting intro/review David Morgan You should already know how to log in run programs at the command line use pipelines and redirection ( < > ) put jobs in the background ( & ) create and edit files make scripts executable ( chmod ) 1
UNIX software tools do one thing well process lines of text, not binary use regular expressions default to standard I/O aren t chatty give same output format as got from input let someone else do the hard part let someone do the hard part -- subcontractors The shell s dependence on other programs to do most of the work is arguably a defect, but also inarguably a strength: you get the concise notation of a scripting language plus the speed and efficiency of programs written in C (etc.) --Robbins and Beebe p. ix specialized subcontractor examples sed for dynamic editing find for selective, arbitrary file processing bc for arbitrary precision math they lack the shell s programming constructs the shell lacks their specialized capabilities strategy: employ them in mutual alliance 2
Command shell definition a shell is just another program, like cal or ls defined by what it does cal: prints a calendar ls: prints a file list shell: prompts for commands, tries to run them running commands for users is what, precisely, makes a shell a shell (and not an editor, browser, or calculator) What features does a shell have? Primary! Essential! Must have ability to cause a command to run distant second Nice to have whatever other features were coded into it by that shell s author. If any. I don t know. 3
Scripting in context Brief feature summary for bash: Command processing parse expand execute I/O redirection Piping Environment control Background processing Shell scripts Scripting: a possible feature that a shell may implement bash, for example, does (but don t take it for granted.) dshell a feature-poor shell causes commands to run (so, it s a shell!) nothing else (certainly not scripting) 4
bash a feature-rich rich shell causes commands to run (so, it s a shell!) but has a zillion times more code doing tons of useful extra things (all optional) Some other, tutorial shells dshell my dirt-simple shell simpleshell http://teaching.idallen.org csimpleshell http://rik0.altervista.org/snippets/csimpleshell.html mini-shell http://code.google.com/p/mini-shell 5
Bash source code Prominent production shells Korn shell C shell Bourne again shell Z shell Almquist shell from AT&T Berkeley GNU Princeton student Ken Almquist 6
Shells installed on our systems ksh tcsh bash zsh mksh dash yash ksh KSH-93 is the most recent version of the KornShell by David Korn of AT&T Bell Laboratories. KornShell is a shell programming language, which is upward compatible with "sh" (the Bourne Shell). tcsh Tcsh is an enhanced but completely compatible version of csh, the C shell. Tcsh is a command language interpreter which can be used both as an interactive login shell and as a shell script command processor. Tcsh includes a command line editor, programmable word completion, spelling correction, a history mechanism, job control and a C language like syntax. 7
bash The GNU Bourne Again shell (Bash) is a shell or command language interpreter that is compatible with the Bourne shell (sh). Bash incorporates useful features from the Korn shell (ksh) and the C shell (csh). Most sh scripts can be run by bash without modification. zsh The zsh shell is a command interpreter usable as an interactive login shell and as a shell script command processor. Zsh resembles the ksh shell (the Korn shell), but includes many enhancements. Zsh supports command line editing, built-in spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and more. mksh mksh is the MirBSD enhanced version of the Public Domain Korn shell (pdksh), a bourne-compatible shell which is largely similar to the original AT&T Korn shell. It includes bug fixes and feature improvements in order to produce a modern, robust shell good for interactive and especially script use, being a bourne shell replacement, pdksh successor and an alternative to the C shell. dash DASH is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. It does this without sacrificing speed where possible. In fact, it is significantly faster than bash (the GNU Bourne-Again SHell) for most tasks. 8
yash Yash is a command line shell that conforms to the POSIX.1 (IEEE Std 1003.1, 2008 Edition) standard for the most part. Yash also has its own features beyond POSIX, such as: * global aliases * random numbers * socket redirections and other special redirections * right prompt * command completion Bash conformant-izability --posix or -o posix +B get rid of brace expansion calling bash as /bin/sh softlink 9
Which shell to script for? #!/path/to/shell 10