Unix Shell Scripts   «Prev  Next»
Lesson 9 Running the script
Objective Run a script from a command line.

Running the Shell Script

Once you have a shell script file created with the correct file permissions set, you can execute that shell script from any command line. Because the shell script file that you have been creating in this module automatically starts the Bourne shell (using the information on the first line of the script), you can start your shell script file from within any shell, including the C shell used by default in the DispersedNet UNIX Lab.

Indicating the location of your Script

You execute a shell script in UNIX just as you would any other program, by entering the name of the file that you want to execute. When you enter a standard UNIX command, however, that command is located in a standard directory on the system, such as /usr/local/bin. Your script file is probably not located in one of the standard directories. Instead, it is probably located in your home directory. If you use the script name alone (welcome in this example), the script file cannot be located:
% welcome
welcome: not found

When you execute any program, UNIX searches all the standard directories included in your PATH environment variable. To indicate that your shell script is not in one of those standard directories, you must indicate where the script is located. In most cases, the script is located in your current working directory, which can be indicated with a period (.).
To start a shell script called welcome that is located in your home directory, use this command:
% ./welcome

The script is executed.

Using the sh command

Another method of starting a script is to start a Bourne shell explicitly and included the name of the shell script that the shell should execute. For example:
% sh ./welcome

Using this syntax allows you to include debugging information as you start the shell. (We do not describe debugging in this course, however.) In addition, if you are concerned that the shell specified in the first line of the script may not be valid (maybe it’s an older version of the shell), you can force the script to be executed by a specific shell using this syntax. This is rarely needed, however.

Shell Script File Permissions - Quz

Click on the Quiz link below to test your knowledge of file permissions and script execution.
Shell Script File Permissions - Quiz
The next lesson wraps up what you have learned in this module.

SEMrush Software9