Docstoc

Chapter 7 The Command Line

Document Sample
Chapter 7 The Command Line Powered By Docstoc
					Guide to Parallel Operating Systems with Microsoft Windows XP and Linux   7-1



Chapter 7
The Command Line

At a Glance

Instructor’s Manual Table of Contents
•   Overview

•   Objectives

•   Teaching Tips

•   Quick Quizzes

•   Class Discussion Topics

•   Additional Projects

•   Additional Resources

•   Key Terms
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                      7-2



Lecture Notes

Overview
        Before the advent of the graphical user interface (GUI), the only way to interact with
        the operating system was through the command-line interface (CLI). Given the user-
        friendly features of the GUI, use of the CLI has declined. However, the CLI should still
        be learned for a number of very good reasons: task efficiency, resource efficiency,
        emergency backup, refined commands, and programming skills.


Objectives
    •   Use the command line
    •   Access Help files
    •   Display the contents of files
    •   Create script files to automate simple tasks


Teaching Tips
Using the Command Line
    1. Use Figure 7-1 to illustrate the process of a command by the command console.

    2. Identify three data streams: STDIN (standard input), STDERR (standard error), and
       STDOUT (standard output).

    3. Indicate that both input and output can be redirected; for example, by sending output to
       a file instead of the standard output device (the monitor).

Using the Command Line with the Windows XP CLI

    1. The following sections cover configuring the command environment and using the
       command prompt.

Customizing the CLI Window

    1. The CLI customization menu can be accessed by right-clicking the title bar of the
       command prompt (see Figure 7-2).

    2. Define the term taskbar.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                      7-3

    3. Review the various CLI menu options, such as Restore, Defaults, and Properties. Focus
       on two dialog boxes: Console Windows Properties and "Command Prompt" Properties
       (see Figure 7-3). Explore the Options tab of the first dialog box and the Font, Layout,
       and Colors tabs of the second.


Teaching        Inform students that Console Windows Properties and "Command Prompt"
Tip             Properties have the same appearance, but serve different functions. The former is
                used to set defaults for future command prompts; the latter is used to configure
                the current command prompt.


Using the Command Prompt Edit Menu

    1. To display the Edit menu of the command prompt (see Figure 7-4), right-click its title
       bar and then click Edit.

    2. Describe the Edit menu options: Mark, Copy, Paste, Select All, Scroll, and Find.

    3. Reinforce the point that the mouse can be enabled for use with the command prompt by
       checking QuickEditMode.

    4. Explain how to copy and paste using either the mouse or the Mark option.

Using the Command History

    1. You may view past commands by using the up/down arrow keys at the command
       prompt.

    2. You may also call the list of commands by pressing F7 (Figure 7-5). Move up and down
       the list using, respectively, the up and down arrow keys. Pressing Enter causes the
       highlighted item to execute. Press Esc to close the window.

Using Shortcut Keys

    1. Remove the time-saving function keys listed in Table 7-1.

    2. Provide examples demonstrating the convenience of shortcuts. For instance, F2 is useful
       for copying part or all of the most recently entered command. Figure 7-6 shows the
       results of using F2 to copy CD "\Program Files\TextPad 4" up to the character "T".

Completing Filenames and Directory Names

    1. Enter CMD /F:ON to enable filename and directory name completion.

    2. To use the completion feature, type a partial directory name or filename at the
       command prompt and either Ctrl+D or Ctrl+F. Figure 7-7 shows the results of entering
       CD \pro and then Ctrl+D.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                         7-4

    3. If multiple directory names or filenames match the characters you enter, press the key
       combination again to move to the next instance. Press Enter to execute the command.

Console Redirection

    1. You may redirect output to a file or printer. Describe the appropriate syntax, including
       the ">" operator. Indicate that printers are specified with the UNC (Universal Naming
       Convention ) format.

    2. Provide examples of redirecting output to a file and a printer.

    3. Explain that input may also be redirected using the "<" operator. Provide an example of
       redirecting input.

