Presentation is loading. Please wait.

Presentation is loading. Please wait.

Batch Files Weaker form of UNIX shell scripts. Introduction UNIX may use one of several shells The shell is the command interpreter It interacts with.

Similar presentations


Presentation on theme: "Batch Files Weaker form of UNIX shell scripts. Introduction UNIX may use one of several shells The shell is the command interpreter It interacts with."— Presentation transcript:

1 Batch Files Weaker form of UNIX shell scripts

2 Introduction UNIX may use one of several shells The shell is the command interpreter It interacts with the user by accepting commands and displaying the results of the program Very early on they figured out that if the input was not from a person, but from a file, this greatly strengthened what could be done Thus was born the shell script Copyright © 2003-2015 by Curt Hill

3 UNIX Shells Like any important program, shells were improved by various programmers We now have: –Bourne shell (sh) –C shell (csh) –BASH (from GNU) –Korn Shell –Among others This has not been the case in the Windows world Copyright © 2003-2015 by Curt Hill

4 What are batch files? Nearest equivalent to the shell script –Usually weaker than shell scripts A batch files is a text file with an extension of.BAT The contents of the batch file are DOS commands and special batch commands The commands are executed immediately when the batch file name is typed Copyright © 2003-2015 by Curt Hill

5 More The batch files allow programming style commands Hence the batch file is like a program where we can: –Have variables and parameters –Execute DOS commands, internal or external –Have conditional statements, like an IF –Execute loops Copyright © 2003-2015 by Curt Hill

6 Why? In its simplest case it is usually a replacement for a common sequence of commands Often we want something slightly more complicated Sometimes, but rarely, we can prototype a full program with batch files –This is much more common for shell scripts –Batch files are somewhat weaker than shell scripts Copyright © 2003-2015 by Curt Hill

7 Example Program abc is in directory \abcfiles –Directory is not on the path There are files in directory that we need to use when we run the program The way we would execute this could be: cd \abcfiles abc cd \original Create a batch file that is on the path and it would do the three as one Copyright © 2003-2015 by Curt Hill

8 Batch contents What can be in a batch file? External DOS commands –The only thing to worry about is whether these are on the path –You may always specify a specific directory: C:\dos\checkdsk a: –Commands can return a numeric value that specifies how well they worked, this can be interrogated, which we will see later Internal DOS commands can be used without any serious problems Copyright © 2003-2015 by Curt Hill

9 Batch Commands There are a number of commands that are for the batch interpreter only These include: –Comments –Output to the console –Variable usage Copyright © 2003-2015 by Curt Hill

10 Comments Since batch files are a programming language, they need to have a comment Rem –Short for Remark –Same comment as BASIC Everything else on the line is ignored All real batch commands should include the author’s name and purpose Copyright © 2003-2015 by Curt Hill

11 Echo Purpose: to display a message or to enable or disable the echoing of the commands on the screen Three parameter possibilities –ON –OFF –Message ON tells DOS to echo commands to the console OFF tells DOS to suppress output Copyright © 2003-2015 by Curt Hill

12 More on Echo Echo Off example ECHO OFF COPY *.* A: The console will not see the copy command, but will see the list of files copied –Use COPY *.* A: >nul: –nul: is a device name like lpt1: or prn: Use the message option when you want to display a message in a batch file when echo off is in effect Copyright © 2003-2015 by Curt Hill

13 Pause Allows the batch file to pause, usually to allow the user to read a message Takes no parameters It waits until they enter a key Also used to section a batch file for debugging –Place several pauses in various places –If you want to stop hit ^C to terminate and then answer the prompt Copyright © 2003-2015 by Curt Hill

14 Variables What constitutes variables in this programming language? Parameters, environment variables and local variables A parameter is a string that follows the batch name on command line –Doesn’t matter if the command is from the prompt or another batch file A local variable is a letter preceded by % Copyright © 2003-2015 by Curt Hill

15 Parameters The parameters are not named but numbered, since there is no prototype like in Pascal or C A parameter is %n where n is a digit between 0 and 9 %0 is the first word of the command line –This may include a path –Includes the name of the batch file %1 is the first parameter A parameter may be a null string Copyright © 2003-2015 by Curt Hill

16 Environment variables An environment variable is a name and value stored in OS memory Environment variables are used to pass information to programs We saw how these were set Any program that is run can ask for the contents of a particular variable PATH is the environment variable of where to search for a program COMSPEC is the environment variable that tells where the shell is Copyright © 2003-2015 by Curt Hill

17 Environment Variables We can use environment variables in a batch file as variables We use the name enclosed in % to make them appear in a statement Example: copying the second parameter to the temp directory copy %2 %temp%p2.dat Notice how the % is used Environment variables are frequently used to construct specifications Copyright © 2003-2015 by Curt Hill

18 Flow of Control Like any good scripting language we must have flow of control Batch files have –Subroutines –An if statement –Evil GOTO –A for loop –A choice statement Copyright © 2003-2015 by Curt Hill

19 Subroutines One batch program can execute another by using Call Format: CALL batname parms... –The first will be suspended and the new one started –When the second is done, the first is resumed Notice that CALL is not needed to execute a.exe Copyright © 2003-2015 by Curt Hill

20 IF statement Batch files have a simplified if –Reminiscent of BASIC, which itself is watered down FORTRAN Example: A format.bat that protects the hard disk –if %1 == c: echo Hey dope, dont do it Format: IF condition statement We now ask what the condition and statement must be Copyright © 2003-2015 by Curt Hill

21 What is a statement? Any DOS command Any Batch command Just one –This FORTRAN IV and before or original BASIC –Disgusting Copyright © 2003-2015 by Curt Hill

