Top Banner
Guide to Parallel Operating Systems with Microsoft Windows XP and Linux Chapter 7 The Command Line
72

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Jan 14, 2016

Download

Documents

Ghita

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux. Chapter 7 The Command Line. Objectives. Use the command line Access Help files Display the contents of files Create script files to automate simple tasks. The Command Line. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Chapter 7The Command Line

Page 2: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 2

Objectives

• Use the command line

• Access Help files

• Display the contents of files

• Create script files to automate simple tasks

Page 3: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 3

The Command Line

• Non-graphical interface to the operation system

• Reasons for studying the CLI – To perform some tasks more efficiently– To troubleshoot a PC that has crashed– To more efficiently utilize system resources– To create powerful sets of commands– To develop better programming skills

Page 4: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 4

Using The Command Line

• Processing a command– Type a command on the keyboard

• Characters pass through the STDIN (standard input)

• Characters are echoed on the console

– Press Enter • Command processor performs appropriate action

– Results of command output to screen• Error message passes through STDERR (standard error)

• Other output passes through STDOUT (standard output)

• Input and output can be redirected

• The appearance of the console may be personalized

Page 5: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 5

Using The Command Line (continued)

Page 6: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 6

Using the Command Line with the Windows XP CLI

• Configure your command prompt

• Copy and paste text using the command prompt

• Explore the command history

• Complete filenames and directory names

Page 7: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 7

Customizing the CLI Window

• Right-click command prompt title bar to open menu

• Some options for manipulating the CLI– Restore: restore command prompt from the taskbar– Size: resize the command prompt with the arrow keys– Minimize: minimize command prompt to the taskbar– Edit: edit lines in the command prompt– Defaults: permanently change the default settings– Properties: change settings for current window only

• Click Defaults; Console Windows Properties opens– Default settings apply to all future command prompts

Page 8: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 8

Customizing the CLI Window (continued)

Page 9: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 9

Customizing the CLI Window (continued)

• “Command Prompt” Properties dialog box (for current prompt)– Options

• Set options, such as Buffer Size and Cursor Size– Font

• Select display font’s pixel size and shape• Verify selection in Selected Font window

– Layout• Set size of the screen buffer and size of the window• Control the positioning of the window

– Colors • Select colors for text and background (screen and popup)

Page 10: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 10

Using the Command Prompt Edit Menu

• Display menu: right-click title bar and then click Edit

• Tasks that may be performed– Mark: mark text using Shift and cursor movement keys– Copy: copy marked text to the Windows Clipboard– Paste: paste copied text from the Windows Clipboard– Select All: mark all of the text in the command prompt– Scroll: turn on scrolling; press Esc to turn off scrolling– Find: display a dialog box to find a text string

• Check QuickEdit Mode check box to enable a mouse– Box is in Options tab of Command Properties dialog

Page 11: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 11

Using the Command History

• Reusing a command applied in current session– Click within the window and then press the up arrow– To move forward in list, press the down arrow

• Press F7 to review a list of previous commands– Use arrow keys to move up and down list– Press Enter to copy highlighted command to prompt– Press Esc to close the window

Page 12: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 12

Using the Command History (continued)

Page 13: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 13

Using Shortcut Keys

• Use function keys to speed the entry of commands

• F2 key: copies current command in history buffer– Requires you to specify character to delimit text

• Example using the F2 key– Command in buffer is CD "\Program Files\TextPad 4“– You want to move up directory to Program Files– Press F2 and then “T” to copy line up to “T”– Command line now contains: CD "\Program Files\– Press Enter to execute the command

Page 14: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 14

Using Shortcut Keys (continued)

Page 15: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 15

Completing Filenames and Directory Names

• Type CMD /F:ON to enable completion feature

• Using the completion feature – Type partial directory name or filename – Type a key combination

• Ctrl+D to complete a directory entry

• Ctrl+F to complete a filename entry

– The system automatically completes the entry• Re-press key combination to scroll through entries

• Example: Type CD\pro and then Ctrl+D– Response: Program Files directory is supplied

Page 16: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 16

Console Redirection

• Redirect output to a file to create a command record– Example: DIR /S *.* > dirlist

• Redirects output of DIR to a file named dirlist• The > switch is the redirection operator