Using Filter Commands

    1. Describe filter commands.

    2. Identify the three filter commands that will be discussed: FIND, MORE, and SORT.

Using the FIND Filter

    1. Review the syntax of the FIND filter, which searches lines of input for a text string you
       specify.

    2. Using examples from the text, show how to redirect input from a file into the FIND
       filter. Explain that you may also redirect output from FIND. In fact, input and output
       may be redirected in the same command construct.

Using the SORT Filter

    1. Review the syntax of the SORT filter, which arranges, or sorts, lines of input and sends
       the sorted output to the standard output device unless you redirect it.

    2. Referring to examples from the text, show how to construct commands using the SORT
       filter and redirection operators.

Connecting Commands with a Pipe

    1. You may route the output of one command into a filter command using a connection
       pipe ("|"). One common example involves the use of DIR with MORE . Consider the
       command DIR /s *.* | MORE. Explain that the output of DIR is filtered by MORE,
       which displays one screen of information (24 lines) at a time followed by --More--.

    2. Review the syntax of the MORE command. Indicate that pressing Enter advances the
       display by one line, while pressing the Spacebar advances the display by 24 lines (a
       screen).
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                     7-5

    3. Indicate that you can combine filter commands. Show the example in the text, which
       combines FIND filters.


Quick Quiz 1
    1. True or False: You must pass data into the command console from a keyboard (standard
       input device).
       Answer: False

    2. To copy text when you have enabled ____________________ mode in the Windows
       XP CLI, drag your mouse to select text and then press Enter to place it in the Windows
       Clipboard.
       Answer: QuickEditMode

    3. True or False: In the Windows XP CLI, the directory name and filename completion
       feature is enabled by default.
       Answer: False

    4. Instead of displaying the output of a command on the screen, you can
       ____________________ the output to a file or printer.
       Answer: redirect

    5. You can route the output of one command to a filter command using the
       ____________________ symbol.
       Answer: pipe

Using the Command Line with Fedora Core 4

    1. The CLI in Fedora Core 4 resembles the CLI in Windows XP, but has more features.

Using Command-Line Symbols

    1. Review the list of common command-line symbols in Table 7-3. These will inform
       scripts that will be written later in the chapter.

Accessing Help

    1. Learning to use reference material will help you complete tasks or discover new
       commands.

    2. Windows XP provides assistance in Help files. Fedora Core 4 provides assistance in the
       man (manual) pages.

Accessing Help with the Windows XP CLI

    1. To call a quick reference of available commands in Windows XP, type HELP (see
       Figure 7-8).
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                         7-6

    2. To obtain information and tips for a specific command, type the command followed by
       /?. See information for the DATE command in Figure 7-9.

Accessing the Man Pages in Fedora Core 4

    1. The man (manual) pages provide information about commands in Fedora Core 4.

    2. Review the structure of the man pages, which includes up to seven information sections
       for each command (Figure 7-10).

Displaying the Contents of Files

    1. In Windows XP, the PRINT and TYPE commands can be used to display file contents.

    2. In Fedora Core 4, you can use the less, cut, head, tail, and grep commands to display
       file contents.


Teaching        Distinguish commands that display file contents, such as less and more, from
Tip             commands that describe the type of content in files, such as file.


Displaying the Contents of Files in the Windows XP CLI

    1. Review the syntax of the PRINT command, which enables you to output the contents of
       a text file to a printer. Indicate that you can use local printer ports or a network printer
       as the output device. Network printers should be specified in the UNC format.

    2. Review the syntax of the TYPE command, which displays the contents of a text file on
       the standard output device.

