Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

52
www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces

Transcript of Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

Page 1: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

FIT1001- Computer Systems

Lecture 11

User Interfaces

Page 2: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 2

Lecture 11: Learning Objectives

• Explain the concept of user interfaces

• Discuss user interface implementation as command, menu and event-driven systems

• Discuss the historical development and design issues of MS DOS, MS Windows and UNIX / Linux in relation to user interface design and implementation

• Explain the concepts of shells and their existence in modern operating systems

• Demonstrate the difference and differentiate between basic CLI commands under MS DOS / Windows and Unix / Linux

• Demonstrate the ability to create and execute a basic command script or batch file

• Demonstrate the ability to modify command environment variables using CLI and GUI interfaces

Page 3: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Introduction

Page 4: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 4

Introduction - Interfaces

We have been studying the components of modern computer systems. Each component is connected to others using interfaces. These are hardware interfaces…

What makes computing machinery unique is that the action of the machine is programmable.

Therefore much depends on preparing, installing and using the right sequence of programs…

Programs are inter-connected using layered software interfaces…using something like subroutine call instructions to cross layers

But we interact and control programs through a User Interface

Page 5: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 5

The Operating System Interface

• The Operating System performs common function for programs

– Thread & Process management

– Device & interrupt handling

– File systems for data storage & retrieval

– Loading & Running programs from files

– Passing messages between processes

– Etc…

• Programs request these functions using Trap or Subroutine calls via System Interface

Page 6: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 6

The User Interface

• Users provide input and receive output through the user interface

• Two types of user interfaces– Specialised or Application interface used to interact directly

with application programs– General interface used to manage Applications

• User interface may be:– Textual:

> Line of text: data or commands

> Selection from a numbered Menu list

– Graphical:> Graphic object selection + pointing device event

> As well as some textual input

Page 7: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 7

Introduction - Interfaces

User Interface Operating System

System Interface

Page 8: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 8

Introduction - Interfaces

• Modern operating systems include generalised user interfaces as standard components

• Both textual and graphical interfaces usually included• These interfaces are usually implemented as utilities

that run like any other application program and rely on support from Operating System for device I/O

• Allow users to make selections and enter commands• Accepts and Interpret commands and then executes

them via System Calls• Multi-user systems start a separate interface program

for each user, using multi-programming features of Operating system.

Page 9: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 9

History of User Interfaces

• Early machines had limited memory and only had character-oriented (ie non-graphical) devices

• Data and commands were entered as lines of text– Command Line Interpreter (CLI)– Shell– Syntax used to represent command name and parameters

• CLI commands included– File manipulation: Create, Delete, Copy files…– Program control: Run, Stop, List programs…– Machine control: Start, Shutdown, Load/unload media…

• Sometimes also (inaccurately) known as:– Batch Job– Job Control Language (JCL)

Page 10: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 10

History of User Interfaces• Graphical User Interfaces became common when:

– Computers became more powerful (ie faster & more storage), – provided multi-programming operating systems,– with support for advanced graphical devices:

> pointing device input

> graphical device output

• Use pointing devices– To select objects and implicitly execute programs to process them

• GUI’s use the “desktop” metaphor– Objects represented as images (“icons”) on screen

• Properties and context of objects on desktop:– used to select programs and as operands during execution– “Object-Oriented” methods…

Page 11: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Methods of Implementing

User Interfaces

Page 12: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 12

Types of User Interfaces

• Command Interpreter and Shell (CLI)– Lines of text, with syntax used to specify action– I/O redirection– Pipelined commands– Foreground/Background

• Menu– Hierarchical lists of choices– Selection by pressing a key (usually the number chosen)

• Graphic User Interface (GUI)– Multiple concurrent input devices– Graphical “Windowed” output– Icons used to represent data objects– Properties of objects and method of selection

used to specify action

Page 13: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Command Interpreters and Shells

Also known as Command Line Interfaces (CLI)

Text based interfaces

…don’t need graphical output devices…

Page 14: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 14

Command Interpreters and Shells (CLIs)

AnalyseSyntax

ExecuteCommand

Prompt & ReadCommand

Exit

Start

End ofFile?

1. Write prompt to output2. Read line from input3. Analyse text:

1. Separate the tokens,2. identify command, options and

parameters. Eg first word is command and other words are parameters.

4. Execute the command and make parameters available to command for execution

5. Repeat the above, until end is reached Eg end of input file.

Page 15: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 15

CLIs and ShellsC:\Documents and Settings\Chris Freeman> HELP COPYCopies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

source Specifies the file or files to be copied. /A Indicates an ASCII text file. /B Indicates a binary file. /D Allow the destination file to be created decrypted destination Specifies the directory and/or filename for the new file(s). /V Verifies that new files are written correctly. /N Uses short filename, if available, when copying a file with a non-8dot3 name. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. /-Y Causes prompting to confirm you want to overwrite an existing destination file. /Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.This may be overridden with /-Y on the command line. Default isto prompt on overwrites unless COPY command is being executed fromwithin a batch script.

To append files, specify a single file for destination, but multiple filesfor source (using wildcards or file1+file2+file3 format).

Page 16: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 16

C:\Documents and Settings\Chris Freeman> HELP COPYCopies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

source Specifies the file or files to be copied. /A Indicates an ASCII text file. /B Indicates a binary file. /D Allow the destination file to be created decrypted destination Specifies the directory and/or filename for the new file(s). /V Verifies that new files are written correctly. /N Uses short filename, if available, when copying a file with a non-8dot3 name. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. /-Y Causes prompting to confirm you want to overwrite an existing destination file. /Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.This may be overridden with /-Y on the command line. Default isto prompt on overwrites unless COPY command is being executed fromwithin a batch script.

To append files, specify a single file for destination, but multiple filesfor source (using wildcards or file1+file2+file3 format).

Eg. The DOS HELP command

Page 17: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 17

• The command line syntax is used to provide details of what command to run and other options

• Most CLIs and shells treat words as separate items• The first word is usually the name of the command

• Commands may be internal or external to CLI itself.Eg. cd is internal, java is externalThe program for Internal commands is usually part of the CLI program itself (like a subroutine), but external command are actually quite separate programs, run from a program file whose name is the same as the command (eg. java.exe)

java –classpath MarieSim.jar MarieSim1

Command Option Parameters (all separated by spaces)

Options indicated by leading “–” or “/”

CLI and shell Syntax

Page 18: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 18

I/O redirection

• Usually the INPUT is the keyboard and OUTPUT is the screen (or a text window on the screen!!)

• But the CLI works just as well if INPUT is from a file containing command lines

– Known as REDIRECTED INPUT

• Similarly, OUTPUT can be directed to a file too!• In a command, the special characters “<“ and “>”

followed by a filename, will cause redirection of INPUT and OUTPUT (respectively)

Eg HELP COPY > COPY.TXTwill create an outputfile called “COPY.TXT” containing the results of the HELP COPY command

Page 19: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 19

Pipelined Commands and Filters• When a sequence of commands is to be executed and

the OUTPUT of one command is to be used as the INPUT to the next command, use a PIPELINE.

• The special symbol “|” is used to separate pipelined commands on a single lineEg. HELP COPY | MORE feeds the OUTPUT of the HELP command as INPUT to the MORE command. This will display the HELP output and pause before scrolling the text off the page…

• Successive commands can also change the text as it passes through the pipeline to create a “filter”Eg. HELP COPY | FIND “file” | MOREonly displays those lines containing the word “file”

Page 20: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 20

Foreground and Background

• Commands are naturally executed one at a time (see the diagram on Slide 14…)

• The execution of command program is synchronised with the reading of the command line text.

• So it is not possible to run more than one command at a time… unless you have a multiprogramming OS!

• Creating a new process in which to run the command, allows the CLI process to go back and read a new command, while the previous command is still running.

• The CLI process is running in the “Foreground” while the command is running in the “Background”

Page 21: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 21

Parameters and Environmental Variables

• When a program runs we often need to specify parameters for use while the program is runningEg. the name of a file to be created, printed, deleted, etc…

• Of course, these may be embedded as data within the program, but then the details cannot be changed… without editing & recompiling the program!

• Hence the need for command-line parameters…Details that are entered with the command and passed to the program for use during execution

• Same program, different parameters each time…no need to edit or recompile the program Eg. “HELP COPY” command=“HELP” parameter=“COPY”

Page 22: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 22

Parameters and Environmental Variables• Most CLIs and Shells can also define parameters and

associate these with named text storage areas• The collection of these named areas is called the

“Shell Environment” and is used to store parameters used by the CLI or shell itself

• The parameters included in the CLI “environment” are also passed to running programs and form a convenient way to define parameters that are used for an entire login session

• Such as the PATH parameter in the CLI environment: used to hold the “Search Path”; a list of directories that will be searched when the CLI needs to find an external program file

Eg SET | MORE will display the environment variables

Page 23: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 23

Parameters and Environmental Variables

Page 24: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 24

Menu interfaces

• Provided a structured interface in a Line-Oriented Character-based environment ie. NO GRAPHICS

• Presented the user with a list of numbered choices• User then selected an item from the menu by entering

the corresponding number or navigating to that choice using the arrow keys and pressing Enter

• Advanced menu system allowed user to quickly navigate through hierarchy of choices using keyboard

• System interface standards (eg IBM SAA/CUA) were used to make all applications operate the same way

• A mouse interface was added Later• Eg. Microsoft DOSshell, Norton Commander

Page 25: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

A simple MENU interface

Demonstration of MENU.BATDownload from http://waserv.netcomp.monash.edu.au/~fit1001/tutorial/SG11/menu.zip

Page 26: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 26

A simple MENU systemREM A sample menu batch file@ECHO OFF:BEGINCLSTYPE menu0.txtSET X=0:LOOPCHOICE /C:012345 /N /T:5,5IF ERRORLEVEL 1 SET X=0IF ERRORLEVEL 2 SET X=1IF ERRORLEVEL 3 SET X=2IF ERRORLEVEL 4 SET X=3IF ERRORLEVEL 5 SET X=4IF ERRORLEVEL 6 SET X=5IF %X% == 0 GOTO BEGINIF %X% == 1 CALL menu1.batIF %X% == 2 CALL menu2.batIF %X% == 3 CALL menu3.batIF %X% == 4 CALL menu4.batIF %X% == 5 GOTO FINISHGOTO LOOP:FINISHECHO Terminating....EXIT

The CHOICE program==================Waits for the user to choose one of a set of choices. If the /T: option is present, it will Time-Out and return to DOS with Default choiceCHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

/C[:]choices Specifies allowable keys. Default is "YN"/N Do not display choices and ? at end of prompt string./S Treat choice keys as case sensitive./T[:]c,nn Default choice to value "c" after nn secondsText Prompt string to display

DOS variable "ERRORLEVEL" is set to the numeric position of the character in the choices string which is equal to the character entered by user.

Page 27: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

A Common User Access Menu interface

Demonstration of DOSshellDownload from http://waserv.netcomp.monash.edu.au/~fit1001/software/DOSshell/DOSSHELL.zip

Page 28: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 28

Common Interfaces for Application systems

• The “Look and Feel” of an interface should be consistent in all parts of the computer application

• One of the biggest computer hardware and software vendors IBM produced an interface definition as part of a larger system application architecture

• Known as the Common User Access (CUA) interface• This was widely used for software development

Page 29: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 29

Common User Access standard

• CUA specifies things like the operation of dialog boxes, menus and keyboard shortcuts

• CUA was so successful that it is used in many GUIs !The CUA includes features such as:• A menu bar across the top of the window;• All operations done with either the mouse or the

keyboard;• Menus opened using Alt key plus the underlined

letter of the menu name• Options are requested using dialog or text boxes;• Navigate within fields in dialog boxes by cursor key;

navigate between fields by pressing Tab, or Shift-Tab to go backwards;

Page 30: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 30

Common User Access standard

• Dialog boxes have a Cancel button, activated by pressing Esc, and an OK button, activated by Enter

• The program should have online help,with Help menu as the last option on the menu bar

• The first menu should be File and contain operations for handling files, quitting the program and so on.The next is Edit and contains cut, copy, paste commands. The next is View.

• The Cut command is Shift-Del, Copy is Ctrl-Ins,Paste is Shift-Ins

• The window size changed by dragging the frame border using the mouse

Page 31: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 31

CUA MENU interface: Microsoft DOSshellAction Menu

(Alt key or TAB)

Disk Drive Selection

File DetailsDirectory Tree

Note the File names are shown in 8.3 format

MSDOS_Ch2.pdf appears as

MSDOS_~1.PDF

Page 32: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 32

CUA MENU interface: Microsoft DOSshell

Use TAB key or F10 to navigate to Action Menu

Use ENTER or SpaceBar to open Drop-Down Menu