• Redirect output to a printer for a physical document– Local printer ports: PRN, LPT1, LPT2, and LPT3

• Example: DIR /S *.* > prn– Network printer uses Universal Naming Convention

• UNC format: \\servername\printername• Example: DIR /S *.* > \\Windows99\Printer

• Redirect input by using < after the command

Page 17: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 17

Using Filter Commands

• Functions performed by filter command– Take input from a file– Change the input in some way– Send output to a file or the standard output device

• Filter commands return essential information

• Three filter commands– FIND: searches for a string of characters– MORE: temporarily pauses display output – SORT: sorts lines in ascending or descending order

Page 18: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 18

Using the FIND Filter

• Searches lines of input for specified text string

• Syntax: FIND [/V] [/C] [/N] [/I] "string“ [[drive:][path]filename[...]]– /V: displays all lines without the specified string– /C: displays only the count of lines with the string– /N: displays line numbers with the displayed lines– /I: ignores the case of characters when searching

• Example: FIND "txt" dirlist > prn– Find all lines containing “.txt” extension in dirlist file – Redirects output of filter to a printer

Page 19: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 19

Using the SORT Filter

• Sort lines of input, output to standard output device– Output may be redirected to another device

• Syntax: SORT [/R] [/+n] [[drive1:][path1]filename1] [/O [drive2:][path2]filename2]

– /R: reverses the sort order; i.e., Z to A, 9 to 0– /+n: specifies character number n to begin comparison

• Example 1: SORT /+40 /R < dirlist– Sort output in descending order on column 40

• Example 2: SORT < dirlist > prn– Sort dirlist in ascending order, redirect output to printer

Page 20: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 20

Connecting Commands with a Pipe• Output of one filter routed directly into another filter

• Type a “|” (pipe) between filters to form connection

• Example: DIR /s *.* | MORE– Display directory files one screen (24 lines) at a time

• MORE syntax: command-name | MORE [/E] [/C] [/P] [/S] [/Tn] [+n]

– command-name: command whose output is displayed– /E: enables extended features

• Combine filter commands– FIND /V"<DIR>”dirlist | FIND /V"File(s)" | FIND/V"Total“

• Omit lines with “<DIR>”, “Files(s)”, “Total” in display

Page 21: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 21

Using the Command Line with Fedora Core 4

• Similar to Windows XP CLI

• Additional features explained in following sections

Page 22: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 22

Using Command-Line Symbols

• Know what you want to do with the files

• Know what type of symbols to use

• Table 7-3 lists many common command-line symbols

• Scripts use symbols to perform file-processing tasks

Page 23: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 23

Using Command-Line Symbols (continued)

Page 24: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 24

Using Command-Line Symbols (continued)

Page 25: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 25

Accessing Help

• Functions of a command reference– Refresh user’s memory – Reveal new commands

• Help: command reference in Windows XP

• Man pages: command manual for Fedora Core 4

Page 26: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 26

Accessing Help with the Windows XP CLI

• Type HELP at command prompt and press Enter

• Commands listed in alphabetical order

• Commands are briefly described

• Displaying Help information for a specific command– Type command followed by /?– Example: type DATE /? for DATE information

• Help describes command form and provides tips

Page 27: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 27

Accessing Help with the Windows XP CLI (continued)

Page 28: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 28

Accessing the Man Pages in Fedora Core 4

• Man (manual) pages provide help in Fedora Core 4

• Information displayed in the man pages– Name: name of the command– Synopsis: the syntax of the command– Description: description or use of the command– Options: the variety of switches you can use – Specific definitions: regular expressions or environment

variables– Diagnostics: return codes associated with command– Bugs: links to, or descriptions of, any known errors

Page 29: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 29

Accessing the Man Pages in Fedora Core 4 (continued)

Page 30: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 30

Displaying the Contents of Files

• Displaying files content in Windows XP– Use the PRINT and TYPE commands

• Displaying file contents in Fedora Core 4– Use the less, cut, head, tail, and grep commands

Page 31: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 31

Displaying the Contents of Files in the Windows XP CLI

• FINDSTR and MORE (discussed earlier in text)

• Two additional display commands: PRINT, TYPE

