Shell script

A shell script is a script written for the shell, or command interpreter, of an operating system. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Usually, shell script refers to scripts written for a Unix shell, while DOS and Windows command-line scripts are called batch files, but here we discuss properties of both. Bourne shell-derived scripting languages are more commonly used than C shell-derived languages for writing shell scripts.

Many shell script interpreters double as command-line interfaces, 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. Examples of shell script languages include:

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:

 cp *.txt /
 cp *.mp3 /

Although each shell script is different, there are a number of common extensions to this primitive macro capability that script writers find useful. One is environment variables, the ability to predefine certain values which are then inserted in the script at specified locations. For example, this script copies all .txt and .mp3 files from the directory whose name is in the environment variable "fuzzy" to the root:

 cp $fuzzy/*.txt /
 cp $fuzzy/*.mp3 /

Now, by changing the value of the environment variable "fuzzy", the user can affect where the files are copied from. However, changing these every time they run the script is taxing, so most scripting languages predefine certain environment variables referring to arguments passed to the script on the command-line. For example, $1 through $9 refer to the first nine arguments given to the script in Bourne shell, as do %1 through %9 in DOS batch files.

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

Most shell scripts also import a selection of features from more sophisticated programming languages, such as control-flow constructs (if, while, goto), mutable variables, comments, subroutines, and so on. Nevertheless, they are rarely adequate for programming of a complex or large-scale system, because they have few structuring mechanisms, limited built-in commands, and are generally interpreted slowly.

For tasks deemed too large or complex to be comfortably handled with shell scripts, programs are often written in scripting languages, such as Perl. However, core system maintenance scripts, which might otherwise be written in such a language, continue to be written as shell scripts, as they cannot always rely on the presence of scripting language engines.

External links

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools