This shell script and shell programming site aims to provide book reviews and free ebook on bash shell, korn shell, shell commands, linux shell, awk shell, unix commands, ftp shell and all other shells.

Programming the UNIX/linux Shell

Claude Cantin (claude.cantin@nrc.ca)
http://www.nrc.ca/imsb/rcsg
This document was produced by Claude Cantin of the National Research Council of Canada. Reproductions are permitted for non-profit purposes provided the origin of the document is acknowledged.

Programming the Shell

This chapter will concentrate on programming the shells. All examples shown will use the Bourne shell. Most shell scripts are written in the Bourne shell because it is the only shell found on *ALL* UNIX systems. Scripts written in the Korn shell are gaining popularity.

C shell scripts are possible, but not recommended. Although the C shell is great for interactive work, it has many drawbacks in script programming.

Bourne - C shell comparisons
A Bourne Shell Script is a file containing a series of Bourne Shell commands, as well as control structures such as if statements, while loops, etc. Parameters can be passed to the script and data can be read from the keyboard.
Bourne shell scripts are usually Algol-66 look-alikes, whereas C shell scripts look more like C program constructs.
The C shell is slower to start execution (as it looks in the .cshrc file EVERY TIME it is called) and produces a hash table of all paths for faster execution. The Bourne shell is slower, but starts executing immediately.
For that reason, short scripts are executed much faster in the Bourne Shell than the C shell (this is noticeable for small SLOW machines; with systems running at 30 to 130 MIPS/CPU, the difference in speed is almost insignificant). Large scripts should most likely be written in a compiled language--preferably C--because compiled code executes much faster than interpreted code.
Many features of the Bourne shell are also found in the C shell. Parameters are interpreted in the same way. Variable assignments are very similar. UNIX commands are exactly the same. Control structures and comparisons differ the most.

Writing Shell Scripts

by William Shotts, Jr.
Here is where the fun begins
With the thousands of commands available for the command line user, how can you remember them all? The answer is, you don't. The real power of the computer is its ability to do the work for you. To get it to do that, we use the power of the shell to automate things. We write scripts.
Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. In addition to the things you have learned so far, the shell also provides a variety of useful programming features to make your scripts truly powerful.
What are scripts good for? A wide range of tasks can be automated. Here are some of the things I automate with scripts:
  • A script gathers up all the files (over 2200) in this site on my computer and transmits them to my web server.
  • The SuperMan pages are built entirely by a script.
  • Every Friday night, all my computers copy their files to a "backup server" on my network. This is performed by a script.
  • A script automatically gets the current updates from my Linux vendor and maintains a repository of vital updates. It sends me an email message with a report of tasks that need to be done.

As you can see, scripts unlock the power of your Linux machine. So let's have some fun!

Click to Read More

Shell Scripts and Awk

By Tim Love
Shell Programming
The `shell' is a process that lets you edit your command line input then runs the command. The shell isn't only a command line interpreter and line editor though, it's also a language with variables, arrays, functions and control structures. Command lines can be put into a file and executed. These so-called shell scripts can quickly be written and tested and should be tried in association with other standard unix utilities before embarking on a higher level language, at least for prototyping purposes.
Various shells are in use. sh, the Bourne Shell, is the oldest. The C-shell (csh) has many useful features lacking from sh but isn't that good for programming in. The Korn Shell (ksh) and the (very similar) POSIX shell are developments of sh that incorporates many csh features. bash is similar and is freely available (it's the default on linux and MacOS 10.3). This document is aimed at Korn Shell and POSIX shell users on CUED's Teaching System, though non-csh users elsewhere shouldn't have any problems.
Writing a shell script is easy - start up your editor with a file called try then type a few harmless commands into this file, one per line. For example
hostname
date
ls
then save it as text. You want to make this file executable, so in the terminal window type `chmod u+x try', adding eXecute permission for the Xser. Run this script by typing its name.
Even scripts as simple as this can save on repetitive typing, but much more can be easily achieved.

Shell Script Programming

