11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

shell 429have achieved their greatest proliferation <strong>and</strong> elaborationwith UNIX, the operating system developed by Ken Thompson<strong>and</strong> Dennis Ritchie starting in 1969 <strong>and</strong> widely used foracademic, scientific, engineering, <strong>and</strong> Web applications.UNIX shells serve the same basic purposes as the MS-DOS shell: interactive control <strong>of</strong> the operating system <strong>and</strong>the ability to run stored comm<strong>and</strong> scripts. However, theUNIX shells have considerably more complex syntax <strong>and</strong>capabilities.Part <strong>of</strong> the design philosophy <strong>of</strong> the UNIX system wasto place the core operating system functions in the kernel(see Kernel <strong>and</strong> UNIX). This modular design meant thatUNIX, unlike most other operating systems, did not have tocommit itself to a particular form <strong>of</strong> user interface or comm<strong>and</strong>processor. Accordingly, a number <strong>of</strong> such processors(shells) have been developed, reflecting the programmingstyle preferences <strong>of</strong> their originators.The first shell to be developed was the Bourne Shell,named for its creator, Steven R. Bourne, who developed itat Bell Labs, the original home <strong>of</strong> UNIX. The Bourne shellimplemented some basic ideas that are characteristic <strong>of</strong>UNIX: the ability to redirect input <strong>and</strong> output to <strong>and</strong> fromfiles, devices or other sources (using the < <strong>and</strong> > characters),<strong>and</strong> the ability to use “pipes” (the | character) to connectthe output <strong>of</strong> one comm<strong>and</strong> to the input <strong>of</strong> another.The next major development was the C shell (csh). TheBourne shell used a relatively simple <strong>and</strong> clean syntaxdevised by its creator. As the name suggests, the C shell(developed at the University <strong>of</strong> California, Berkeley) takesits syntax from the C programming language, which wasby far the most commonly used language on UNIX systems.One logical reason for this choice was that C programmerscould quickly learn to write scripts with the C shell. TheC shell also added support for job control (that is, movingprocesses between foreground <strong>and</strong> background operation)<strong>and</strong> in general was easier to use for interactively controllingprograms from the comm<strong>and</strong> line.UNIX users sometimes used both shells, since the simpler<strong>and</strong> more consistent syntax <strong>of</strong> the Bourne shell is generallythought to be better for writing scripts. (The twoshells also reflected the split in the UNIX world betweenthe version <strong>of</strong> the operating system provided by AT&T <strong>and</strong>the variant developed at UC Berkeley.)David Korn at AT&T then decided to combine the bestfeatures <strong>of</strong> both shells. His Korn shell (Ksh) kept the betterscripting language features from the Bourne shell but addedjob-control <strong>and</strong> other features from the C shell. He alsoadded the programming language concept <strong>of</strong> functions (seeprocedures <strong>and</strong> functions), allowing for cleaner organization<strong>of</strong> code.Another popular shell, BASH (Bourne Again Shell) wasdeveloped by the Free S<strong>of</strong>tware Foundation for GNU, anopen-source version <strong>of</strong> UNIX. BASH <strong>and</strong> Ksh share mostfeatures <strong>and</strong> both are compatible with POSIX, a st<strong>and</strong>ardspecification for connecting programs to the UNIX operatingsystem.The surge <strong>of</strong> interest in open-source UNIX in recentyears (see Linux) has brought a new generation <strong>of</strong> shellusers. Although modern Linux distributions provide a fullgraphical user interface (GUI)—indeed, a choice <strong>of</strong> them—such tasks as s<strong>of</strong>tware installation <strong>and</strong> configuration <strong>of</strong>teninvolve entering shell comm<strong>and</strong>s. Experienced users canalso find, copy, move, or otherwise manipulate batches <strong>of</strong>files more efficiently in the shell than in using windows <strong>and</strong>mouse movements.Shell ScriptsRegardless <strong>of</strong> the version <strong>of</strong> the shell used, shell scriptswork in the same basic way. A shell script is a text filecontaining comm<strong>and</strong>s to the shell. The comm<strong>and</strong>s can usecontrol statements (see branching statements <strong>and</strong> loop)<strong>and</strong> invoke both the shell’s internal features <strong>and</strong> the manyhundreds <strong>of</strong> utility programs that are available on UNIXsystems (see scripting languages).Once the script is written, there are two ways to executeit. One way is to type the name <strong>of</strong> the shell at the comm<strong>and</strong>prompt, followed by the name <strong>of</strong> the script file, as in:$ sh MyScriptAlternatively, the chmod (change mode) comm<strong>and</strong> can beused to mark the script’s file type as executable, <strong>and</strong> the firstline <strong>of</strong> the script then contains a statement that invokes theshell, which will parse the rest <strong>of</strong> the script. The script cannow be executed simply by typing its name at the comm<strong>and</strong>prompt (or it can be included as a comm<strong>and</strong> in anotherscript).Here is a simple example <strong>of</strong> a shell script that prints outvarious items <strong>of</strong> information about the user <strong>and</strong> the currentsession on a UNIX system:#! /sbin/shecho My username: `whoami`echo My current directory: `pwd`echoecho My disk usage:du -kechoecho System status:uptimeif test -f log.txt; thencat log.txtelse echo Log file not foundfiThe first line tells UNIX which shell to use to interpretthe script (in this case the Bourne shell, sh, will beexecuted). The echo comm<strong>and</strong> simply outputs the text thatfollows it to the screen. “whoami” is a UNIX comm<strong>and</strong> thatprints the user’s name. The script takes advantage <strong>of</strong> aninteresting UNIX feature: The whoami comm<strong>and</strong> is put in“backquotes” (` `). This inserts the output <strong>of</strong> the whoamicomm<strong>and</strong> (the user name) in place <strong>of</strong> that comm<strong>and</strong>, <strong>and</strong>the resulting text is output by the echo comm<strong>and</strong>.The du comm<strong>and</strong> gives the user’s disk usage, while theuptime comm<strong>and</strong> gives some statistics about how manyusers are on the system <strong>and</strong> how long the system has beenrunning. Finally, the if statement at the end <strong>of</strong> the script

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!