• PRINT syntax: PRINT [/E:device] [[drive:][path]filename[...]]– /E:device: specifies a print device– Local printer ports: PRN, LPT1, LPT2, and LPT3– Specifying network printer: \\servername\printername

• TYPE syntax: TYPE [drive:][path]filename– Display text file contents on a standard output device

Page 32: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 32

Displaying the Contents of Files in Fedora Core 4

• less: move forward and backward through a file

• less syntax: less [-[+]NpsS] [FILE]– Example: less: less –Np "/rc.d" /etc/inittab

• Starts file read at first occurrence of match (“/rc.d”)

• cut: used to extract fields from a file line (record)

• cut syntax: cut [OPTION]... [FILE]...– Mainly uses the –d and –f options– Example: cut –d: -f3-6 /etc/passwd | more -10

• Prints third field of lines in the /etc/passwd file

Page 33: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 33

Displaying the Contents of Files in Fedora Core 4 (continued)

Page 34: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 34

Displaying the Contents of Files in Fedora Core 4 (continued)

Page 35: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 35

Displaying the Contents of Files in Fedora Core 4 (continued)

• head: prints first 10 lines from file to standard output

• head syntax: head [OPTION]... [FILE]...– -n: change number of default lines to print– Example: head –n15 /etc/passwd

• Prints first 15 lines from /etc/passwd file

• tail: prints last N lines or bytes in a file

• tail syntax: tail [OPTION]... [FILE]...– Example: tail –n20 –v /etc/passwd | more– Prints the last 20 lines of the /etc/passwd file

Page 36: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 36

Displaying the Contents of Files in Fedora Core 4 (continued)

Page 37: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 37

Displaying the Contents of Files in Fedora Core 4 (continued)

Page 38: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 38

Displaying the Contents of Files in Fedora Core 4 (continued)

• grep command – Searches a file and prints lines matching search– Lists line numbers and counts to summarize results– Use wildcards such as * and ? to refine your search

• grep syntax: grep [options] PATTERN [FILE...]• Example: grep –n "root" /etc/passwd

– Searches /etc/passwd for “root”– Prints line numbers, count, actual lines of text

• Tip: use more command to help display information– Example: grep –n "nologin" /etc/passwd | more -10

Page 39: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 39

Displaying the Contents of Files in Fedora Core 4 (continued)

Page 40: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 40

Creating Script Files

• Script: batched instructions that do repetitive tasks

• Section goals– Learn to build basic script files– Enhance scripts with decision-making and repetition

• Script files are an act of programming

• Learn problem-solving process before writing scripts

Page 41: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 41

The Six-Step Problem-Solving Process

• General guidelines for solving problems– 1. Read the problem statement

• Then name the programming project– 2. Read the problem statement

• Then write a descriptive summary of the problem– 3. Read the problem summary

• Then list the known input elements– 4. Read the problem summary

• Then list the display (output) elements– 5. Read the problem summary

• Then identify and list the processing steps– 6. Read the problem summary

• Then define the constants and variables

Page 42: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 42

The Six-Step Problem-Solving Process (continued)

• Illustration: write a script listing certain filenames– Filenames in Accounting having “06” and .txt

• Results following each of the six steps– 1. Programming project is named “Text file for ’06”– 2. A descriptive summary of the project is written– 3. It is stated that the script file does not need input– 4. The items to be displayed are listed– 5. The processing steps are listed – 6. It is determined that no variables are needed

• Script written following six steps: CD accounting FIND "06" *.txt

Page 43: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 43

Creating Batch Programs in Windows XP

• Batch program (or file)– An unformatted text file with one or more commands– Has a .bat or .cmd extension

• Commands in a batch file carried out as a group

• Any command used at prompt can be batched

• Some XP commands designed for batch programs– CALL– FOR– IF

Page 44: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 44

Using Batch Parameters

• Batch parameters: information passed to program– %0 - %9: separated by spaces, commas, semicolons

• %0 has the name of the batch command

• %1 - %9 represent strings in numerical order

• Example: XCOPY %1\*.* %2 in file mycopy.dat– Run file with mycopy.bat C:\folder1 E:\folder2– %1 is replaced by Folder1, %2 is replaced by Folder2– Contents in Folder1 copied to Folder2

• Modifiers expand parameters to partial or full name– Example: %~d1 expands %1 to a drive letter

Page 45: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 45

Using Batch Parameters (continued)

Page 46: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 46

Displaying Text with the ECHO Command

• ECHO: displays text to the standard output device

• Syntax: ECHO [{on|off}] [message]– {on|off}: specifies whether command is on or off– message: specifies text to display on screen

• Enter ECHO without parameters to display setting

• Some ways to use ECHO command– Insert @ before ECHO in batch to prevent echoing

• Example: @ECHO off

– Type ECHO followed by a period to echo blank line

Page 47: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 47

Repeating Commands with the FOR Command

• for: execute command for a specific set of items

• Syntax: FOR {%%variable} IN (set) DO command [CommandLineOptions]– set parameter can represent a series of text strings

• Text strings may represent one or more file groups

– Items in set sequentially replace %%variable– Specified command acts on each item

• Sample script: @ECHO off FOR %%V IN (Blue Green Red) DO @ECHO %%V– Echo “Blue”, “Green”, and “Red” on separate lines

Page 48: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 48

Branching with the GOTO Command

• GOTO directs XP to a line identified by a label

• Syntax: GOTO label– Label must match a label in the batch program

• Label in a batch program must begin with a colon (:)– Line beginning with colon not processed as command

• Response by OS if specified label not in program– Label not found

Page 49: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 49

Making Decisions with the IF Command

• IF: performs conditional processing in programs • Three variations on basic syntax

– IF [not] errorlevel number command [else expression]– IF [not] string1==string2 command [else expression]– IF [not] exist FileName command [else expression]

• Extended syntax: IF [/i] string1 CompareOp string2 command [else expression]

• Conditional logic– If condition is true, following command is executed– If condition is false, following command is ignored

• Command in ELSE clause, if present, is executed

Page 50: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 50

Making Decisions with the IF Command (continued)

Page 51: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 51

Making Decisions with the IF Command (continued)

• Example 1: IF %errorlevel% LEQ 1 GOTO okay– Verify errorlevel <= 1 before going to okay label

• Example 2: IF EXIST filename. ( DEL filename.

) ELSE ( ECHO filename. missing.

)– If filename exists, delete it; else, echo filename missing

• Example 3: IF EXIST c:\mydir\nul GOTO process– Tests for the existence of a directory (via null device)

Page 52: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 52

Calling Other Batch Programs

• Call child batch programs without stopping parent

• Syntax: CALL [[Drive:][Path] FileName [BatchParameters]]

• Example 1: CALL child– Call child.bat from within another batch program

• Example 2: CALL child %1 %2– Pass two parameters to child before the call

Page 53: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 53

Pausing for User Response

• Performs two actions– Suspends processing of a batch program – Prompts the user to continue or exit

• Syntax: PAUSE

• Message after execution of PAUSE in batch file– Press any key to continue ...

• Message after entering Ctrl+C to stop program– Terminate batch job (Y/N)?

Page 54: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 54

Adding Comments to a Batch Program

• REM: used to include comments in a batch file

• Syntax: REM [comment]

• Specify any remarks after typing REM

• Example 1: REM Program: Backup

• Example 2: REM Author: Ron Carswell

Page 55: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 55

Changing the Position of Batch Parameters

• SHIFT: changes position of batch parameters – Parameters are not required

• Syntax: SHIFT

• How SHIFT works– Applied to batch parameters %0 - %9 – Copies each parameter into the previous one– Example: value of %1 copied to %0, %2 to %1, etc.

• SHIFT useful for same operation on all parameters

Page 56: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 56

Using Environment Variables

• System: define behavior of global OS environment– Available to all Windows XP processes– Can only be changed by administrators

• Local: define behavior in current instance of prompt

• Predefined variables allowed in batch programs– Example: @ECHO Today is %DATE% at %TIME%

• A few other predefined system environment variables– %PATH%: specifies search path for executable files– %RANDOM%: returns random number (0 – 32767)– %WINDIR%: retains location of OS directory

Page 57: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 57

Using Environment Variables (continued)

• Use SET to display, create, or remove variables

• SET without parameters displays current settings

• Syntax: SET [[/A [expression]] [/P [variable=]] string]– Use the /A switch to perform arithmetic– Use the /P switch to set a variable equal to user input

• Example: SET /A dice=%RANDOM% % 6 + 1 ECHO %dice%

– Set dice (new variable) to random number % 6 + 1 – Display the “roll” of %dice%

Page 58: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 58

Creating Scripts in Fedora Core 4

• Serve same function as batch files in Windows XP– Manipulate internal data and files in the OS

• Use six-step process to design programs

• Write Fedora Core 4 scripts with editor, such as Vim

Page 59: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 59

Linux Shells

• Linux shell scripts are interpreted, not compiled• Shell: provides script’s development and environment• Different scripts require different environments• Example: Bourne Again shell (bash)

– Includes aliasing and history functions from Korn shell– Takes a number of functions from the C shell

• Job control• Shell functions• Indexed arrays• Integer arithmetic

– Most Bourne shell scripts run under bash

Page 60: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 60

Header Line

• First line in a script

• Indicates shell environment under which script is run

• Example: #!/bin/bash– Statement shows a header line for a bash script

Page 61: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 61

Exit Return Code

• Code must be returned from a script

• Return code of 0 indicates success

Page 62: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 62

Executable Permission

• File user types: owner, user group, everyone else• Display permissions using the ls –l command

– First character in the detail line is the file type– Hyphen represents the script as a text file– Permissions mapped to user type in groups of three

• r = read, w = write, x = executable

• Example: enter ls –l /home/myScript– Output: -rw-r--r-- 1 User01

User01 303 Dec 4 13:54 myScript

– Permissions: Owner = rw, Group = r, Others = r • Use chmod to modify permissions on a file

Page 63: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 63

File Extensions

• Using extensions is optional

• Some administrators use .sh extension for scripts

Page 64: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 64

Script Parameters

• Batch parameters: information passed to program– %0 - %9: separated by spaces, commas, semicolons

• %0 has the name of the batch command

• %1 - %9 represent strings in numerical order

• Example: cp /home/myFile1 /home/myFile2– %1 is replaced by myFile1, %2 is replaced by myFile2– Contents in myFile1 copied to myFile2

Page 65: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 65

Conditional Execution

• Repetitive and conditional structures – Processing elements present in sophisticated scripts

• Properties of repetitive and conditional structures– Lines executed multiple times written once

• Avoid errors in consistency

• Simplicity makes scripts easy to maintain

– Clear branching should follow test of any condition

Page 66: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 66

if Command

• Directs execution based on test of a condition

• Two constructs used: if fi and if else fi

• if fi syntax: if [decision placed here]; then fi

– Used when only one decision path is needed

• if else fi syntax: if [decision ]; then else

fi– Used when two distinct decision paths are needed

• Decision in both constructs made by testing variable

Page 67: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 67

while command

• Enables multiple executions of a set of instructions– Repetition continues while a certain condition exists

• Syntax: while [ loop decision ] do done

– Limit for loop comes from testing a changing variable

Page 68: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 68

for in Command

• Counter-controlled repetition

• Syntax: for [somevalue] in [limitation variable] do done

• Two types of limitation variable for the loop – A single number – Operation on a list of items (loop while list not empty)

• Figures 7-22 and 7-23 demonstrate three constructs– if else fi: test for correct number of passed arguments – while: change ct variable while ct <= argument count– for in: echo passed arguments stored in list

Page 69: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 69

for in Command (continued)

Page 70: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 70

for in Command (continued)

Page 71: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 71

Summary

• Command line is used for efficient OS interaction

• Commands pass through STDIN to processor

• Command processing errors pass through STDERR

• Output passes through STDOUT

• Reference: Help files (Windows XP) and man pages (Fedora Core 4)

Page 72: Guide to Parallel Operating Systems with Microsoft Windows XP and Linux

Guide to Parallel Operating Systems with Microsoft Windows XP and Linux 72

Summary (continued)

• Some file content operations: displaying, searching, sorting

• Use six-step problem solving process to design effective programs

• Batch command files and scripts automate tasks

• Linux shell: provides script’s development and run-time environment

• Sophisticated scripts use repetitive and selection structures