22 What is a condition? One of three constructions S1==S2 ERRORLEVEL EXIST filespec We may also apply NOT to any of these Copyright © 2003-2015 by Curt Hill

23 Equality condition This condition is usually used to compare a parameter against some constant value, but can be used for other things as well Uses double = (like C), not a single = There must be something on the right and left Copyright © 2003-2015 by Curt Hill

24 Missing Parameters A missing parameter expands to null, so we usually use additional characters: IF “%1” == “C:” These are not required by syntax but are helpful Then if there is no parameters this will expand to: IF “” == “C:” Copyright © 2003-2015 by Curt Hill

25 The Missing Parameter If we just used: IF %1 == C: And there was no first parameter, it expands to: IF == C: This is a syntax error –Batch files are processed when they are executed, so the syntax error message will come out to the user of the batch file –No compilation, no error checking Copyright © 2003-2015 by Curt Hill

26 NOT We can prefix the condition with NOT and end up with the logical negative IF NOT “%1” == “a:” ECHO Dope! There are no comparisons Recall to compare environment variables, use %: IF %A_VAR% == %1 Copyright © 2003-2015 by Curt Hill

27 Return Codes Every program or command in DOS/Windows has a return code This is the int of C the main function A numeric value that describe what happened Internal and external Not all not all actually use it –The return code may not mean anything By convention 0 means everything is fine Copyright © 2003-2015 by Curt Hill

28 More The DOS prompt does not show us the return code A batch file may check the value and then do something depending on that value ERRORLEVEL is used to look at last return code only –Ony the return code of the last program run Copyright © 2003-2015 by Curt Hill

29 ERRORLEVEL Format is ERRORLEVEL testno –This returns true if the returned number is greater than or equal to testno ERRORLEVEL 0 will be true for zero or positive return codes In practice we test the return codes in a descending order fashion –Large ones first NOT may also precede the condition Copyright © 2003-2015 by Curt Hill

30 EXIST condition This is used to determine if a file exists The item following the exist is a file specification The condition is true if the given file does exist NOT may precede condition Copyright © 2003-2015 by Curt Hill

31 GOTO One statement is not enough In C/C++ we have a compound statement –Here we do not –Use the infamous GOTO statement Nobody in their right mind has designed a language that depends on the GOTO since the early 70s –Curse you Bill Gates! Copyright © 2003-2015 by Curt Hill

32 GOTO Again The format is: GOTO label Where label is label in the batch program A label in a batch program starts with a colon (:) and is alone on the line, but in the GOTO you do not use the colon Somewhat similar to MS assembly Copyright © 2003-2015 by Curt Hill

33 GOTO Example Example using FORMAT to interpret error codes: FORMAT A: IF ERRORLEVEL 5 GOTO USERNO IF ERRORLEVEL 4 GOTO FATALERR IF ERRORLEVEL 3 GOTO CTRLC ECHO COMPLETED SUCCESSFULLY GOTO END :USERNO ECHO YOU RESPONDED NO GOTO END :FATALERR ECHOR A FATAL ERROR OCCURRED... :END Copyright © 2003-2015 by Curt Hill

34 Looping Goto can be used to make loops or an if then else construction –This is exactly how it is done in assembly If we conditional branch to later in the file it is usually a conditional If we conditional branch earlier then it is making a loop There is one easier to make loop Copyright © 2003-2015 by Curt Hill

35 For Loops The FOR allows us to run the same command many times The many times is once each for each item in a group or set This is a much more sophisticated loop –Likely added later Copyright © 2003-2015 by Curt Hill

36 The FOR The general format of the for is: FOR var IN set DO cmd where: –FOR, IN and DO are just keywords –var is a variable such as %c –From the command prompt it is %c –set is a parenthesized list of things, usually files Copyright © 2003-2015 by Curt Hill

37 Sets Usually a file specification with wildcards –(*.*) is the set of all files in this directory May have multiple things separated by blanks –(abc.dat xyz.doc stuff.jun) We may have a combination of files with and without wildcards –(abc.* dat??.sss $*.* last.one) Copyright © 2003-2015 by Curt Hill

38 More on For The control variable (the %x or %x) will become each file in this list before being substituted into the command The cmd is a single command usually with replaceable parameter It may be a executable program or bat file For each item in the set we get one execution of the command Copyright © 2003-2015 by Curt Hill

39 For Continued The replaceable parameter should make each execution of the command different Parameters and file specifications may be constant or replaceable parameters Type command does not allow wildcards Execute a type that uses wildcards Type out all files of extension.lst: for %f in (*.lst) do type %f Copyright © 2003-2015 by Curt Hill

40 Restrictions and exceptions The things in the set do not have to be files, but that is usually what they are –Example: execute ls with two different parameters: for %p in (x d) do ls /%p –Execute ls twice: ls /x ls /d Copyright © 2003-2015 by Curt Hill

41 Redirection Redirection and piping may be used, but it does not always do what you want Redirection: for %f in (*.lst) do type %f >stuff.lis –Each separate copy of type z.lst will be redirected to stuff.lis –Thus only the last one is saved Copyright © 2003-2015 by Curt Hill

42 Non Batch The batch language is limited and primitive compared to the UNIX shell It is a common thing in UNIX to execute the shell from inside a program to access an external command The Windows Application Programming Interface (API) allows this as well –More common than lots of batch files are GUIs that execute commands –Such as IDEs Copyright © 2003-2015 by Curt Hill


Download ppt "Batch Files Weaker form of UNIX shell scripts. Introduction UNIX may use one of several shells The shell is the command interpreter It interacts with."

Similar presentations


Ads by Google