DistributedNetworks DistributedNetworks


Unix Shell Scripts   «Prev  Next»
Lesson 3 Defining a script file
Objective Explain what makes a file a shell script.

Defining a Unix Script File

The shell script contains commands that appear just as you would enter them on a command line. These commands are stored in a file, but they appear like regular commands that you would enter manually. For a file to be a shell script, it must have the following items:
  1. The first line of the file must indicate which shell to use for executing the script.
  2. The file permissions[1] must be set to allow the file to be executed as a program
  3. The file must contain valid commands

Defining the execution path

The first line of every shell script indicates the path and filename of the command interpreter, or shell, used to execute the commands in that script. This line begins with the characters #!. For example, your shell scripts written for the Bourne shell will all begin with the following line:

#!/bin/sh

If you write a shell script for the C shell (which is not covered explicitly in this course), the first line of your script would look like this:
#!/bin/csh

Because the first line of the script indicates which shell will execute the script, you can use one shell (such as the C shell) as your command-line environment, and write scripts that are executed by a different shell. Your command-line shell starts the shell you chose to run the script (which you specified on the first line of the shell script). When the script is finished, you return to your command line shell.
If you want to change to a different shell for your regular command line, ask your system admnistrator to change your user account settings. You will learn about setting the file permissions for a script file later in this Module.

Shell script file extensions

In many operating systems, file extensions[2] are used to indicate the content or purpose of a file. For example, word processing documents may end with .doc, text files may end with .txt, and postcript documents may end with .ps. Even when developing software, files containing source code written in the C language end with .c; the object code compiled from that source code is stored in a file ending with.o.
Shell scripts do not rely on any file extension. You will occasionally see a file with a filename ending in .sh, but this is simply to aid users in recognizing the file as a shell script. A file can have any filename and still be a shell script, as long as the three conditions listed at the beginning of this lesson are met.

Valid Commands

Every shell script must contain commands that would be valid if executed on a command line using the shell you have selected to run the script. Later modules describe how to use specific commands in your shell scripts.
The following MouseOver applet shows the components of a script that we have discussed in this lesson.

  1. The file permissions are set to include Execute
  2. The first line of the script includes the path of the script interpreter (the Bourne shell).
  3. The script contains valid commands.

Defining Shell Script File
Scripts are created in a text editor. The next lesson reviews the vi text editor.

Defining Script File - Quiz

Click the Quiz link below to test yourself on what you have learned so far about shell script files.
Defining Script File - Quiz

[1] File permission: An access right assigned to a file to determine which user can read, write, or execute a UNIX file.
[2] File extension: The last part of a filename, usually separated by a period (such as .tif or .doc), which indicates the format or function of the file