Displaying the Contents of Files in Fedora Core 4

    1. The less command is like more, but has additional capabilities. It can move backward
       through files and read just a portion of files. The latter feature enables you to access
       larger files faster.

    2. Review the syntax of the less command. Figure 7-11 illustrates the results of using the
       less command to display the contents of a file starting at the first occurrence of a
       pattern.

    3. Review the syntax of the cut command, which extracts fields from a file line (record).
       Indicate that the -d and -f options are most frequently used. Additionally, the default
       output device is the monitor, unless redirected.

    4. Figure 7-12 shows the results of using the cut command to print the third field of the
       lines in the /etc/passwd file. Figure 7-13 shows the results of using the cut command to
       print the first 15 characters of the lines in the /etc/passwd file.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                      7-7

    5. Review the syntax of the head command, which prints the first 10 lines from a file to
       standard output. For example, Figure 7-14 shows the results of using the head command
       to print the first 15 lines from the /etc/passwd file.

    6. Review the syntax of the tail command, which prints a specified number of lines or
       bytes at the end of a file and allows you to move backward through the file. Figure 7-15
       shows the results of using the tail command to print the last 20 lines of the /etc/passwd
       file.

    7. Review the syntax of the grep command, which can be used to search a file and then
       print all the lines that match your search. You may use wildcard characters such as *
       and ? to refine a search. Figure 7-16 shows the results of a grep command that prints the
       line numbers, count, and actual lines of text that contain the “root” pattern in the /etc/
       passwd file. The results of piping grep to more (to control the display of information)
       are shown in Figure 7-17.


Quick Quiz 2
    1. In Fedora Core 4, Help files are called ____________________ pages.
       Answer: man

    2. To redirect the output to a network printer in the PRINT command, specify the printer
       using the ____________________ format of \\servername\printername.
       Answer: UNC (Universal Naming Convention)

    3. True or False: The less command is required to read an entire file.
       Answer: False

    4. In Fedora Core 4, the ____________________ command prints a specified number of
       lines or bytes at the end of a file and allows you to move backward through the file.
       Answer: tail


Creating Script Files
    1. Define the term script files.

    2. In the following sections, students will be shown how to build scripts. These scripts will
       be enhanced with decision-making and repetition structures.

    3. Analogize script writing to programming. Emphasize that before writing a single line of
       code, students should apply the high-level six-step method to any programming
       problem.

The Six-Step Problem-Solving Process

    1. Explain the importance of developing a systematic approach to solving problems.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                          7-8

    2. Review the six-step problem solving process, which may be summarized as follows:

            •   Read the problem statement, name the project.
            •   Re-read the problem statement, write a summary.
            •   Use the summary to list the input elements.
            •   Use the summary to list the output elements.
            •   Use the summary to list the processing steps.
            •   Use the summary to define constants and variables.

    3. Apply the six-step process to create a script program that lists all filenames with a .txt
       extension and an “06” in the Accounting directory.


Teaching        Before moving from Step 6 to writing code, it would be helpful to write an
Tip             algorithm in pseudocode, a high-level condensation of all six steps. The high-
                level algorithm solidifies the logic of the solution and moves you closer to the
                syntax of the programming language.




Teaching        Studies show that up to 85% of the time spent constructing a program is actually
Tip             dedicated to high-level preparation, as represented by the six-step process. It is
                important to emphasize this statistic with beginning programming students, who
                tend to begin writing code without fully exploring the underlying problem.


Creating Batch Programs in Windows XP

    1. Define the term batch program.

    2. Indicate that any command used at the command prompt may also be used in a batch
        program. List the commands specifically designed for batch programs: CALL, ECHO,
        FOR, GOTO, IF, PAUSE, REM, SET, and SHIFT.

Using Batch Parameters

    1. Define the term batch parameter. Describe the format of batch parameters.

    2. Show the example using batch parameters with XCOPY.

    3. Review the batch parameter modifiers in Table 7-6.


Teaching        Remind students that they must save batch programs with a .bat or .cmd
Tip             extension. The new batch file may be edited by right-clicking the file icon in
                Windows Explorer and then clicking Edit.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                         7-9



Displaying Text with the ECHO Command

    1. Review the syntax of the ECHO command, which re-displays text that you have
       entered.

    2. Explain how to turn off echoing and how to echo a blank line. Review the pertinent
       example in the text.


