|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
| "The lyfe so short, the craft so long to lerne,''
Chaucer c. 1340–1400 |
This collection of links is oriented on students (initially it was provided as a reference material to my shell programming university course) and is designed to emphasize usage of advanced shell constructs and pipes in shell programming (mainly in the context of ksh93 and bash 3.2+ which have good support for those constructs). An introductory paper Slightly Skeptical View on Shell discusses the shell as a scripting language and as one of the earliest examples of very high level languages. The page might also be useful for system administrators who constitute the considerable percentage of shell users and lion part of shell programmers.
This page is the main page to a set of sub-pages devoted to shell that collectively are known as Shellorama. The most important are:
Language page - describes some of the exotic shell contracts and provides links to web resources about them. Bash 3.x added several useful extensions. Among them (Bash Reference Manual ):
$(( expression ))
Older (( ... )) construct borrowed from ksh93 is also supported.
=~ operator.
An additional binary operator, ‘=~’,
is available, with the same precedence as ‘==’
and ‘!=’. When it is used,
the string to the right of the operator is considered an extended
regular expression and matched accordingly (as in regex3)).
The return value is 0 if the string matches the pattern, and 1 otherwise.
If the regular expression is syntactically incorrect, the conditional
expression's return value is 2. If the shell option nocasematch
(see the description of shopt in
Bash Builtins) is enabled, the match
is performed without regard to the case of alphabetic characters.
Substrings matched by parenthesized sub-expressions within the regular
expression are saved in the array variable BASH_REMATCH.
The element of BASH_REMATCH with index 0 is the portion
of the string matching the entire regular expression. The element
of BASH_REMATCH with index n is the portion
of the string matching the nth parenthesized sub-expression.
C-style for loop. Bash implements the
for (( expr1 ; expr2
; expr3 )) loop, similar to the C
language (see
Looping Constructs).
bash$ echo a{d,c,b}e
ade ace abe
~N -- The string that would
be displayed by ‘dirs +N’
~+N -- The string that would be
displayed by ‘dirs +N’~-N -- The string that
would be displayed by ‘dirs -N’
which is rather convenient for implementing "directory favorites"
concept with "push/pop/dirs" troika
Debugging Current bash has the best debugger and from this point of view represents the best shell. Until Actually for ksh93 absence of the debugger is more a weakness, it is a blunder and it is strange that such talented person David Korn did not realize this.
I strongly recommend getting a so-called orthodox file manager (OFM). This tool can immensely simplify Unix filesystem navigation and file operations (Midnight Commander while defective in handling command line can be tried first as this is an active project and it provides fpt and sftp virtual filesystem in remote hosts)
Actually filesystem navigation in shell is an area of great concern as there are several serious problems with the current tools for Unix filesystem navigation. I would say that usage of cd command (the most common method) is conceptually broken and deprives people from the full understanding of Unix filesystem; I doubt that it can be fixed within the shell paradigm (C-shell made an attempt to compensate for this deficiency by introducing history and popd/pushd/dirs troika, but this proved to be neither necessary nor sufficient for compensating problems with the in-depth understanding of the classical Unix hierarchical filesystem inherent in purely command line navigation ;-). Paradoxically sysadmins who use OFMs usually have much better understanding of the power and flexibility of the Unix filesystem then people who use command line. All-in-all usage of OFM is system administration represents Eastern European school of administration and it might be a better way to administer system that a typical "North American Way".
The second indispensable tool for shell programmer is Expect. This is a very flexible application that can be used for automation of interactive sessions as well as automation of testing of applications.
Usually people who know shell and awk and/or Perl well are usually considered to be advanced Unix system administrators (this is another way to say the system administrators who does not know shall/awk/Perl troika well are essentially a various flavors of entry-level system administrators no matter how many years of experience they have). I would argue that no system administrator can consider himself to be a senior Unix system administrator without in-depth knowledge of both one of the OFMs and Expect.
|
No system administrator can consider himself to be a senior Unix system administrator without in-depth knowledge of both one of the OFMs and Expect. |
An OFM tends to educate the user about the Unix filesystem in some subtle, but definitely psychologically superior way. Widespread use of OFMs in Europe, especially in Germany and Eastern Europe, tend to produce specialists with substantially greater skills at handling Unix (and Windows) file systems than users that only have experience with a more primitive command line based navigational tools.
And yes, cd navigation is conceptually broken. This is not a bizarre opinion of the author, this is a fact: when you do not even suspect that a particular part of the tree exists something is conceptually broken. People using command line know only fragments of the file system structure like blinds know only the parts of the elephant. Current Unix file system with, say, 13K directories for a regular Solaris installation, are just unsuitable for the "cd way of navigation"; 1K directories was probably OK. But when there are over 10K of directories you need something else. Here quantity turns into quality. That's my point.
The page provides rather long quotes as web pages as web pages are notoriously unreliable medium and can disappear without trace. That makes this page somewhat difficult to browse, but it's not designed for browsing; it's designed as a supplementary material to the university shell course and for self-education.
|
Note:
A highly recommended shell site is
SHELLdorado
by Heiner Steven. |
A complementary page with Best Shell Books Reviews is also available. Although the best book selection is to a certain extent individual, the selection of a bad book is not: so this page might at least help you to avoid most common bad books (often the book recommended by a particular university are either weak or boring or both; Unix Shell by Example is one such example ;-). Still the shell literature is substantial (over a hundred of books) and that mean that you can find a suitable textbook. Please be aware of the fact that that few authors of shell programming books have a broad understanding of Unix necessary for writing a comprehensive shell book.
IMHO the first edition of O'Reilly Learning Korn Shell is probably one of the best and contains nice set of examples (the second edition is more up to date but generally is weaker). Also the first edition has advantage of being available in HTML form too (O'Reilly Unix CD). It does not cover ksh93 but it presents ksh in a unique way that no other book does. Some useful examples can also be found in UNIX Power Tools Book( see Archive of all shell scripts (684 KB); the book is available in HTML from one of O'Reilly CD bookshelf collections).
Still one needs to understand that Unix shells are pretty archaic languages which were designed with compatibility with dinosaur shells in mind (and Borne is a dinosaur shell by any definition). Designers even such strong designers as David Korn were hampered by compatibility problems from the very beginning (in a way it is amazing how much ingenuity they demonstrate in enhancing Borne shell; I am really amazed how David Korn managed to extend borne shell into something much more usable and much loser to "normal" scripting language. In this sense ksh93 stands like a real pinnacle of shell compatibility and the the testament of the art of shell language extension).That means that outside of interactive usage and small one page scripts they generally outlived their usefulness. That's why for more or less complex tasks Perl is usually used (and should be used) instead of shells. While shells continued to improve since the original C-shell and Korn shell, the shell syntax is frozen in space and time and now looks completely archaic. There are a large number of problems with this syntax as it does not cleanly separate lexical analysis from syntax analysis. Bash 3.2 actually made some progress of overcoming most archaic features of old shells but still it has it own share of warts (for example last stage of the pipe does not run in on the same level as encompassing the pipe script)
Some syntax features in shell are idiosyncratic as Steve Bourne played with Algol 68 before starting work on the shell. In a way, he proved to be the most influential bad language designer, the designer who has the most lasting influence on Unix environment (that does not exonerate the subsequent designers which probably can take a more aggressive stance on the elimination of initial shell design blunders by marking them as "legacy").
For example there is very little logic in how different types of blocks are delimitated in shell scripts. Conditional statements end with (broken) classic Algor-68 the reverse keyword syntax: 'if condition; then echo yes; else echo no; fi', but loops are structured like perverted version of PL/1 (loop prefix do; ... done;) , individual case branches blocks ends with ';;' . Functions have C-style bracketing "{", "}". M. D. McIlroy as Steve Borne manager should be ashamed. After all at this time the level of compiler construction knowledge was pretty sufficient to avoid such blunders (David Gries book was published in 1971) and Bell Labs staff were not a bunch of enthusiasts ;-).
Also the original Bourne shell was a almost pure macro language. It performed variable substitution, tokenization and other operations on one line at a time without understanding the underlying syntax. This results in many unexpected side effects: Consider a simple commandrm $fileIf variable $file is accidentally contains space that will lead to treating it as two separate augments to the rm command with possible nasty side effects. To fix this, the user has to make sure every use of a variable in enclosed in quotes, like in rm "$file".
Variable assignments in Bourne shell are whitespace sensitive. 'foo=bar' is an assignment, but 'foo = bar' is not. It is a function call with "= "and "bar" as two arguments. This is another strange idiosyncrasy.
There is also an overlap between aliases and functions. Aliases are positional macros that are recognized only as the first word of the command like in classic alias ll='ls -l'. Because of this, aliases have several limitations:
ll() { ls -l $*; }
The curly brackets are some sort of pseudo-commands, so skipping the semicolon
in the example above results in a syntax error. As there is no clean separation
between lexical analysis and syntax analysis removing the whitespace
between the opening bracket and 'ls' will also result in a syntax error.Since the use of variables as commands is allowed, it is impossible to reliably check the syntax of a script as substitution can accidentally result in key word as in example that I found in the paper about fish (not that I like or recommend fish):
if true; then if [ $RANDOM -lt 1024 ]; then END=fi; else END=true; fi; $ENDBoth bash and zsh try to determine if the command in the current buffer is finished when the user presses the return key, but because of issues like this, they will sometimes fail.
Dr. Nikolai Bezroukov
| 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 |
Interesting featured are -l -u options in declare statement (automatic conversion to lower case/upper case).
n. The -p option to `declare' now displays all variable values and attributes
(or function values and attributes if used with -f).
o. There is a new `compopt' builtin that allows completion functions to modify
completion options for existing completions or the completion currently
being executed.
p. The `read' builtin has a new -i option which inserts text into the reply
buffer when using readline.
s. Changed format of internal help documentation for all builtins to roughly
follow man page format.
t. The `help' builtin now has a new -d option, to display a short description,
and a -m option, to print help information in a man page-like format.
u. There is a new `mapfile' builtin to populate an array with lines from a
given file. The name `readarray' is a synonym.
w. There is a new shell option: `globstar'. When enabled, the globbing code
treats `**' specially -- it matches all directories (and files within
them, when appropriate) recursively.
x. There is a new shell option: `dirspell'. When enabled, the filename
completion code performs spelling correction on directory names during
completion.
dd. The parser now understands `|&' as a synonym for `2>&1 |', which redirects
the standard error for a command through a pipe.
ee. The new `;&' case statement action list terminator causes execution to
continue with the action associated with the next pattern in the
statement rather than terminating the command.
hh. There are new case-modifying word expansions: uppercase (^[^]) and
lowercase (,[,]). They can work on either the first character or
array element, or globally. They accept an optional shell pattern
that determines which characters to modify. There is an optionally-
configured feature to include capitalization operators.
ii. The shell provides associative array variables, with the appropriate
support to create, delete, assign values to, and expand them.
jj. The `declare' builtin now has new -l (convert value to lowercase upon
assignment) and -u (convert value to uppercase upon assignment) options.
There is an optionally-configurable -c option to capitalize a value at
assignment.
kk. There is a new `coproc' reserved word that specifies a coprocess: an
asynchronous command run with two pipes connected to the creating shell.
Coprocs can be named. The input and output file descriptors and the
PID of the coprocess are available to the calling shell in variables
with coproc-specific names.
Changes: Fairly extensive coverage of the version 4.0 Bash release. A great deal of other new material and bugfixes. This is a very important update.
BashStyle-NG is a graphical tool for changing Bash's behavior and look and feel. It can also style Readline, Nano, and Vim.
It ships with a set of scripts, which are used by the styles shipped with BS-NG, but can also be used separately. Since v6.3 you have the opportunity to create your own prompts. For important notes on how to do so, refer back to the documentation.If you don’t understand an option or something does not work as expected read the documentation. It’s installed in /usr/share/doc/bashstyle-ng/index.html ( /usr is default but may vary if you passed an other prefix to configure).
Currently BS-NG ships 15 pre-defined Styles for your prompt. Most of them can be modified via the Custom Prompt Builder. If you want to save your current configuration and want to re-import it later (or for using it on a different user/machine) you can do so via the Profiler (bs-ng-profiler –help).
Standalone (GConf-Free) Configuration (for faster Bash-Startup) can be created via the RCGenerator (rcgenerator –help).
by Randal K. Michael (Author)
From foreword:
We urge everyone to study this entire book. Every chapter hits a different topic
using a different approach. The book is written this way to emphasize that there is
never only one technique to solve a challenge in UNIX. All the shell scripts in this book
are real-world examples of how to solve a problem. Thumb through the chapters, and
you can see that we tried to hit most of the common (and some uncommon!) tasks
in UNIX. All the shell scripts have a good explanation of the thinking process, and
we always start out with the correct command syntax for the shell script targeting a
specific goal. I hope you enjoy this book as much as I enjoyed writing it. Let’s get
started!
A must-have for all levels of *nix users. ,
August 1, 2004
By Bindlestiff (rixtertech.com) - See all my reviews
This review is from: Mastering UNIX Shell Scripting (Paperback)The breadth of real-world examples make the difference between this book and most reference texts. It's true that it's written for korn, but I've had little trouble adapting for Bash; many of the scripts run almost unchanged and the ones that don't provide a useful opportunity for exercise in adaptation. The authors prose is clear. His attitude is a bit challenging; he says early on that that his intention is to teach you how to -solve problems- by shell scripting, NOT to present a ream of canned solutions. This is NOT a reference text for any particular shell, you'll still need plenty of O'Reilly books, a web browser & etc.
This book has enabled me to write a major project using scripting as the glue to hold together a hefty mass of file-moving daemons, fax/paging engines, python UI code, PostGreSQL database engine, networking/email, SSH, and Expect scripts on a Gnu Linux platform. I absolutely could not have done it without this book and I'm very grateful to Mr Michael for his work. If a later edition could more closely serve the needs of the masses by presenting more Bash examples and maybe throwing in a CD it would be a 5-star text.
Quote
#, precedes each line of a comment.Command readability and step-by-step comments are just the very basics of a
well-written script. Using a lot of comments will make our life much easier when we
have to come back to the code after not looking at it for six months, and believe me; we
will look at the code again. Comment everything! This includes, but is not limited to,
describing what our variables and files are used for, describing what loops are doing,
describing each test, maybe including expected results and how we are manipulating
the data and the many data fields. A hash mark,
The
script stub that follows is on this book’s companion web site at www.wiley.com/go/michael2e
. The name is script.stub. It has all the comments ready to get startedwriting a shell script. The
script.stub file can be copied to a new filename. Edit thenew filename, and start writing code. The
script.stub file is shown in Listing 1-1.#!/bin/Bash
#
# SCRIPT:
NAME_of_SCRIPT# AUTHOR:
AUTHORS_NAME# DATE:
DATE_of_CREATION# REV:
1.1.A (Valid are A, B, D, T and P)#
(For Alpha, Beta, Dev, Test and Production)#
# PLATFORM: (SPECIFY: AIX, HP-UX, Linux, OpenBSD, Solaris
# or Not platform dependent)
#
# PURPOSE: Give a clear, and if necessary, long, description of the
# purpose of the shell script. This will also help you stay
# focused on the task at hand.
#
# REV LIST:
# DATE:
DATE_of_REVISION# BY:
AUTHOR_of_MODIFICATION# MODIFICATION:
Describe what was modified, new features, etc--#
#
# set -n # Uncomment to check script syntax, without execution.
# # NOTE: Do not forget to put the comment back in or
# # the shell script will not execute!
# set -x # Uncomment to debug this shell script
#
##########################################################
# DEFINE FILES AND VARIABLES HERE
##########################################################
Listing 1-1
script.stub shell script starter listingMichael c01.tex V4 - 03/24/2008 4:45pm Page 8
8 PartI■ The Basics of Shell Scripting
##########################################################
# DEFINE FUNCTIONS HERE
##########################################################
##########################################################
# BEGINNING OF MAIN
##########################################################
# End of script
Listing 1-1
(continued)The shell script starter shown in Listing 1-1 gives you the framework to start writing
the shell script with sections to declare variables and files, create functions, and write
the final section,
BEGINNING OF MAIN, where the main body of the shell script iswritten.
About: The Bash Debugger (bashdb) is a debugger for Bash scripts. The debugger command interface is modeled on the gdb command interface. Front-ends supporting bashdb include GNU-Emacs and ddd. In the past, the project has been used as a springboard for other experimental features such as a timestamped history file (now in Bash versions after 3.0).Changes: This major rewrite and reorganization of code has numerous bugfixes and has been tested on bash 3.1 and 4.0 alpha. With the introduction of a simple debugger command alias mechanism, there are some incompatibilities. Command aliasing of short commands is no longer hard-wired. New commands of note are "set autoeval" and "step+", taken from ruby-debug. Emacs support has been greatly improved. Long option command-processing is guaranteed.
BashDiff is a patch for the bash shell that can do an amazing number of things. It extends existing bash features, brings a few of awk's tricks into the shell itself, exposes some common C functions to bash shell programming, adds an exception mechanism, provides features of functional programming such as list comprehension and themapfunction, lets you talk with GTK+2 and databases, and even adds a Web server right into the standard bash shell.There are no packages of BashDiff in the openSUSE, Fedora, or Ubuntu repositories. I'll build from source using BashDiff 1.45 on an x86 Fedora 9 machine running bash 3.0. While versions 3.1 and 3.2 of bash are available, the 1.45 BashDiff patch does not apply cleanly to either.
Recreational scripts were added, such as an almost full-featured Perquacky clone script, a script that does the "Petals Around the Rose" puzzle, and a crossword puzzle solver script. There is also the usual batch of bugfixes and other new material.
These are simple stand-alone scripts. You are unlikely to find anything very impressive here if you already hack your own scripts, but a novice might find some of the ideas new.
|
Please visit Heiner Steven SHELLdorado ; the best shell scripting site on the Internet |
***** SHELLdorado -- An excellent site by Heiner Steven. Very cute name that catches the fact that the shell is a crown jewel of Unix ;-) IMHO this is the best shell-related site on Internet. Actively maintained. Highly recommended ! Note: This site now has a bulletin that you can subscribe to [Feb 02, 2002]
***** The official Korn Shell Web Site by David Korn, the author of ksh88 and ksh93. See also his son's Home Page (actually I think that Tksh implementation is the best free shell available, but ksh93 is better supported).
***+ Open Directory - Computers Operating Systems Unix Shell -- a decent collection of links. Could be better...
**** Unix shell scripts by Paul Dunne. contains links and several useful scripts.
scripting -- this is a bellatlantic.net member site and can be down
***+ kshweb.html-- a good site by Dana French
*** dotfiles.com home -- several dot files of very uneven (often low) quality. Still better than nothing...
docs.sun.com man pages section 1 User Commands - ksh
comp.unix.shell -- the only Usenet group about shells
Note: see first Unix FAQshell Index -- it's probably more up-to-date than this document
See also additional material at
Shell Command Line History Substitution -- a very important facility to learn !!!
Collections of reference materials, cards, etc
Reference cards:
Reference manuals
Public domain Korn Shell (pdksh)
Public domain Korn Shell is closer to ksh88 than ksh93. It has most of the ksh88 features, not much of the ksh93 features, and a number of its own features.
Old Korn shell (Kornshell 88)
References
Another open source shell that is actively developed. Probably the most
powerful free Unix shell but it definitely suffers from feature creep.
Not 100% compatible with ksh93 or bash. Many of the useful features of bash,
ksh93, and tcsh were incorporated into zsh; many original features were
added (see
An Introduction to the Z Shell ). Zsh was originally written by
Paul Falstad while he was a student in Princeton. Zsh is now
maintained by the members of the zsh workers mailing list zsh-workers@math.gatech.edu.
The development is currently coordinated by Zoltan Hidvegi, hzoli@cs.elte.hu.
Z-shell is very popular in Europe. Not so much in the USA.
Advantages of Zsh include:
Main WEB sites include:
Most scripting languages can be used as shell. Experimental shells exist for Perl, Scheme, but only TCL mange to get into commercial grade product (Tksh). Javascript can be used as shell for NT.
Tclsh
Scsh (a Unix Scheme shell) FAQ
Perl Shell Gregor N. Purdy - November 23rd 1999, 18:03 EST
The Perl Shell (psh) is an interactive command-line Unix shell that aims to bring the benefits of Perl scripting to a shell context and the shell's interactive execution model to Perl.
Changes: This version of the Perl Shell adds significant functionality. However, it is still an early development release. New features include rudimentary background jobs handling and job management, signal handling, filename completion, updates to history handling, flexible %built_ins mechanism for adding built-in functions and smart mode is now on by default.
A modular Perl shell written, configured, and operated entirely in Perl. It aspires to be a fully operational login shell with all the features one normally expects. But it also gives direct access to Perl objects and data structures from the command line, and allows you to run Perl code within the scope of your command line. And it's named after one of the greatest characters on Futurama, so it must be good...
A very interesting shell with advanced piping support.
Lost Links
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Last modified: November 27, 2009