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

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
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.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
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
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Shell Scripting Introduction. Agenda What is Shell Scripting? Why use Shell Scripting? Writing and Running a Shell Script Basic Commands -ECHO - REM.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
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
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
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.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Linux Commands C151 Multi-User Operating Systems.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
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.
Linux+ Guide to Linux Certification, Second Edition
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Batch Files More flow of control Copyright © by Curt Hill.
Chapter 15 Introductory Bourne Shell Programming.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Department of Computer Engineering
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
PowerShell Introduction Copyright © 2016 – Curt Hill.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CAP652 Lecture - Shell Programming
INTRODUCTION TO UNIX: The Shell Command Interface
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Linux Shell Script Programming
Java Looking at our first console application in Eclipse
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Batch Files Weaker form of UNIX shell scripts Copyright © 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 script Copyright © 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 © 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 © 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:

Another Example Fragment Copyright © by Curt Hill #!/bin/bash./generate_coverage_report rm -rf devinfo mkdir devinfo mv coverage_report devinfo/ doxygen 2> devinfo/doxygen-errors.txt mv doxyoutput/html devinfo/doxyoutput cd addons doxygen cppcheckdata.doxyfile mv html../devinfo/cppcheckdata cd.. …

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 parameter Copyright © 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 © 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 © 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 © 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 © 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 scripts Copyright © 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 © 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 \abcfiles abc cd \original Create a batch file that is on the path and it would do the three as one Copyright © 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 © 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 © 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 © 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 © by Curt Hill

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 © 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 © 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 © 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 © 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 © 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 directory copy %2 %temp%p2.dat Notice how the % is used Environment variables are frequently used to construct specifications Copyright © 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 © 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 © 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 © 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 nested Copyright © by Curt Hill

Example Copyright © by Curt Hill echo off echo A is %a% setlocal set a=This thingie echo A is %a% echo Temp is %temp% :endit endlocal echo %a% Echo All done

Example Results 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

Not Finally There is more to know about the batch language This will be covered in later presentation –Flow of control –Other things Copyright © by Curt Hill