Teaching       To speed up learning, students should run each of the batches specified in the
Tip            latter portion of the text. Before entering a processing request, they should
               navigate to the directory location containing the batch files.


Repeating Commands with the FOR Command

    1. Review the syntax of the FOR command, which lets you repeat the execution of a
       specific set of commands.

    2. Explain that the set parameter in the FOR loop may be filled with string literals, files,
       directories, or a range of values. Show the examples in the text; the output of the first is
       represented in Figure 7-18.

Branching with the GOTO Command

    1. Review the syntax of the GOTO command, which directs Windows XP to a labeled
       line; the command on the next line below is then executed.

    2. Emphasize that the label you specify must match a label in the program, which should
       also be preceded by a colon (:).

Making Decisions with the IF Command

    1.   Review the three basic variants of the IF command, as well as the extended syntax. The
         IF command performs conditional processing and makes decisions in batch programs.

    2. Review the comparison operators used in the extended IF syntax (Table 7-8).

    3. Cover the examples in the text using IF.

Calling Other Batch Programs

    1. You can call a child batch program from a parent batch program, without stopping the
       latter.

    2. Review the syntax of the CALL command. Cover the examples in the text using CALL.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                      7-10

Pausing for User Response

    1. Review the syntax of the PAUSE command, which suspends processing of a batch
       program and displays a message prompting the user to press any key to continue.

    2. Show the example in the text, which uses PAUSE to create interactive I/O.

    3. Define the term infinite loop. Indicate that pressing Ctrl+C will terminate a program
       caught in an infinite loop.


Teaching       Infinite loops are usually created by a failure to properly update a limitation
Tip            (control) variable. The source of the error may be semantic (faulty logic) and/or
               syntactic (a missing or improperly written symbol(s)).


Adding Comments to a Batch Program

    1. Review the syntax of the REM command, which enables you to include comments or
       remarks in a file.

    2. Explain the importance of accurately and clearly commenting batch files.

Changing the Position of Batch Parameters

    1. Review the syntax of the SHIFT command, which changes the positions of batch
       parameters in a batch file.

    2. Show the batch program example by using SHIFT to display the contents of the files
       listed in the batch parameters.

Using Environment Variables

    1. Define the term environment variables. Distinguish between system and local
       environment variables.

    2. Using Table 7-9, review the predefined environment variables that can be used in your
       batch files.

    3. Review the syntax of the SET command, which is used to display, create, or remove
       environment variables. SET can also be used to store numeric or text values in variables.

    4. If you use the /A switch with the SET command, you can perform arithmetic. Table 7-10
       lists the arithmetic operators for the SET command.

    5. Review the examples in the text that use the SET command.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                       7-11

Creating Scripts in Fedora Core 4

    1. Discuss the similarities between Fedora Core 4 scripts and Windows XP batch
       programs.

    2. Indicate that the six-step problem solving process covered earlier applies to Fedora Core
       4 scripts.

Linux Shells

    1. Define the terms interpret and compile.

    2. Indicate that Linux shell scripts are interpreted, they are not compiled. Additionally,
       shell scripts provide both a script’s development and its run-time environment.
       Different scripts require different environments.

    3. Describe the Bourne Again (bash) shell. Review the Fedora Core 4 shell types in Table
       7-11.

Header Line

    1. Define the term header line.

    2. Show the example of a header line for a bash script: #!/bin/bash

Exit Return Code

    1. The statement exit 0 concludes all scripts. A script returning code 0 is a success.

Executable Permission

    1. Emphasize that when you are creating and running shell scripts, you must change the
       permissions on the file before running it.

    2. The three file permissions are read, write, and execute. These permissions are assigned
       for the file owner, a group of users, and everyone else.

    3. Review the Fedora Core 4 permissions in Table 7-12. Show how to calculate the
       numerical permission value for each file entity (owner, group, and everyone else).

    4. Show how to modify permissions with the chmod command.

