Basic Shell Programming

  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.

The most popular Linux shell is bash. This stands for Bourne Again Shell, and is named after one of the original UNIX shell designers. bash has a lighter relative, ash, which lacks some features such as command-line histories, but requires substantially less memory and is therefore found in some Linux distributions and on emergency recovery disks. Clones of the standard UNIX shells, sh (the Bourne shell), csh (the c shell), and ksh (the Korn shell) are also available.

Another point to remember is that a shell under Linux (as well as under other UNIX systems, and interestingly enough, under DOS and OS/2) is just another program. You can start another shell from the shell you're in, just like you can start the program ls to get a directory listing. This is significant because many shell scripts are not interpreted by the same type of shell in which you type the command; instead, a different shell is started to process the command. The different shells have different syntaxes. If a program is written using sh but you are running bash, it makes sense that sh would be started when your shell script is started.

What Shell Scripts Are Used For

Shell scripts are actually one of the most common types of programs on all UNIX systems. They are relatively easy to write and maintain, and they can tie together other programs (for example, other shell scripts) to get a lot of work done with one simple command.

Every time you log in to your Linux system, the system executes a shell script before you even see the first $ prompt. Almost all aspects of system startup, including network initialization, are controlled by shell scripts. For a regular Linux system user, shell scripts can make work easier and more productive. For the Linux system administrator, a basic understanding of shell scripts makes all the difference between a well-run and trouble-free Linux system and one that can get really ugly, really fast.

Writing Shell Programs
Shell programs are similar to batch files in the DOS world. Unlike DOS batch files, however, shell programs have much more advanced functionality; they are akin to conventional programming languages. Entire books have been written about most available shells; this chapter covers only bash because it is the most popular, and because it is used by default on Red Hat, Caldera, and most popular Linux distributions.

A Sample Program
The last thing the world needs is another "Hello, World!" program. Instead, I want to show you something useful. For example, suppose you want a nicely formatted printout of what is in the current directory. The code shown below provides a means to do just that (the numbers at the beginning of each line are for your use; they do not actually appear in the code).

01 #!/bin/bash
02 #Sample Program for Practical Linux
03 # www.linuxindya.com
04 # Blog Basic Shell
05 # written by v dev
06 # and checked by ravi
07 # This code comes without warranty of any kind. If it breaks u get
08 # to keep both pieces.
09 ls -l > /tmp/lstemp
10 pr /tmp/lstemp | lpr
11 rm /tmp/lstmp
12 #end of script

This script works best when you're logged in as a regular user rather than system administrator; it assumes that you have a printer installed and correctly configured. Simply put, it executes the commands in the file in sequence. The more interesting aspects of this listing are as follows:

* Line 1 tells the system which shell the system should start to execute the script. This is vital because different shell programs sport different built-in commands.
* Line 9 executes the program ls -l and redirects output to a file in the system temporary directory.
* Line 10 feeds the contents to pr (which breaks the file into pages and, depending on the arguments, numbers the pages, adds a nice header, and so on) and feeds the output of pr to lpr, the system print spooler. The important point here is the redirection of the output of commands, not the commands themselves.
* Line 11 removes the temporary file created in line 3.