| Lesson 2 || An interactive “script” on the command line |
| Objective || Execute script commands without creating a script file. |
Unix interactive Script on the Command Line
In previous modules you learned that a shell script is a collection of commands that would otherwise be entered at a command-line prompt, but which have been collected in a file for easy or repeated use as a program or script.
Control commands executed at a command prompt
You have also learned that shell scripts include control commands such as
else statements and looping structures that can repeat blocks of commands. These special commands can also be executed at a command prompt, either to demonstrate how a script works, or to complete a task that requires decisionmaking (either a test of some sort or a repeated command).
Entering required information
When you begin a statement on the UNIX command line with a keyword that requires additional information, the shell does not finish executing your command until you have entered all of the required information.
This is true even if the command is several lines long. In these cases, the shell prompt changes to show you that more information should be entered to complete the current command.
As an example of this, you can use the
if command on a command line to determine whether to perform an action.
When you type the following text at a Bourne shell command line:
$ if [ -e /etc/passwd ]
The shell prompt changes to the character
>. This new type of prompt is an indication to you that you are still entering part of a multiline command. You then can enter additional lines, as if you were writing a shell script. For example, the display might look like this:
Shell prompt: The character used by a shell to indicate that it is ready to accept commands entered by a user from the keyboard.
Standard shell prompts are $, %, and #.
cp /etc/passwd /etc/passwd.backup
By entering the text:
To end the
statement, you signal to the shell that you have completed the
statement that you began.
The entire set of lines that you entered is then executed.
Later in the course, you will learn in detail how to use if statements to test conditions as shown in this example and in the simulation that follows.
Using commands like
on the command line is usually done to test how a command works, although some of the tests and loops that you will learn about later can be useful on the command line as well as in shell script files.
Entering Shell Script Commands
at command Line
The next lesson defines what changes a text file into a shell script.