File Extensions

    1. The use of file extensions in Fedora Core 4 scripts is optional. For the sake of clarity,
       some system administrators use a .sh extension on their scripts.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                         7-12

Script Parameters

    1. Script parameters in Fedora Core 4 serve the same function as batch parameters in
       Windows XP.

    2. Review the list of command-line reference parameters in Table 7-13.

Conditional Execution

    1. Special structures are required to perform repetitive and conditional processing.

    2. Review the two important properties of repetitive and conditional structures.


Teaching       As you review the syntax of the repetition and decision-making structures,
Tip            remind students that the interpreter is rigid with respect to format. For example,
               the brackets surrounding a test expression must include the proper spacing.


if Command

    1. The if command controls instruction execution based on the evaluation of a condition.

    2. Review two constructs associated with if: if fi and if else fi. During program execution,
       the former either adds or ignores an instructions, while the latter chooses between two
       instructions.

    3. Illustrate the use of if else fi in Figure 7-19.

while Command

    1. The while command executes a set of instructions while a condition is true.

    2. Review the general syntax of the while command. Emphasize the need to update the
       limit variable.

    3. Using Figures 7-20 and 7-21, illustrate the use of the while command.

for in Command

    1. The for in command executes a number of instructions based on a maximum count you
       specify.

    2. Review the general syntax of the for in command. Emphasize two ways the structure is
       exited: a counter has been updated to a termination value or a list of items has been
       exhausted.

    3. Figure 7-22 illustrates a script using the for in loop; the results of the script are shown in
       Figure 7-23.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                      7-13


Quick Quiz 3
    1. ____________________ files are lists of CLI instructions that are batched together in
       one document or a small program to help you automate repetitive tasks.
       Answer: Script

    2. Batch ____________________ can contain any information you need to pass to a batch
       program when you execute the program.
       Answer: parameters

    3. You can define the behavior of the command processor or the entire operating system
       environment by using two types of ____________________ variables—system and
       local.
       Answer: environment

    4. True or False: Linux shell scripts are compiled, they are not interpreted.
       Answer: False

    5. The limit for the while loop comes from testing a changing ____________________.
       Answer: variable


Class Discussion Topics
    1. What are the benefits of learning to interact with your PC via the CLI?

    2. Compare Console Windows Properties and "Command Prompt" Properties, dialog
       boxes opened from the Windows CLI menu.

    3. Compare the output redirection operators (>, >>) with the pipe (|) operator. How do
       both broaden your command processing capabilities?

    4. What are the benefits of becoming acquainted with the Help files in Windows XP and
       the man pages in Fedora Core 4?

    5. Why is a script considered to be a kind of program?

    6. What are the benefits of batch programming?

    7. How does the use of repetition and decision-making structures enhance your scripts?
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                     7-14


Additional Projects
    1. Practice using the command prompt shortcut keys shown in Table 7-1. First, enter the
       following command at the CLI prompt: CD "\Program Files\TextPad 4". Next, use
       shortcuts to perform the following tasks:

                •   Bring back the entire command just entered.
                •   Move the cursor to the beginning of the line.
                •   Delete the characters making up CD.
                •   Show the command history.
                •   Re-enter the original command from the historical list.

        Identify the primary function key used for each task. Were the shortcuts used the most
        efficient way to perform the given tasks? Explain in 1- 2 paragraphs.

    2. Compare the search operations in the Vim editor (discussed in Chapter 6) to search
       operations performed with the grep command. Is there any relationship between the
       two? Are there significant functional differences? Which approach is more user-
       friendly? Summarize your findings in 2-3 paragraphs.

    3. Scripts are actually program segments. As such, they can be described in terms of
       syntax and semantics. Research the meaning of the words "syntax" and "semantics" in
       relation to programming. Refer to the code segment at the end of the section entitled,
       "The Six-Step Problem-Solving Process". Describe the syntax and semantics of the
       code segment. Summarize your results in 2-3 paragraphs.

    4. Use the six-step problem-solving process to construct a shell script that performs the
       following:

            •   Adds the odd numbers from 1 – 100.
            •   Echoes each calculation performed.

        Hint: You will need to define variables and create repetition and decision structures.
        Record each of the six steps. Before coding the routine, write the algorithm in English
        pseudocode. Ensure that your implementation is commented.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                 7-15