by Matz Kindahl
Introduction
In the early days computers where used to run programs, and nothing more. You punched your program on cards, delivered the pack to the computer department. The staff there loaded the program into the computer, executed it and retrieved the result on paper. This was in turn returned to you and you had to sit down and figure out why you got the result you got.
Modern computers are a little more complex than that. You there have a complete environment where you can execute your programs and even have such astonishing things as interactive programs (hear-hear). It is no longer enough to be able to load your program and just print the result. You also need support to reformat the results, process them in other manners (maybe printing a nice diagram) and store them in a database. It would of course be possible to write specially designed programs that formatted the output of your programs according to your wishes, but the number of specialized programs would quickly increase, leaving your computer loaded with "might come in handy" programs.
A better approach would be to have a small set of processing programs together with a program made to "glue the parts together." On a UNIX system such a program is called the shell (in contrast with the core that contains time-sharing code, file access code and other system oriented code). The shell is used to issue commands, start processes, control jobs, redirect input and output, and other mundane things that you do on a modern computer. Not only that, the shell is a pretty complete programming language.
In this paper we will introduce concepts and methods that a good shell-programmer can use to get the most out of his/her UNIX system. We will start from the beginning, but a basic familiarity with programming and/or the basic principles of computers will be assumed. This is not an paper for the complete novice, although you are welcome to read the paper.

Shell Programming Part 2

