Learning the Korn Shell (Nutshell Handbooks) and over 2 million other books are available for Amazon Kindle . Learn more

Buy New

Sign in to turn on 1-Click ordering.
Buy Used
Used - Very Good See details
Price: £11.91

Trade in Yours
For a £0.88 Gift Card
Trade in
More Buying Choices
Have one to sell? Sell yours here
Start reading Learning the Korn Shell (Nutshell Handbooks) on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Learning the Korn Shell [Paperback]

Arnold Robbins , Bill Rosenblatt
3.6 out of 5 stars  See all reviews (5 customer reviews)
Price: £25.99 & FREE Delivery in the UK. Details
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
Only 1 left in stock (more on the way).
Dispatched from and sold by Amazon. Gift-wrap available.
Want it Tuesday, 23 Dec.? Choose Express delivery at checkout. Details
‹  Return to Product Overview

Product Description


This is an excellent book for anyone wanting to learn the Korn shell. -- Lee R. Chikasue, Williamsburg Macromedia User Group, Feb 2003

From the Publisher

Learning the Korn Shell is the key to gaining control of the Korn shell and becoming adept at using it as an interactive command and scripting language. Readers will learn how to write many applications more easily and quickly than with other high-level languages. A solid offering for many years, this newly revised title inherits a long tradition of trust among computer professionals who want to learn or refine an essential skill.

About the Author

Arnold Robbins, an Atlanta native, is a professional programmer and technical author. He has worked with Unix systems since 1980, when he was introduced to a PDP-11 running a version of Sixth Edition Unix. He has been a heavy AWK user since 1987, when he became involved with gawk, the GNU project's version of AWK. As a member of the POSIX 1003.2 balloting group, he helped shape the POSIX standard for AWK. He is currently the maintainer of gawk and its documentation. He is also coauthor of the sixth edition of O'Reilly's Learning the vi Editor. Since late 1997, he and his family have been living happily in Israel.

Excerpt. © Reprinted by permission. All rights reserved.

Chapter 4 - Basic Shell Programming

Shell Scripts and Functions
Shell Variables
Compound Variables
Indirect Variable References (namerefs)
String Operators
Command Substitution
Advanced Examples: pushd and popd

If you have become familiar with the customization techniques we presented in the previous chapter, you have probably run into various modifications to your environment that you want to make but can't -- yet. Shell programming makes these possible.

The Korn shell has some of the most advanced programming capabilities of any command interpreter of its type. Although its syntax is nowhere near as elegant or consistent as that of most conventional programming languages, its power and flexibility are comparable. In fact, the Korn shell can be used as a complete environment for writing software prototypes.

Some aspects of Korn shell programming are really extensions of the customization techniques we have already seen, while others resemble traditional programming language features. We have structured this chapter so that if you aren't a programmer, you can read this chapter and do quite a bit more than you could with the information in the previous chapter. Experience with a conventional programming language like Pascal or C is helpful (though not strictly necessary) for subsequent chapters. Throughout the rest of the book, we will encounter occasional programming problems, called tasks, whose solutions make use of the concepts we cover.

Shell Scripts and Functions
A script, or file that contains shell commands, is a shell program. Your .profile and environment files, discussed in Chapter 3, are shell scripts.

You can create a script using the text editor of your choice. Once you have created one, there are a number of ways to run it. One, which we have already covered, is to type . scriptname(i.e., the command is a dot). This causes the commands in the script to be read and run as if you typed them in.

Two more ways are to type ksh scriptor ksh < script. These explicitly invoke the Korn shell on the script, requiring that you (and your users) be aware that they are scripts.
The final way to run a script is simply to type its name and hit ENTER, just as if you were invoking a built-in command. This, of course, is the most convenient way. This method makes the script look just like any other Unix command, and in fact several "regular" commands are implemented as shell scripts (i.e., not as programs originally written in C or some other language), including spell, man on some systems, and various commands for system administrators. The resulting lack of distinction between "user command files" and "built-in commands" is one factor in Unix's extensibility and, hence, its favored status among programmers.

You can run a script by typing its name only if . (the current directory) is part of your command search path, i.e., is included in your PATH variable (as discussed in Chapter 3). If . isn't on your path, you must type ./scriptname, which is really the same thing as typing the script's relative pathname (see Chapter 1).
Before you can invoke the shell script by name, you must also give it "execute" permission. If you are familiar with the Unix filesystem, you know that files have three types of permissions (read, write, and execute) and that those permissions apply to three categories of user (the file's owner, a group of users, and everyone else). Normally, when you create a file with a text editor, the file is set up with read and write permission for you and read-only permission for everyone else.[1]

[1] This actually depends on the setting of your umask, an advanced feature described in Chapter 10.

Therefore you must give your script execute permission explicitly, by using the chmod(1) command. The simplest way to do this is like so:

chmod +x scriptname

Your text editor preserves this permission if you make subsequent changes to your script. If you don't add execute permission to the script, and you try to invoke it, the shell prints the message:

ksh: scriptname: cannot execute [Permission denied]
But there is a more important difference between the two ways of running shell scripts. While the "dot" method causes the commands in the script to be run as if they were part of your login session, the "just the name" method causes the shell to do a series of things. First, it runs another copy of the shell as a subprocess. The shell subprocess then takes commands from the script, runs them, and terminates, handing control back to the parent shell.
Figure 1 shows how the shell executes scripts. Assume you have a simple shell script called fredthat contains the commands bob and dave. In Figure 1.a, typing . fred causes the two commands to run in the same shell, just as if you had typed them in by hand. Figure 1.b shows what happens when you type just fred: the commands run in the shell subprocess while the parent shell waits for the subprocess to finish.

You may find it interesting to compare this with the situation in Figure 1.c, which shows what happens when you type fred &. As you will recall from Chapter 1, the & makes the command run in the background, which is really just another term for "subprocess." It turns out that the only significant difference between Figure 1.c and Figure 1.b is that you have control of your terminal or workstation while the command runs -- you need not wait until it finishes before you can enter further commands.

‹  Return to Product Overview