Use Down-Arrow keyor choose by Letter

(I, K, S, C,…)

Page 33: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 33

CUA MENU interface: Microsoft DOSshell

Press ENTER or SpaceBar to perform selected menu choice.

In this case type: F10 H U

Menu Interfaces are still present in modern GUI systems as “Accelerator Keys”. Sometime skilled users find this to be faster than using a mouse

Page 34: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Graphical User Interfaces

Graphical User Interfaces (GUIs)

… are now the most common way of using general purpose computers …

You have probably been using GUIs already Intuitive, language neutral, concurrent interfaces …

Page 35: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 35

Graphical User Interfaces• GUIs originated in research projects during the 1960’s

(see Sketchpad and SAGE)• The WIMP (Windows, Icons, Menus, Pointers) interface

paradigm was developed at XEROX Palo Alto Research Centre (PARC) in 1970s

• X Window System (X11) developed in mid-1980s (Project Athena at MIT) and is now the common windowing system for Unix

• The Apple Macintosh (1984) was the first commercially successful computer GUI

• Microsoft Windows first appeared in 1985 but did not really dominate until version 3 (1990)

Page 36: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 36

A GUI example: MS Windows XPMENU bar showing CUA choices

(File, Edit, View…. Help)

Multiple Window Panes

Slider Bar

Drop Down Menu Listbox Clickable Icons

Textbox for Command Options

Buttons trigger command execution

Page 37: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 37

GUIs are Object-Oriented and Event-Driven

• A Command environment naturally executes only one command line at a time (ie. is “synchronous”)

• A GUI environment has many simultaneous inputs and the sequence or inputs is unknown (ie the interface is “asynchronous”)

• This means that a GUI control program needs to be able to accept input at any time, not just when the program is ready to receive it!

• This can be implemented using the concurrency features of an operating system (ie threads) or using an event-driven approach.

Page 38: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 38

GUIs are Object-Oriented and Event-Driven• Many GUI programming environments are structured

so that input arriving from various devices (mouse, buttons, keyboard, etc…) is treated as an event

• Information about the event is stored in a Queue. Events types are identified by a unique number.

• The GUI control program is continuously scanning the event Queue, and when an event has occurred, it uses the properties of that event to identify the appropriate handler routine.

• Applications that need to process certain events, may register their services with the GUI control program and provide a “call-back” address (eg. the address of a

subroutine) for the control program to call (Dispatch)

Page 39: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 39

GUIs are Object-Oriented and Event-Driven

Events occur at any time

Events are processed serially

Application programs register as handlers for specific event types, passing a “call-back” address to the Dispatcher

Page 40: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 40

User Interface Resources• Command Line Interface from Wikipedia

http://en.wikipedia.org/wiki/Command_line_interface“A command line interface or CLI is a way to interact with a computer via a terminal…”

• Graphical User Interface from Wikipediahttp://en.wikipedia.org/wiki/GUI“A graphical user interface allows of interaction with a computer through manipulation of graphical images and widgets in addition to text…”

• History of the Graphical User Interface from Wikipediahttp://en.wikipedia.org/wiki/History_of_the_graphical_user_interface

• A History of the GUI by Jeremy Reimer http://arstechnica.com/articles/paedia/gui.ars “… Why did computers come to adopt the GUI as their primary mode of interaction, and how did the GUI evolve to be the way it is today?”

• The Graphical User Interface – An Introductionby B.J.Jansen, 1998.. SIGCHI Bulletin. 30(2)http://jimjansen.tripod.com/academic/pubs/chi.html

• Creating a GUI with JFC/Swing - Java Swing Tutorial http://java.sun.com/docs/books/tutorial/uiswing/index.html

Page 41: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Time for a Break

Page 42: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

www.monash.edu.au

Window and UNIX shell

Programming Basics

Page 43: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 43

Batch / Script = CLI commands in a file

• Entering the same sequence of CLI commands repeatedly is tiresome and likely to cause errors!

• Most CLIs and Shells will allow input redirection• So commands may be read from a file instead of

from the keyboard…• Such files are called Batch, Command or Script files

• Lets look at some simple examples of Script files and how they can be used …These examples are based on DOS/Windows BATCH files but the same ideas apply (with slightly different syntax) to UNIX/Linux script files

Page 44: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 44

Parameter Substitution