Invoking The Shell The shell is a command and can be invoked just like a command (Don’t type the following command, it's only an example!).
sh proc [arg . . .] a shell is created to process proc
sh -v proc [arg . . .] same as previous but input lines are printed
proc [arg . . .] if proc is an executable script file, it is the same as typing sh proc [arg . . .]

Shell Script Files
These are text files that contain shell commands. The file must be flagged as executable using the chmod utility.
Type the following command
$ cat - > script
Enter the following lines as they appear below, finishing with the ctrl-d (that is the control key and the d key pressed at the same time) which will bring the shell prompt back.
echo The date is
date
ctrl-d
$
Flag the file as executable.
$ chmod +x script
Execute the file by typing
$ sh script
Introduction to Shell Scripting - Part 2 By Ben Okopnik
Read more

Shell Programming

by Katja and Guido Socher
In this article we explain how to write little shell scripts and give many examples.
Why shell programming?
Even though there are various graphical interfaces available for Linux the shell still is a very neat tool. The shell is not just a collection of commands but a really good programming language.You can automate a lot of tasks with it, the shell is very good for system administration tasks, you can very quickly try out if your ideas work which makes it very useful for simple prototyping and it is very useful for small utilities that perform some relatively simple tasks where efficiency is less important than ease of configuration, maintenance and portability.
So let's see now how it works:
Creating a script
There are a lot of different shells available for Linux but usually the bash (bourne again shell) is used for shell programming as it is available for free and is easy to use. So all the scripts we will write in this article use the bash (but will most of the time also run with its older sister, the bourne shell).
For writing our shell programs we use any kind of text editor, e.g. nedit, kedit, emacs, vi...as with other programming languages...

Click to Read More

Shell Command Language

The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group
The shell is a command language interpreter. This chapter describes the syntax of that command language as it is used by the sh utility and the system() and popen() functions in the XSH specification.
The shell operates according to the following general overview of operations. The specific details are included in the cited sections of this chapter.
The shell reads its input from a file (see sh), from the -c option or from the system() and popen() functions in the XSH specification. If the first line of a file of shell commands starts with the characters #!, the results are unspecified.
The construct #! is reserved for implementations wishing to provide that extension. A portable application cannot use #! as the first line of a shell script; it might not be interpreted as a comment.
The shell breaks the input into tokens: words and operators. (See Token Recognition .)
The shell parses the input into simple commands (see Simple Commands ) and compound commands (see Compound Commands ).
The shell performs various expansions (separately) on different parts of each command, resulting in a list of pathnames and fields to be treated as a command and arguments (see Word Expansions ).
The shell performs redirection (see Redirection ) and removes redirection operators and their operands from the parameter list.
The shell executes a function (see Function Definition Command ), built-in (see Special Built-in Utilities ), executable file or script, giving the names of the arguments as positional parameters numbered 1 to n, and the name of the command (or in the case of a function within a script, the name of the script) as the positional parameter numbered 0 (see Command Search and Execution ).
The shell optionally waits for the command to complete and collects the exit status (see Exit Status for Commands ).

Getting the Most From Your Shell

By Paul Dunne
Everyone uses it, but do they know how to use it to best advantage? I'm talking about the Unix shell. No matter whether you log in to a super-duper latest-thing X desktop or with a Wyse 30 over a slow modem connection, you will most likely at some point be using the shell. This article looks at how you can get the most from it.
  • Introduction
  • All Those Dot Files
  • Environment Variables
  • Aliases
  • Functions
  • Options
  • Start-up Commands
  • The Command Lne
  • Resources
  • Conclusion

Questions regarding this article should be directed to the author at paul.dunne@mailroom.com

While the rest of the world points and clicks in a scary little world of icons, all alike, we in the world of Unix get to use our good old CLI, or command line interface. One big reason why the CLI has remained so prevaisive in Unix environments is that it is actually damn good. Modern Unix shells are stable and powerful. This article looks at some methods to increase the power and usableness of your shell.

Introduction
So, I will be looking at how to get more out of your shell. I personally use a version of the Korn shell, pdksh, but most of what I say will be applicable to bash; where it isn't, I'll tell you so. As Zsh is mostly compatible with ksh, most of the article is useful for that as well. I will not bother with the C shell, on the grounds that I neither use it nor like it, and that it is so different as to be really the subjet of a seperate article. This article is not a shell tutorial for novices; it will presume that you know how to run commands, what wildcards are, and such stuff. What I will be doing is taking a look at some things that a lot of regular shell users don't realise can be done.

Click to Read More

An Introduction to the Unix Shell

Created by 'era' at iki.fi.
Mirrored by Steve Parker
steve-parker.org
The shell is a command programming language that provides an interface to the UNIX operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. Constructs such as while, if then else, case and for are available. Two-way communication is possible between the shell and commands. String-valued parameters, typically file names or flags, may be passed to a command. A return code is set by commands that may be used to determine control-flow, and the standard output from a command may be used as shell input.
The shell can modify the environment in which commands run. Input and output can be redirected to files, and processes that communicate through `pipes' can be invoked. Commands are found by searching directories in the file system in a sequence that can be defined by the user. Commands can be read either from the terminal or from a file, which allows command procedures to be stored for later use.

UNIX shell differences and how to change your shell

FAQ
faqs.org
Why change your shell
The UNIX shell is most people's main access to the UNIX operating system and as such any improvement to it can result in considerably more effective use of the system, and may even allow you to do things you couldn't do before. The primary improvement most of the new generation shells give you is increased speed. They require fewer key strokes to get the same results due to their completion features, they give you more information (e.g. showing your directory in your prompt, showing which files it would complete) and they cover some of the more annoying features of UNIX, such as not going back up symbolic links to directories.
A brief history of UNIX shells
Note, this history is just known to be slightly out of historical order, it is in the process of being corrected, but for the moment should be taken with a pinch of salt In the near beginning there was the Bourne shell /bin/sh (written by S. R. Bourne). It had (and still does) a very strong powerful syntactical language built into it, with all the features that are commonly considered to produce structured programs; it has particularly strong provisions for controlling input and output and in its expression matching facilities. But no matter how strong its input language is, it had one major drawback; it made nearly no concessions to the interactive user (the only real concession being the use of shell functions and these were only added later) and so there was a gap for something better.

ksh - Public domain Korn shell

by Eric Gisin
ksh is a command interpreter that is intended for both interactive and shell script use. Its command language is a superset of the sh(1) shell language.
Shell Startup
The following options can be specified only on the command line:
-c command-string
the shell executes the command(s) contained in command-string
-i
interactive mode - see below
-l
login shell - see below interactive mode - see below
-s
the shell reads commands from standard input; all non-option arguments are positional parameters
-r
restricted mode - see below
In addition to the above, the options described in the set built-in command can also be used on the command line.
If neither the -c nor the -s options are specified, the first non-option argument specifies the name of a file the shell reads commands from; if there are no non-option arguments, the shell reads commands from standard input. The name of the shell (i.e., the contents of the $0) parameter is determined as follows: if the -c option is used and there is a non-option argument, it is used as the name; if commands are being read from a file, the file is used as the name; otherwise the name the shell was called with (i.e., argv[0]) is used.
A shell is interactive if the -i option is used or if both standard input and standard error are attached to a tty. An interactive shell has job control enabled (if available), ignores the INT, QUIT and TERM signals, and prints prompts before reading input (see PS1 and PS2 parameters). For non-interactive shells, the trackall option is on by default (see set command below).

ZSH Documentation

Written by Paul Falstad
Co-ordinating by Peter Stephenson
zsh.sunsite.dk
Zsh is a UNIX command interpreter (shell) usable as an interactive login shell and as a shell script command processor. Of the standard shells, zsh most closely resembles ksh but includes many enhancements. Zsh has command line editing, builtin spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and a host of other features.
Invocation Options The following flags are interpreted by the shell when invoked to determine where the shell will read commands from:
-c
Take the first argument as a command to execute, rather than reading commands from a script or standard input. If any further arguments are given, the first one is assigned to $0, rather than being used as a positional parameter.
-i
Force shell to be interactive.
-s
Force shell to read commands from the standard input. If the -s flag is not present and an argument is given, the first argument is taken to be the pathname of a script to execute.
After the first one or two arguments have been appropriated as described above, the remaining arguments are assigned to the positional parameters.
For further options, which are common to invocation and the set builtin.
Options may be specified by name using the -o option. -o acts like a single-letter option, but takes a following string as the option name. For example,
zsh -x -o shwordsplit scr
runs the script scr, setting the XTRACE option by the corresponding letter `-x' and the SH_WORD_SPLIT option by name. Options may be turned off by name by using +o instead of -o. -o can be stacked up with preceding single-letter options, so for example `-xo shwordsplit' or `-xoshwordsplit' is equivalent to `-x -o shwordsplit'.

An Introduction to the Z Shell

By Paul Falstad
pfalstad@phoenix.princeton.edu
Department of Computer Science
Princeton University Princeton, NJ 08544
zsh is a shell designed for interactive use, although it is also a powerful scripting language. Many of the useful features of bash, ksh, and tcsh were incorporated into zsh; many original features were added. This document details some of the unique features of zsh. It assumes basic knowledge of the standard UNIX shells; the intent is to show a reader already familiar with one of the other major shells what makes zsh more useful or more powerful. This document is not at all comprehensive; read the manual entry for a description of the shell that is complete and concise, although somewhat overwhelming and devoid of examples.

A User's Guide to the Z-Shell

By Peter Stephenson
The Z-Shell, `zsh' for short, is a command interpreter for UNIX systems, or in UNIX jargon, a `shell', because it wraps around the commands you use. More than that, however, zsh is a particularly powerful shell --- and it's free, and under regular maintenance --- with lots of interactive features allowing you to do the maximum work with the minimum fuss. Of course, for that you need to know what the shell can do and how, and that's what this guide is for.
The most basic basics: I shall assume you have access to a UNIX system, otherwise the rest of this is not going to be much use. You can also use zsh under Windows by installing Cygwin, which provides a UNIX-like environment for programmes --- given the weakness of the standard Windows command interpreter, this is a good thing to do. There are ports of older versions of zsh to Windows which run natively, i.e. without a UNIX environment, although these have a slightly different behaviour in some respects and I won't talk about them further.
I'll also assume some basic knowledge of UNIX; you should know how the filesystem works, i.e. what /home/users/pws/.zshrc and ../file mean, and some basic commands, for example ls, and you should have experience with using rm to delete completely the wrong file by accident, and that sort of thing. In something like `rm file', I will often refer to the `command' (rm, of course) and the `argument(s)' (anything else coming after the command which is used by it), and to the complete thing you typed in one go as the `command line'.
You're also going to need zsh itself; if you're reading this, you may well already have it, but if you don't, you or your system administrator should read Appendix A. For now, we'll suppose you're sitting in front of a terminal with zsh already running.
Now to the shell. After you log in, you probably see some prompt (a series of symbols on the screen indicating that you can input a command), such as `$' or `%', possibly with some other text in front --- later, we'll see how you can change that text in interesting ways. That prompt comes from the shell. Type `print hello', then backspace over `hello' and type `goodbye'. Now hit the `Return' key (or `Enter' key, I'll just say from now on, likewise for the tab key, for the space key); unless you have a serious practical-joker problem on your system, you will see `goodbye', and the shell will come back with another prompt. All of the time up to when you hit , you were interacting with the shell and its editor, called `Z-Shell Line Editor' or `zle' for short; only then did the shell go away and tell the print command to print out a message. So you can see that the shell is important.

