CHAPTER 2: UNIX SHELLS
back to the Computing Guide Contents


Although the C-Shell (csh) serves as the default Unix shell in the MMM computing environment, the Bourne shell (sh) and the T C-Shell (tcsh) are also available. This chapter will cover the following basic information on all three shells.


2.1 The C-Shell


2.1.1 General Information

In a Unix environment, the commands you enter are executed by a program called the "shell". While there are a variety of shells, the default shell on MMM's computers is the C-shell (csh). This shell was designed to add interactive features to the Bourne shell and, while not as robust as the Bourne shell, is suitable for most tasks.

This section provides a quick overview of the C-Shell. More information can be found by using man csh. We also highly recommend that you obtain a good csh book; many are available. We have some copies available for checkout from the Systems library.

When a C-shell (csh) is started, it looks in two different files for basic information about the environment. When you first log onto a system, the csh reads a file in your home directory called .login. From then on, whenever you start a new csh, it looks for a file in your home directory named .cshrc. See Chapter 1 for more information on these files.

A new shell is started every time you run a command or a script. Therefore, put information into your .cshrc that should be available at all times. Generally, your .login and .cshrc files should contain environmental variables and aliases that are the same, since, in certain cases, the .login file will not be executed.

2.1.2 Environment Variables

The csh relies on a set of variables that should be defined in both your .login and .cshrc files to tell it about your environment. Whenever you start a new shell, these variables are passed to the shell from the parent. The most important variables are:
PATH
This is a list of directories in which the shell looks for commands and programs. If a program is in a directory that is not in the path, the shell will not be able to find it. Generally, you set this in your .cshrc. If you change it, use the rehash command to update the directory list.

HOME
You will not need to set this variable. It points to your home directory.

MANPATH
Similar to PATH, except it points to directories in which to look for man pages. (This is used on some systems, such as Sun.)

TERM
The type of terminal you are using. The csh uses the terminal definitions given in the termcap file. If your terminal type is not defined, problems occur. Typical terminal types are "vt100" or "xterm".

TERMCAP
You do not not need to set this. It specifies the file containing terminal definitions.

GRAPHCAP
This variable defines the output device for NCAR Graphics.

LD_LIBRARY_PATH
This variable defines a set of directories in which to look for "shareable" libraries on Sun systems. Without a complete set of libraries, some programs will not run.