• The commands in a batch file often need parameters• But the actual value of these parameters will only be

known when the Batch file is executedNOT when the file is first created !!

• So when the file is being written, we use replaceable (or dummy) parameters

• The CLI will substitute the actual command parameters for these dummy parameters each time the script or Batch file is executed

• Dummy parameters are those with “%” signs (or which start with “$” in UNIX/Linux)

• There are 10 command parameters (%0 through %9)

But the SHIFT command can be used to process more than 10

Page 45: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 45

Parameter Substitution: Command Line Parameters

• For Example

rem SORTER.BATrem will display a sorted list of lines containing a stringtype %2 | find “%1” > string.txttype string.txt | sort | moredel string.txt

C:\SG11> SORTER rem sorter.bat

rem SORTER.BATrem will display a sorted list of lines containing a stringtype sorter.bat | find "rem" 1>string.txttype string.txt | sort | more rem SORTER.BAT rem will display a sorted list of lines containing a stringdel string.txt

C:\SG11> _

Output from the more command

Commands with dummy parameters replaced by

command line parameters

Dummy parameters

Page 46: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 46

Parameter Substitution: Environmental parameters

Instead of just deleting files, lets write a batch file that saves them to a Trash directory first…This uses an environment variable “deldir” as the name of the Trash directoryBefore running SDEL.BAT you must set an environmental variable called “deldir” to be the Trash directorySET deldir=C:\Trash

REM SDEL.BAT safe deleteCOPY %1 %deldir%DEL %1DIR /w %deldir%ECHO All done…

Note: Environment variables need two “%” signs, but command parameters only need one

Page 47: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 47

ITERATION: repeated operations on sets of files

• One very common situation is where the same command must be executed on a set of files

• The FOR command can be used to do this

FOR %%c in ( list… ) DO command

will execute “command” as many times as there are items in “list…”; each time the variable %c will contain the current item from the list…

Eg. FOR %%f IN ( report memo address ) DO DEL %%f

will repeat the DEL command 3 times, and %f will contain each filename from the list, in sequence.

Page 48: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 48

ITERATION: repeated operations on sets of files

• The FOR command is especially useful when combined with “Wildcard” filename templates

• The CLI will replace “*” and “?” in the command line with the names of matching files from the current directory. UNIX/Linux has Regular Expression syntax that is more sophisticated that these two wildcards…

• In DOS“*” matches any sequence of characters except “.”

“?” matches any single character

• This is known as Wildcard or filename substitution.

Eg. FOR %%f IN ( *.tmp ) DO DEL %%f

will delete all files whose names end in “.tmp” from

Page 49: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 49

Conditional Execution: making decisions…

• Often a batch file needs to either execute a command, or not, depending on some parameter

• In DOS/Windows the IF command does this• The conditions tested by the IF command are:

– exit status code values returned by previous command– if text strings or parameters are equal– if a file exists

• The exit status code can be used to test if a previous programmed action was successful or not, and take appropriate action FORMAT A: /S

IF ERRORLEVEL 0 GOTO endECHO An error occurred during formatting….PAUSE:end

Page 50: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 50

Conditional Execution: making decisions…

• The following batch program fragment could be used to check a parameter before processing it

IF NOT “%1” == “” GOTO ParamOKECHO Error: First parameter missing ??GOTO Finish:ParamOKIF EXIST %1 GOTO FileOKECHO Error: File “%1” does not exist!GOTO Finish:FileOK… (do processing using %1)…:Finish

Check that the first parameter is not blank

Check that the first parameter is the name of

an existing file

Uses “GOTO” command to jump to the exit

Page 51: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 51

DOS/Windows BATCH commands

Command What it does…

CALL Calls another batch file and returns when the called procedure exits

ECHO Displays a message, Also controls the display of commands while the procedure is running

FOR Performs a command as many times as there are items in a list. The items may be filenames. The current item is available as a parameter

GOTO Resumes execution of commands after the label

IF Performs a command if a condition is true

PAUSE Waits for the user to press Enter before proceeding

REM Used to display comments in a batch script

SHIFT Discards %0 and moves all parameters down by one

Use HELP for more…

Page 52: Www.monash.edu.au FIT1001- Computer Systems Lecture 11 User Interfaces.

SG11: FIT1001 Computer Systems S1 2006 52

Next Week

• Study Guide 12– System Software