UNIX shell scripting with sh/ksh

© Dartmouth College
  • Learn what kinds of problems are suited to shell scripts
  • Review the most commonly used Unix commands that are useful in shell scripts.
  • Write simple shell scripts using the Bourne, Korn or Bash shells These notes are intended for use in a 2-part class, total duration 3 hours.

What is a Shell Script

  • A text file containing commands which could have been typed directly into the shell.
  • The shell itself has limited capabilities -- the power comes from using it as a "glue" language to combine the standard Unix utilities, and custom software, to produce a tool more useful than the component parts alone.
  • Any shell can be used for writing a shell script. To allow for this, the first line of every script is:#!/path/to/shell (e.g. #!/bin/ksh). #! The #! characters tell the system to locate the following pathname, start it up and feed it the rest of the file as input. Any program which can read commands from a file can be started up this way, as long as it recognizes the # comment convention. The program is started, and then the script file is given to it as an argument. Because of this, the script must be readable as well as executable. Examples are perl, awk, tcl and python.
  • Any file can be used as input to a shell by using the syntax:ksh myscript
  • If the file is made executable using chmod, it becomes a new command and available for use (subject to the usual $PATH search).chmod +x myscript A shell script can be as simple as a sequence of commands that you type regularly. By putting them into a script, you reduce them to a single command.