Additional Resources
    1. General information about the CLI:
       http://en.wikipedia.org/wiki/Command_line_interface

    2. Windows XP CLI:
       www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-
       us/ntcmds.mspx?mfr=true

    3. KDE Konsole:
       http://konsole.kde.org/
    4. Using filters in Windows XP:
       www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-
       us/filters.mspx?mfr=true

    5. Batch programming:
       www.computerhope.com/batch.htm#1

    6. Linux Shell Scripting:
       www.freeos.com/guides/lsst/


Key Terms
        batch program: A program that executes without interacting with the user.
        chmod command: A command in Fedora Core 4 that changes file permissions.
        CompareOp: A three-letter comparison operator used by the IF command in Windows
        XP.
        cut command: A command in Fedora Core 4 that extracts fields from a line in a file or
        extracts an entire record from a file.
        ECHO command: A command that displays text to the standard output device.
        environment variables: Named storage locations you can use to determine the
        behavior of the command processor and the operating system. Two types are available:
        system and local environment variables. You need administrative privileges to use
        system variables; you can use local environment variables to control the operation of
        batch files.
        FOR command: A command that executes a section of code a specified number of
        times in Windows XP. Command syntax can vary depending on the task the loop needs
        to perform.
        for in command: The Fedora Core 4 version of the for command. You can use the
        command without the in and vary the syntax depending on the task the loop needs to
        perform.
        GOTO command: A command you use in Windows XP programs to transfer execution
        to some other statement; the high-level equivalent of a branch or jump instruction.
        grep (global regular expression print) command: A command in Fedora Core 4 that
        searches a file or files by keyword.
        head command: A command in Fedora Core 4 that prints the first 10 lines (by default)
        from a file to standard output.
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux                       7-16

        IF command: A Windows XP command that executes a block of statements if a
        decision expression evaluates as true; when an ELSE clause is included, its statements
        execute if the decision evaluates as false.
        if else fi construct: A command in Fedora Core 4 that works like the IF command in
        Windows XP when the ELSE clause is included.
        if fi construct: A command in Fedora Core 4 that works like the IF command in
        Windows XP.
        less command: A command that allows backward movement when you view file
        contents.
        man pages: The Help file documents in Fedora Core 4.
        parameters: In programming, a value given to a variable, either at the beginning of an
        operation or before a program evaluates an expression. The parameter can be a string, a
        number, or another parameter name.
        PAUSE command: A Windows XP command that temporarily stops the operation of a
        program or command.
        REM command: A Windows XP command to add comments (remarks) to a program
        or batch file.
        script: A program that consists of a set of instructions to control some function of an
        application or utility program; these instructions typically use the rules and syntax of
        the application or utility.
        SET command: A Windows XP command that displays, creates, or removes
        environment variables.
        SHIFT command: A Windows XP command that changes the position of batch
        parameters in a batch file.
        synopsis: A term used in the Fedora Core 4 man pages to describe the syntax of
        commands.
        tail command: A command in Fedora Core 4 that typically prints the last 10 lines or
        bytes of a file. For large files, this command can provide faster access to data within the
        file.
        taskbar: A graphic toolbar used in Windows operating systems to select active
        applications.
        TYPE command: A Windows XP command that displays the contents of a text file on
        the standard output device.
        UNC (Universal Naming Convention) format: A system of naming files on a network
        so that they have the same pathname when accessed from any of the networked
        computers.
        while command: A command that enables you to execute a group of statements a
        specified number of times.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:236
posted:11/15/2011
language:English
pages:16