Wednesday, January 9, 2013

Shell Programming:GETTING STARTED

GETTING STARTED:

* The first thing to do in understanding shell programs is to understand the elementary system commands that can be used in them. A list of fundamental UNIX system commands follows:
  ls         # Give a simple listing of files.
  cp         # Copy files.
  mv         # Move or rename files.
  rm         # Remove files.  
  rm -r      # Remove entire directory subtree.
  cd         # Change directories.
  pwd        # Print working directory.
  cat        # Lists a file or files sequentially.
  more       # Displays a file a screenfull at a time.
  pg         # Variant on "more".
  mkdir      # Make a directory.
  rmdir      # Remove a directory.
The shell executes such commands when they are typed in from the command prompt with their appropriate parameters, which are normally options and file names.
* The shell also allows files to be defined in terms of "wildcard characters" that define a range of files. The "*" wildcard character substitutes for any string of characters, so:
   rm *.txt
-- deletes all files that end with ".txt". The "?" wildcard character substitutes for any single character, so:
   rm book?.txt
-- deletes "book1.txt", "book2.txt", and so on. More than one wildcard character can be used at a time, for example:
   rm *book?.txt
-- deletes "book1.txt", "mybook1.txt", "bigbook2.txt", and so on.
* Another shell capability is "input and output redirection". The shell, like other UNIX utilities, accepts input by default from what is called "standard input", and generates output by default to what is called "standard output". These are normally defined as the keyboard and display, respectively, or what is referred to as the "console" in UNIX terms. However, standard input or output can be "redirected" to a file or another program if needed. Consider the "sort" command. This command sorts a list of words into alphabetic order; typing in:
   sort
   PORKY
   ELMER
   FOGHORN
   DAFFY
   WILE
   BUGS
   <CTL-D>
-- spits back:
   BUGS
   DAFFY
   ELMER
   FOGHORN
   PORKY
   WILE
Note that the CTL-D key input terminates direct keyboard input. It is also possible to store the same words in a file and then "redirect" the contents of that file to standard input with the "<" operator:
   sort < names.txt
This would list the sorted names to the display as before. They can be redirected to a file with the ">" operator:
   sort < names.txt > output.txt
They can also be appended to an existing file using the ">>" operator:
   sort < names.txt >> output.txt
In these cases, there's no visible output, since the command just executes and ends. However, if that's a problem, it can be fixed by connecting the "tee" command to the output through a "pipe", designated by "|". This allows the standard output of one command to be chained into the standard input of another command. In the case of "tee", it accepts text into its standard input and then dumps it both to a file and to standard output:
   sort < names.txt | tee output.txt
So this both displays the names and puts them in the output file. Many commands can be chained together to "filter" information through several processing steps. This ability to combine the effects of commands is one of the beauties of shell programming. By the way, "sort" has some handy additional options:
   sort -u    # Eliminate redundant lines in output.
   sort -r    # Sort in reverse order.
   sort -n    # Sort numbers. 
   sort -k 2  # Skip first field in sorting.
* If a command generates an error, it is displayed to what is called "standard error", instead of standard output, which defaults to the console. It will not be redirected by ">". However, the operator "2>" can be used to redirect the error message. For example:
   ls xyzzy 2> /dev/null
-- will give an error message if the file "xyzzy" doesn't exist, but the error will be redirected to the file "/dev/null". This is actually a "special file" that exists under UNIX where everything sent to it is simply discarded.
* The shell permits the execution of multiple commands sequentially on one line by chaining them with a ";":
   rm *.txt ; ls
A time-consuming program can also be run in a "parallel" fashion by following it with a "&":
   sort < bigfile.txt > output.txt &
* These commands and operations are essential elements for creating shell programs. They can be stored in a file and then executed by the shell. To tell the shell that the file contains commands, just mark it as "executable" with the "chmod" command. Each file under UNIX has a set of "permission" bits, listed by an "ls -l" -- the option providing file details -- as:
   rwxrwxrwx
The "r" gives "read" permission, the "w" gives "write" permission, and the "x" gives "execute" permission. There are three sets of these permission bits, one for the user, one for other members of a local group of users on a system, and one for everyone who can access the system -- remember that UNIX was designed as a multiuser environment.
The "chmod" command can be used to set these permissions, with the permissions specified as an octal code. For example:
   chmod 644 myfile.txt
This sets both read and write permission on the file for the user, but everybody else on the system only gets read permission. The same octal scheme can be used to set execute permission, though it's simpler just to use chmod "+x" option:
   chmod +x mypgm
This done, if the name "mypgm" is entered at the prompt, the shell reads the commands out of "mypgm" and executes them. The execute permission can be removed with the "-x" option.
For example, suppose we want to be able to inspect the contents of a set of archive files stored in the directory "/users/group/archives". We could create a file named "ckarc" and store the following command string in it:
  ls /users/group/archives | pg
This is a very simple shell program. As noted, the shell has control constructs, supports storage variables, and has several options that can be set to allow much more sophisticated programs. The following sections describe these features in a quick outline fashion.

0 comments:

Post a Comment

Powered by Blogger.