Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

32
Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill

description

The Script Is a file, often ending in.sh It is actually a programming language so may contain the usual things System commands like cp, mv, chdir Comments – usually starts with a # Variables, assignments, expressions Flow of control – if, while, etc May accept command-line parameters Copyright © by Curt Hill

Transcript of Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Page 1: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Batch Files

Weaker form of UNIX shell scripts

Copyright © 2003-2016 by Curt Hill

Page 2: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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 scriptCopyright © 2003-2016 by Curt Hill

Page 3: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

The Script• Is a file, often ending in .sh• It is actually a programming language

so may contain the usual things• System commands like cp, mv, chdir• Comments – usually starts with a #• Variables, assignments, expressions• Flow of control – if, while, etc• May accept command-line parameters

Copyright © 2003-2016 by Curt Hill

Page 4: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Uses• Collect a series of commands that

are frequently used together• Implement simple programs • Prototypes to show proof of concept• Favorite of system administrators

who need a variety of utility programs that infrequently run or of short duration

• Consider a BASH example on next screen

Copyright © 2003-2016 by Curt Hill

Page 5: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Copyright © 2003-2016 by Curt Hill

if [ $# -ne 3 ] then echo "$0: three numbers are not given" >&2 exit 1 fi n1=$1 n2=$2 n3=$3 if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ] then echo "$n1 is Bigest number" elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ] then echo "$n2 is Bigest number" elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ] then echo "$n3 is Bigest number" elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ] then echo "All the three numbers are equal" else echo "I can not figure out which number is biger" fi

Consider:

Page 6: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Another Example Fragment

Copyright © 2003-2016 by Curt Hill

#!/bin/bash./generate_coverage_reportrm -rf devinfomkdir devinfomv coverage_report devinfo/doxygen 2> devinfo/doxygen-errors.txtmv doxyoutput/html devinfo/doxyoutputcd addonsdoxygen cppcheckdata.doxyfilemv html ../devinfo/cppcheckdatacd ..…

Page 7: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Notice• For a system that is dominated by

C this language is definitely not • No semicolons• One statement per line• No declaration• fi is the end of an if

– elif is an else if without need of an additional fi

• $ indicates a parameterCopyright © 2003-2016 by Curt Hill

Page 8: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Scripts Again• This is a real programming language

– There is even a character that forces two (or more) programs to execute in parallel

• However, like most scripts:– No compilation, must be interpreted– Slower execution

• Unlike most scripts:– All system commands are available

• Dependent on shell– Tends to not be very portable

Copyright © 2003-2016 by Curt Hill

Page 9: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

UNIX Shells• Like any important UNIX 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-2016 by Curt Hill

Page 10: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 11: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 12: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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 scriptsCopyright © 2003-2016 by Curt Hill

Page 13: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Beware• The batch file facility starts in DOS

1• Many changes have occurred since

then– This presentation may be up to date

or not

Copyright © 2003-2016 by Curt Hill

Page 14: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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 \abcfilesabccd \original

• Create a batch file that is on the path and it would do the three as one

Copyright © 2003-2016 by Curt Hill

Page 15: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 16: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 17: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 18: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 19: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

More on Echo• Echo Off example

ECHO OFFCOPY *.* 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-2016 by Curt Hill

Page 20: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 21: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Variables• What constitutes variables in

this programming language?• Parameters and environment

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

Copyright © 2003-2016 by Curt Hill

Page 22: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 23: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 24: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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 directorycopy %2 %temp%p2.dat

• Notice how the % is used • Environment variables are

frequently used to construct specifications

Copyright © 2003-2016 by Curt Hill

Page 25: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Environment variables again

• The batch command may both set and examine environment variables

• Some environment variables are read only– Should not be set by the batch file

• We may also have local environment variables

Copyright © 2003-2016 by Curt Hill

Page 26: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Assignment • The is handled by the SET command• The set has the form:

SET var=expr• The var does not need the leading

and trailing %• The blanks are significant to the

right of =• The expression may include

operators

Copyright © 2003-2016 by Curt Hill

Page 27: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Values• If a variable does not exist it is

expanded as nothing• It may be undefined by having

nothing to right of =

Copyright © 2003-2016 by Curt Hill

Page 28: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Locals• The SETLOCAL command creates a

local scope block for environment variables

• It is terminated by the ENDLOCAL command

• Anything created between the two is local

• Like programming language scope, if not found in current it looks at next larger

• SETLOCAL/ENDLOCAL may be nestedCopyright © 2003-2016 by Curt Hill

Page 29: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Example

Copyright © 2003-2016 by Curt Hill

echo offecho A is %a%setlocalset a=This thingieecho A is %a%echo Temp is %temp%:enditendlocalecho %a%Echo All done

Page 30: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Example Results

Copyright © 2003-2016 by Curt Hill

Page 31: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

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-2016 by Curt Hill

Page 32: Batch Files Weaker form of UNIX shell scripts Copyright © 2003-2016 by Curt Hill.

Not Finally• There is more to know about the

batch language• This will be covered in later

presentation– Flow of control– Other things

Copyright © 2003-2016 by Curt Hill