Unix Shell Scripts   «Prev  Next»
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 if/then/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). 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 #.
 then
 cp /etc/passwd /etc/passwd.backup
 fi

By entering the text:
fi

Ad Linux Shell Scripting
To end the if statement, you signal to the shell that you have completed the if 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 if 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.

Entering shell script commands at the command line

  1. Enter the following command to start a Bourne shell: sh
  2. Enter the following command at the command line: if [ -f /etc/database.conf.sample ]
  3. Enter the following command: then
  4. Enter the following command: cp /etc/database.conf.sample /etc/database.conf
  5. Enter the following command: else
  6. Enter the following command: echo No sample configuration file is available.
  7. Enter the following command: fi
  8. The command is executed and displays the results of the commands you entered.

Example 4-1: Cleanup: A script to clean up log files in /var/log
# Cleanup
# Run as root, of course.
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Log files cleaned up."

A set of commands that could just as easily have been invoked one by one from the command-line on the console or in a terminal window. The advantages of placing the commands in a script go far beyond not having to retype them time and again. The script becomes a program or tool and it can easily be modified or customized for a particular application.

Example 4-2: cleanup: An improved clean-up script
#!/bin/bash
# Proper header for a Bash script.
# Cleanup, version 2
# Run as root, of course.
# Insert code here to print error message and exit if not root.
LOG_DIR=/var/log
# Variables are better than hard-coded values.
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
exit # The right and proper method of "exiting" from a script.
# A bare "exit" (no parameter) returns the exit status
#+ of the preceding command.

The next lesson defines what changes a text file into a shell script.
SEMrush Software 2 SEMrush Banner 2