411 REFERENCE · Wikibooks
Shell Programming/Introduction
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Shell Programming Shell Programming What is shell
A shell script is a text file containing Unix commands, so the entire stored group of command lines may be treated as a single program that performs many steps. The script can be read and executed by a standard Unix shell, which normally works as the command line interpreter, allowing users to type commands from the keyboard. When a user types in the name shell script on the command line, the shell will read from the script file, and try to parse+interpret+run each command in the file, as if they were quickly typed at the command line. This avoids retyping, simplifies work for the user, and allows the script to be used as any other program on the computer. Shell scripts have programming features that are seldom used directly on the command line, such as loops, variables, and conditional logic statements. These code features allow parts of the script to be repeated multiple times, or script logic may decide to skip other command lines that may not be needed at that time. By using shell scripts, the command line shell supports a scripting language that can be used to tell the host operating system how to perform more complex tasks than users normally type with a single command.
Basic shell scripts are very easy to write using a text editor, and they do not need to be compiled into a binary format: This often makes shell scripts much faster to write than compiled programs in C, Java, or Fortran. People may write simple shell scripts in fifteen minutes, while C programs with similar abilities may take hours or weeks to write. However, shell scripts run slowly compared to compiled programs (such as C), because the shell interpreter must read+parse text for each command line in the script file, and decide what actions to perform. (code/command interpretation is performed in advance for compiled programs, and translated to binary executable programs that run more efficiently) Using shell scripts can still be thousands of times faster than typing individual statements by hand, since a shell script containing hundreds of lines may be run by the user by typing a single statement on the command line. One other aspect that makes shell scripts slower than compiled programs is that C programs often work as a single executable process, while shell scripts gain much of their capabilities by calling separate external programs, which may run three processes or thousands of other programs.
Since shell scripts contain text that is readable by people, script programs are more easily modified to meet new requirements, and also easily contain documentation and comments to explain how the script is to be used, and a written log of any code changes made.
Shell scripts are primarily suited for simple tasks that call other programs on the operating system. Examples may include – file manipulation – system administration – process control – text manipulation and formatting – repetitive tasks – job scheduling. More complex or performance-sensitive tasks are more commonly written in general-purpose languages, traditionally C, more recently Perl or Python. Due to ease of writing, shell scripts are also well-suited for one-off code or for rapid prototyping, as with other scripting languages, and provide a very good introduction both to programming generally and operating systems specifically, particularly for users familiar with the command line.
Languages
This book treats sh-compatible shells.
There are various shells, with slightly different syntax and behaviors, and variations for programming languages features. Most shells are variants of the original Bourne shell developed in the 1970;s (sh; see Bourne Shell Scripting), sharing the basic syntax but adding features, and the associated programming languages are referred to as “dialects” of sh. Today most common, particularly on Linux, is bash, the “Bourne-again shell” (see Bash Shell Scripting), which is a complex shell providing many features. The Korn shell (ksh) also sees use, primarily on proprietary Unix systems (per POSIX standard) and BSD OSes. However, other shells are used, such as the Almquist shell (ash), particularly in resource-constrained environments, for security, or for licensing – such shells typically omit interactive features, which make them smaller and more secure – and writing a script which is compatible across dialects of sh is called “portable shell scripting”. Portable shell scripting requires restricting oneself to a subset of features compatible across the target languages – this is largely equivalent to restricting oneself to the original Bourne shell language, but there are subtle edge cases to beware of.
The C shell (csh), and variants, is the primary non-sh-compatible shell. It is also scriptable (see C Shell Scripting), but this is uncommon, due to severe problems with the language."Csh Programming Considered Harmful" Accordingly, csh was primarily used as an interactive shell in the 1980s and 1990s, alongside some variant of sh for scripting. This has largely disappeared, due to sh-compatible shells adding the interactive features of csh. Thus for the purpose of this book, a shell means an sh-Bourne-compatible shell.
Applications
Shell scripts are fundamentally scripting languages for the operating system, and thus are suited to tasks that involve invoking operating system facilities, such as file operations, or multiple other commands, with relatively little logic or advanced language facilities. By contrast, tasks that involve complicated logic or invoke few other commands – or can call a library instead of a separate command – are better written in a general-purpose language, today typically a high-level language like Python or Ruby. Earlier Perl was written for precisely this purpose – more advanced shell scripting – and retains much of the flavor and syntax of shell scripting; it was very popular from the late 1980s to early 2000s, but has since declined in popularity.
The main applications of shell scripting are either automating a repetitive task or writing a one-off script for a complicated one-time task. Shell scripting is a key skill in system administration, and is also used to script a program or combination of programs when there is a good command-line interface but no built-in scripting language or libraries.
The key advantage of shell scripts over other languages is the ease of invoking other commands, without verbose syntax (explicit function call and quoting), while the key disadvantages are limited facilities, awkward and delicate syntax (particularly due to compatibility with interactive use and due to quoting), and slow execution (due to overhead on running separate processes, particularly context switches).
Alternatives
Instead of a shell script that executes separate commands, if libraries are available one can instead write a program that calls the libraries, which avoids the separate process time overhead (and space overhead, if dynamically linked), at the cost of linking in the libraries. This incurs other costs: if statically linked this requires a time cost at compile time, space cost on disk, and space cost at run time (due to including the libraries); while if dynamically linked this incurs a time cost at run time, though dynamic linking is only required once per library.
Conversely, for very simple commands, particularly one-liners, a shell alias avoids the overhead of executing a separate script. For example, for a directory listing shortcut, the alias:
alias lsal='clear;ls -al'
is faster and simpler than the script:
#!/bin/sh
clear
ls -al
References