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.

Followers