Custom Search

Introduction

A shell is a command line interpretor. It takes commands and executes them. As such, it implements a programming language. The Bourne shell is used to create shell scripts -- ie. programs that are interpreted/executed by the shell. You can write shell scripts with the C-shell; however, this is not covered here.

Necessity of Structured Shell Programming

Shell scripts remain a staple of the business world.With high development costs, it is not practical to develop everything in a high-level language. Many business processes are simply a matter of following a series of steps, after which the results are labeled and archived.This is the very type of activity scripts are made to handle. Over time, shells have collected new features, often by copying the capabilities of other shells. In his book, The Humane Interface, Apple Macintosh interface designer Jef Raskin once spoke with a pilot regarding the design of the plane’s autopilot.The device was designed with five ways to enter coordinates.The reason for this design was to reduce training costs by emulating other autopilots. However, a pilot pointed out that the operator of the aircraft is responsible for knowing how to operate every piece of equipment, and that meant he had to know how to adjust the autopilot in each of the five ways.
Many shells, including Bash, have developed a mixture of features with specialized or arcane applications. Like the autopilot, it is not always clear how these features should be
used nor which features are provided primarily for compatibility.Too often, poorly designed scripts are all-but illegible to another programmer, or even to the same programmer a few months after a script was hastily assembled.
This presents a problem for serious business scripts.The cost of developing a highlevel language solution is high, but a disorganized shell script can be expensive to maintain
over time. As the needs of the business change, and the shell script is modified by successive generations of programmers, the problem escalates.
As a rule of thumb, business programs never go away.At one place I recently worked,what started off as a simple shell script used to print reports gradually evolved into a Web-based reporting system complete with personal customizations and secure access.
The original programmer had no idea what his script would become. Because of the essential openness and complex syntax of Bash, it’s to the benefit of any serious project to adapt some kind of standard. Bash is very forgiving, but the costs are not. For example, there are many ways to add 2 + 2 in Bash. Like the autopilot anecdote, it’s not practical to expect the programmers who maintain the scripts to have to deal with this kind of redundancy. Likewise, shell word quoting is sometimes optional, sometimes required, depending on the context. A command that works with one form of quotation can suddenly stop working when different data appears in the quotes. Issues like shell word quoting, specialized capabilities, or portability features, when abused, can lead to increased maintenance and long-term development costs.

The Origin of Linux

The Linux operating system was created as a hobby by a young student, Linus Torvalds, at the University of Helsinki in Finland. Linus, interested in the Unix clone operating system Minix, wanted to create an expanded version of Minix with more capabilities. He began his work in 1991 when he released version 0.02 and invited programmers to participate in his project.Version 1.0 was released in 1994.
Linux uses GNU General Public License (GPL) and its source code is freely available to everyone. Linux distributions, CD-ROMs with the Linux kernel and other software ready for installation, do not have to be free, but the Linux source code must remain available. Making source code available is known as distributing open source. The word “Linux” is properly pronounced using a Swedish accent, making it difficult to say in North America. It is most often pronounced with a short “i” and with the firstsyllable stressed, as in LIH-nicks, but it is sometimes pronounced LYE-nicks (the anglicized “Linus’ Unix”) or LEE-nucks. Strictly speaking, Linux refers to the operating system kernel that starts and manages other programs and provides access to system resources.The various open source shells, compilers, standard libraries, and commands are a part of another project called GNU. The GNU project was started by the Free Software Foundation (FSF) as an attempt to create a free version of Unix.The main Linux C compiler, gcc, is a part of the GNU project. There is also a GNU kernel project, but the Linux kernel has largely superseded thiseffort.
X Windows is also not strictly a part of Linux. Xfree86, the open source version of X Windows, was adapted to the Linux operating system and was released under a different license.

find, cp, mv, rm, and ln

As you create more and more files, you may want to back them up, change their names,erase some of them, or even give them added names. Linux provides you with several filecommands that enable you to search for files, copy files, rename files, or remove files. If you have a large number of files, you can also search themto locate a specific one. The command names shortened forms of full words, consisting ofonly two characters. The cp command stands for “copy” and copies a file, mv stands for“move” and renames or moves a file, rm stands for “remove” and erases a file, and lnstands for “link” and adds another name for a file, often used as a shortcut to the original.One exception to the two-character rule is the find command, which performs searches ofyour filenames to find a file. All these operations can be handled by the GUI desktops suchas GNOME and KDE.
Searching Directories: findOnce you have a large number of files in many different directories, you may need to searchthem to locate a specific file, or files, of a certain type. The find command enables you toperform such a search from the command line. The find command takes as its argumentsdirectory names followed by several possible options that specify the type of search and thecriteria for the search; it then searches within the directories listed and their subdirectoriesfor files that meet these criteria. The find command can search for a file by name, type,owner, and even the time of the last update.$ find directory-list -option criteria
The find command also has options that merely perform actions, such as outputtingthe results of a search. If you want find to display the filenames it has found, you simplyinclude the -print option on the command line along with any other options. The -printoption is an action that instructs find to write to the standard output the names of all thefiles it locates (you can also use the -ls option instead to list files in the long format). In thenext example, the user searches for all the files in the reports directory with the namemonday. Once located, the file, with its relative pathname, is printed.$ find reports -name monday -printreports/mondayThe find command prints out the filenames using the directory name specified in thedirectory list. If you specify an absolute pathname, the absolute path of the founddirectories will be output. If you specify a relative pathname, only the relative pathnamewill be output. In the preceding example, the user specified a relative pathname, reports, inthe directory list. Located filenames were output beginning with this relative pathname. Inthe next example, the user specifies an absolute pathname in the directory list. Locatedfilenames are then output using this absolute pathname.$ find /home/chris -name monday -print/home/chris/reports/monday

Locating DirectoriesYou can also use the find command to locate other directories. In Linux, a directory isofficially classified as a special type of file. Although all files have a byte-stream format,some files, such as directories, are used in special ways. In this sense, a file can be said tohave a file type. The find command has an option called -type that searches for a file of agiven type. The -type option takes a one-character modifier that represents the file type.The modifier that represents a directory is a d. In the next example, both the directory nameand the directory file type are used to search for the directory called thankyou:$ find /home/chris -name thankyou -type d -print/home/chris/letters/thankyou$File types are not so much different types of files as they are the file format applied toother components of the operating system, such as devices. In this sense, a device is treatedas a type of file, and you can use find to search for devices and directories, as well asordinary files. Table 6-4 lists the different types available for the find command’s -typeoption.You can also use the find operation to search for files by ownership or security criteria,like those belonging to a specific user or those with a certain security context. The useroption lets you locate all files belonging to a certain user. The following example lists allfiles that the user chris has created or owns on the entire system. To list those just in theusers’ home directories, you use /home for the starting search directory. This finds all filesin a user's home directory as well as any owned by that user in other user directories.$ find / -user chris -print