Batch Files Flow of Control to Strengthen Copyright © 2003-2016 by Curt Hill.

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
Programming Batch Files Aim: To introduce the concept of Batch processing and programming techniques. Lesson Outcomes  The need for Batch Processing.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
More Command Line Options Pipes, Redirection, Standard files Copyright © 2015 Curt Hill.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Command Prompt Chapter 10 Introduction to Batch Files Richard Goldman February 7, 2000.
The Command Line Mostly MS Dos with some UNIX/LINUX Copyright © Curt Hill.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth.
Controlling Execution Dong Shao, Nanjing Unviersity.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Applications Development
Keywords and Functions That is Different Copyright © by Curt Hill.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Copyright © 2015 Curt Hill Make An Indispensable Developer’s Tool.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
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.
Batch Files Weaker form of UNIX shell scripts Copyright © by Curt Hill.
Controlling Program Flow with Decision Structures.
Linux+ Guide to Linux Certification, Second Edition
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Copyright © Curt Hill Flow of Control A Quick Overview.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Batch Files More flow of control Copyright © by Curt Hill.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Linux Administration Working with the BASH Shell.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Mostly MS Dos with some UNIX/LINUX
User-Written Functions
PowerShell Introduction Copyright © 2016 – Curt Hill.
Accomplishing Executables
Control Structures – Selection
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
CSC215 Lecture Flow Control.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
The Java switch Statement
Bash Scripting CS 580U - Fall 2018.
CSC215 Lecture Control Flow.
Presentation transcript:

Batch Files Flow of Control to Strengthen Copyright © by Curt Hill

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

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

Exit statement The exit statement exits the DOS Box The syntax is: EXIT [/B] returncode The returncode is a numeric that is returned to the OS The /B is optional –Leaves the batch command but not the DOS Box –If the batch command was called by another it only leaves the current batch command/file Copyright © by Curt Hill

IF statement Batch files have a simplified if –Reminiscent of BASIC, which itself is watered down FORTRAN Example: Use an environment variable as a batch password –if not %1 == %pwd% exit /B 2 Format: IF condition statement We now ask what the condition and statement must be Copyright © by Curt Hill

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

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

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

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:” and not a syntax error that would end the batch Copyright © by Curt Hill

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

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

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

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

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

One more thing There is also an environment errorlevel variable You may test it directly: if %errorlevel% == 5 … You are not allowed to set it Copyright © by Curt Hill

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

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

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

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

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

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

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

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

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

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

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

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