PRINTER
These commands set the default printer on systems (HP Alpha and Linux systems.

LPDEST
This command sets the default printer on System V systems (SGI and Sun).

LASER
Local variable for use with scripts such as dup80, dup132, dupf, and dotex.

DISPLAY
If you are using an X device, you must set this. It is used to keep track of the display the graphics should be created on. If you are on a console, you can enter the name of the machine, for example, "mimosa:0.0" for the Sun workstation, walnut.
Use the command, setenv to set the values of these variables:
         setenv LASER plum
The command env will give you a list of the current variables. Individual variables can also be seen with the echo command, as in the following example.
        echo $DISPLAY

2.1.3 Aliases

Aliases are a powerful way to make personal mnemonics for complicated or often-used commands. Define an alias using the alias command, as in the example below. To clear the alias, use the unalias command.
        alias lf  'ls -alF |more'
In general, you should not define aliases that are the same as actual Unix commands, especially when the alias may require an interactive response, since problems may result when they are used in certain applications. For example,
        alias rm 'rm -i'
redefines the Unix rm command to prompt the user on whether to delete a specific file. Now when you use rm in a shell script, the script will hang, waiting for a response. To avoid this problem, an alternate alias might be
        alias del 'rm -i'

2.1.4 Executing Programs and Scripts

You can execute compiled programs and shell scripts by entering the name of the program or script at the prompt. Successful execution depends on the following factors.
  1. The program or script must be located in a directory that is specified in your PATH variable, or you must include the path of the program.
  2. You must have execute access to the file and all the directories above it (see Chapter 3 for information on the ls and chmod commands).
You can execute C-shell scripts with the C-shell source command. This command causes the C-shell to read commands from a file.
        Syntax: source scriptfilename
When debugging C-shell scripts, it is best to use the csh command.
        Syntax: csh -x scriptfilename
The -x option sets the echo variable so that commands are echoed to standard error (by default, the screen) after variable substitutions have been made and before the commands are executed.

The C-shell has a facility that controls the execution of programs and scripts. The C-shell command, jobs lists the job number, the status of the job, and the command that generated them. You can execute a program in background mode in one of two ways.

1. Enter the program name followed by an ampersand (&).
     
     program &

   Then enter jobs. 
   The following message is displayed.

     [1]     + Running program

2. To suspend a running program or script, enter CTRL-Z. 
   The following message is displayed.

     Stopped

   To verify that the job is suspended, enter jobs, which 
   displays the following message.

     [1]     + Stopped program

   Enter the C-shell command, bg to place the suspended job 
   in background mode.
   To resume the job, bring it back into the foreground with the
   C-shell command, fg. Again, you can verify the results with 
   the jobs command, which displays

     [1]    + Running program
You can also use C-shell job control to terminate jobs using the kill command with the job number displayed by the jobs command.
        Syntax: kill -9 %jobnumber

2.1.5 Writing Shell Scripts

A shell script is a file that contains a list of shell commands. The following sections include two example shell scripts. The first illustrates the use of the foreach loop construct, and the second shows how to use the if-then-else construct. For more information on writing shell scripts, see the references at the end of this section.

Sample Shell Script with Loop Construct

The following shell script copies all files with the .f extension, and gives the new files the same names as the originals, but with .sav appended at the end. This example uses the foreach loop construct. Note that the C-shell also has a while loop construct.

#!/bin/csh
# Make a copy of each file with the .f extension. New files have
# the same names as the orginals with .sav appended onto the name.
set files = (*.f)
foreach file ($files)

end
exit 0
#

Explanation of Command Lines

  1. #!/bin/csh

    Specifies the shell to be executed (in this example,the C-shell,csh).
    This should be the first line in the shell script.

  2. # Make a copy of each file with the .f extension. New files have
    # the same names as the orginals with .sav appended onto the name.

    Comments lines are preceded by a # sign, and are not executed.

  3. set files = (*.f)

    Sets the shell variable, files to the list of files ending with
    .f.

  4. foreach file ($files)
      echo "Copying" $file "to" $file.sav
      cp $file $file.sav
    end

    These lines comprise a C-shell foreach loop construct, and are
    interpreted as follows: Set the shell variable file to the first
    entry in the list stored in the shell variable files, execute
    the commands between the foreach and end statements, set the
    variable file to the next entry in files, and continue until file
    has been set to every entry in the list stored in files.

    The shell commands within the loop are echo and cp. The echo
    command writes text to standard output (by default, the screen).
    In this example, the text is a combination of quoted strings and
    the contents of shell variables. The cp command copies files. In
    this example, the first argument is the value of the shell variable
    file. The second argument, $file, is the value of the shell
    variable file with the characters, ".sav" appended to it,
    "$file.sav.

  5. exit 0

    Sets the shell variable status to 0. This return status can be
    used to determine the status of an executed shell script.

Sample Shell Script with If-Then-Else Construct

The following shell script executes the date command if "yes" is entered in response to the question posed by the script.

#!/bin/csh
echo -n ".Do you want to know today's date? Enter yes or no:"
set answer = $<
if ($answer == yes) then

else endif

Explanation of Command Lines

  1. #!/bin/csh

    Specifies the shell to be executed (in this example, the C-shell, csh).
    This should be the first line in the shell script.

  2. echo -n "Do you want to know today's date? Enter yes or no: "

    Outputs the text within the quotations to standard output (by
    default, the screen).

  3. set answer = $<

    Waits and reads characters from standard input (by default, the
    terminal), and sets the shell variable answer to the input string.

  4. if ($answer == yes) then
      date
      exit 1
    else
      exit 2
    endif

    These lines constitute the C-shell if-then-else construct. Commands
    in between the then and else statements (in this example, the date
    and exit 1 commands) are executed if the expression in the parentheses
    is true. The commands in between the else and endif statements (in
    this example, the exit 2 command) are executed if the expression is false.

    In this example, if the shell variable answer is "yes", the date command
    is executed, the date is output to standard output, and the shell
    variable status is set to "1" by the exit command. If the shell variable
    answer is anything else, the exit 2 command is executed, setting the shell
    variable status to the value of 2.

2.1.6 Executing Shell Scripts

To execute a shell script that has been stored in a file, first type chmod 755 filename or chmod +x filename to make the file executable, then press RETURN.  Next, type filename and press RETURN.


On-line Information

Hardcopy References



  Return to top of page

2.2 The Bourne Shell


2.2.1 General Information

Like other Unix shells, the Bourne shell (sh) is a command line interpreter, an interface between the user and the system, that can execute your commands, either interactively (as you type them at the prompt) or from a file containing a list of commands, called a shell script.

The Bourne shell is a popular choice for writing shell scripts because it is quick, compact, and contains less bugs than other shells. However, users often prefer other shells with a greater choice of command sets and features that better facilitate interactive use.

2.2.2 Using the Bourne Shell Interactively

  1. To begin using the Bourne shell,enter sh at the prompt.

    You are now in the Bourne shell, and any commands you type at the
    prompt will be interpreted by this shell. For information on Bourne
    shell commands, see the references at the end of this section.

  2. To exit the Bourne shell,type CTRL-d and press RETURN.

2.2.3 Writing Shell Scripts

Shell scripts are typed directly into a file that you can later execute.  The following section provides examples of Bourne-shell scripts using two common shell-script constructs, loops and if-tests.  For more information on shell-script constructs and writing shell scripts, see the references at the end of this section.

Sample Shell Script with Loop Construct

The following shell script is designed to make a copy of all files with the .f extension, and to give the new files the same names as the originals, but with .sav appended at the end.

#!/bin/sh
# Make a copy of each file with the .f extension.
# New files have same names as originals with .sav
# appended on the name.
ext=.f
files = `ls *${ext}`
for file in $files
do

done
exit 0

Explanation of Command Lines

  1. #!/bin/sh

    Specifies which shell is to be executed (here, the Bourne shell, sh).

  2. # Make a copy of each file with the .f extension.
    # New files have same names as originals with .sav
    # appended on the name.

    Comments are not part of the executable script, and are preceded by
    a #sign.

  3. ext=.f

    Sets the shell variable, ext to ".f ".

  4. files = `ls *${ext}`

    Executes the ls command with an argument to match all files
    ending with characters specified by the ext shell variable.
    The results of the ls command are stored as a list in the
    shell variable named files.

  5. for file in $files
    do
      echo "Copying ${file} "to" ${file}.sav
      cp ${file} ${file}.sav
    done

    These lines comprise a Bourne-shell loop construct. They are
    interpreted as follows: Set the shell variable file to
    the first entry in the list of entries stored in the shell variable
    files, execute the commands between the do and
    done statements, set the file variable to the next entry in
    files, and continue until file has been set to every entry
    in the list stored in files. The first statement within the loop
    (between the do and done statements) outputs text to
    standard output (in this case, the screen), indicating what the
    cp command will do.

    For example, if the variable file has been set to the entry
    program.f, the echo command will output: "Copying program.f
    to program.f.sav.
    " The next statement will copy the file,
    program.f
    to program.f.sav.

  6. exit 0

    Returns a status code (in this example, 0) that can be used by the
    shell to pass status information on to other shell scripts.

Sample Shell Script with If-Test Construct

The following shell script executes the date command if "yes" is entered in response to the question posed by the script.

#!/bin/sh
echo "Do you want to know today's date? Enter yes or no: \c"
read answer
if test ${answer} = yes
then

else fi

Explanation of Command Lines

  1. #!/bin/sh

    Specifies which shell is to be executed (in this example, the Bourne
    shell, sh).

  2. echo "Do you want to know today's date? Enter yes or no: \c"

    Outputs the text within quotations to standard output (by default,
    the screen).

  3. read answer

    Waits and reads characters from standard input, usually terminal input
    (what you type in at the keyboard).

  4. if test ${answer} = yes
    then
      date
      exit 1
    else
      exit 2
    fi

    These lines constitute the Bourne-shell "if" construct. Commands in
    between the then and else statements (in this example,
    the date and exit commands) are executed if the
    expression following the test command is true. The commands in between
    the else and fi statements are executed if the test
    command returns false.

    The test command has its own syntax to test for a variety of
    conditions, including string tests, numeric tests and file-type tests.
    This example tests a string match.

    The exit command returns a status code (in this case, either
    1 or 2) that the shell can use to pass status information on to other
    shell scripts.

2.2.4 Executing Shell Scripts

To execute a Bourne-shell script that has been stored in a file:

On-Line Information

Hardcopy References



  Return to top of page

2.3 The T C-Shell


2.3.1 General Information

The T C-shell (tcsh) is a command interpreter that is similar to the C-shell (csh). The syntax for writing scripts in the tcsh is the same as for the csh. See section 2.1.5 for examples. Unlike the csh, this shell is not available on every Unix system. However, it is available on the division's Sun, SGI, and HP workstations. The tcsh shell is the default when you log on to a Linux Intel system. The location for tcsh varies between the architectures.  You will need to include some branching code in your .login file to accomodate this: The advantage of the tcsh over the csh lies in its flexibility and numerous options. The man page provides details about the program. This document will highlight some of the T C-shell's interactive features and will describe how to make this shell your default login shell.

2.3.2 Features of the tcsh

One feature of the tcsh is command-line editing of previous commands using emacs-style or vi-style commands. This option is also available in the csh, but does not work consistently across HP, SGI, and Sun workstations. There are options for moving to the beginning and end of the command line, deleting characters and words, and many more of the common editing commands that apply in a single line setting. The default editing uses emacs-style commands. To use the vi editing style once you are in tcsh, enter If you want to switch back to emacs-style editing, enter A feature the tcsh shares with the csh is interactive command, filename, and username completion. This feature can save a lot of tedious typing. This command works by using the TAB key. (Command completion in the csh uses an ESC, which interferes with command-line editing when using vi.) For example, to use the feature for filename completion, type the command and then enough characters of the filename to uniquely identify it. Then press the TAB key to complete the name. As an example, suppose a directory had the following files in it.

To cat fort.doc, enter

and then press the TAB key. The tcsh completes the name. To cat file1.f, enter and press TAB.

Other features in tcsh

  • history mechanism that provides time-stamps of when the commands were executed
  • automatic logout option
  • flexible options for setting prompts
  • automatic periodic command execution
  • 2.3.3 Running the tcsh

    To set the tcsh as your login shell
    1. At the end of your .login file, place the following code:
      if ($MACH == sgi-sysv) then
        exec /usr/bin/tcsh
      else
        exec /usr/local/bin/tcsh
      endif

      The branching is required as tcsh is in a different location depending on the system architecture.

    2. In your .cshrc file, place the following line:
      setenv SHELL /usr/local/bin/tcsh       Sun, HP
      setenv SHELL /usr/bin/tcsh                   SGI
    To try out the tcsh without making it your login shell, enter at the prompt. When finished, type You will now leave the tcsh and return to your original shell.

    On-line Information



      Return to top of page

    Copyright © UCAR 1998 - Disclaimer - mmminfo@ncar.ucar.edu
    Last Modified: 1 December 2002