Linux Shell Scripting –Intro/Review
Total Page:16
File Type:pdf, Size:1020Kb
Linux shell scripting –intro/review David Morgan © David Morgan 2012-17 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 ) © David Morgan 2012-17 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 © David Morgan 2012-17 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 © David Morgan 2012-17 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) © David Morgan 2012-17 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. © David Morgan 2012-17 3 Scripting in context Brief feature summary for bash: Command processing – parse – expand – execute Scripting: a possible feature that a shell may implement I/O redirection bash, for example, does (but don’t take it for granted.) Piping Environment control Background processing Shell scripts © David Morgan 2012-17 dshell – a feature -poor shell causes commands to run (so, it’s a shell!) nothing else (certainly not scripting) © David Morgan 2012-17 4 bash – a feature -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) © David Morgan 2012-17 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 © David Morgan 2012-17 5 Bash source code © David Morgan 2012-17 Prominent “production ” shells Korn shell from AT&T C shell Berkeley Bourne again shell GNU Z shell Princeton student Almquist shell Ken Almquist © David Morgan 2012-17 6 Shells installed on our systems ksh tcsh bash zsh mksh dash yash © David Morgan 2012-17 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. © David Morgan 2012-17 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. © David Morgan 2012-17 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. © David Morgan 2012-17 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 © David Morgan 2012-17 Bash conformant -izability --posix or -o posix +B get rid of brace expansion calling bash as /bin/sh softlink © David Morgan 2012-17 9 Which shell to script for? #!/path/to/shell © David Morgan 2012-17 10.