Click to Read More

ksh(1) - KornShell, a standard/restricted command and programming language

docs.sun.com
The /usr/xpg4/bin/sh utility is a standards compliant shell. This utility provides all the functionality of /usr/bin/ksh, except in cases discussed below where differences in behavior exist. See Arithmetic Expansions section for details.
/usr/bin/ksh is a command and programming language that executes commands read from a terminal or a file. rksh is a restricted version of the command interpreter ksh; it is used to set up login names and execution environments whose capabilities are more controlled than those of the standard shell. See Invocation below for the meaning of arguments to the shell.
Definitions
A metacharacter is one of the following characters:
; & ( ) < > NEWLINE SPACE TAB
A blank is a TAB or a SPACE. An identifier is a sequence of letters, digits, or underscores starting with a letter or underscore. Identifiers are used as names for functions and variables. A word is a sequence of characters separated by one or more non-quoted metacharacters.
A command is a sequence of characters in the syntax of the shell language. The shell reads each command and carries out the desired action either directly or by invoking separate utilities. A special-command is a command that is carried out by the shell without creating a separate process. Except for documented side effects, most special commands can be implemented as separate utilities.

The C Shell tutorial

University of Hawaii at Manoa
What is a shell?
A shell is a program which provides a user interface. With a shell, users can type in commands and run programs on a Unix system. Basically, the main function a shell performs is to read in from the terminal what one types, run the commands, and show the output of the commands.
What's so good about C Shell?
The C shell was written by Bill Joy at the University of California at Berkeley. His main intent for writing the C shell was to create a shell with C language-like syntax.
What can one do with C Shell?
The main use of the C shell is as an interactive shell, but one can write programs using the C shell. These programs are called shell scripts.

Followers