Shell Script Article Index for
Shell
Website Links For
Shell
 

Information About

Shell Script




Many shell script interpreters double as Command-line Interface , such as the Unix shell or the MS-DOS COMMAND.COM . Others, such as AppleScript , add scripting capability to computing environments lacking a command-line interface. Other examples of languages primarily intended for shell scripting include DCL and JCL .


CAPABILITIES

In their most basic form, shell scripts allow several commands that would be entered "by hand" at a command-line interface to be executed automatically and rapidly. For example, the following Bourne Shell script copies all .txt and .mp3 files in the current directory to the root directory:

  • .txt /

  • .mp3 /


  • " denotes any sequence of characters). Most shells implement basic Pattern Matching capabilities like this, which allow them to perform commands on groups of items with similar names and sometimes parse simple strings of text.


Although each shell scripting language is different, there are a number of additional features which are often provided. One is a mechanism for manipulating some form of Variable s, in other words, named values which can be inserted elsewhere in the script at specified locations. For example, this script copies all .txt and .mp3 files from the current directory to the directory named by the variable "fuzzy":

  • .txt

  • .mp3


Now, by changing the value of the variable "fuzzy", the user can specify where the files are copied to. However, rewriting a script to change a variable's definition each time the script is run would be a nuisance, so scripting languages typically also support special variables which provide access to any arguments passed to the script on the command line. For example, $1 through $9 refer to the first nine arguments given to a Bourne shell script, as do %1 through %9 in DOS batch files. Also, a shell's internal variables are often integrated with the operating system's notion of per-process or per-session Environment Variable s.

Another common feature of shell scripting languages is some way of dealing with ''return codes'', which are numbers returned from executed programs to indicate whether they succeeded or failed. In the Bourne shell, for example, the notation a && b means to first execute a, then execute b only if a succeeded. Batch files use if errorlevel for this purpose.

Many modern shells also supply various features usually found only in more sophisticated General-purpose Programming Language s, such as control-flow constructs (''if'', ''while'', ''goto''), mutable variables, comments, subroutines, and so on. With these sorts of features available, it is sometimes possible to write reasonably sophisticated applications as shell scripts, although of course the more demanding, complex or large-scale systems will usually require more powerful programming languages. Though the shells are powerful in their way, they have few structuring mechanisms, limited built-in commands, and are generally interpreted relatively slowly.


Other scripting languages

See Also: scripting language



For tasks deemed too large or complex to be comfortably handled with ordinary shell scripts, but for which the advantages of a script are desirable and the development overhead of a full-blown, compiled programming language would be disadvantageous, many powerful scripting languages have been introduced. The specifics of what separates scripting languages from High-level Programming Language s is a frequent source of debate.



Disadvantages

One significant disadvantage of using shell scripts is that they can run slowly due to the need to create potentially many new sub-processes for each of the many commands executed. When a script's job can be accomplished by setting up a Pipeline in which efficient Filter commands perform most of the work, the slowdown is minimal. But if a shell script has to perform multiple individual actions on many individual data items, resulting in multiple Forks for each command invoked to perform each action, the script might be orders of magnitude slower than a conventional compiled program (in which those same actions might require but single processor instructions).


SEE ALSO



EXTERNAL LINKS