PowerShell Flow of Control Copyright © 2016 – Curt Hill.

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Flow of Control MINS298c Fall 1998 Chapter 9. Overview ABAP Programming Structures for: –Iteration –Decisions Control Flow –If … Then –Do & While loops.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Copyright © Curt Hill Flow of Control A Quick Overview.
Introduction to SQL Server Automation with Powershell by Chris Sommer.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
The for Statement A most versatile loop
Chapter 10 Programming Fundamentals with JavaScript
Flow of Control An Overview
PowerShell Introduction Copyright © 2016 – Curt Hill.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Lecturer : Dr. Pavle Mogin
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Arrays An array in PHP is an ordered map
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
JavaScript Syntax and Semantics
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Scripts & Functions Scripts and functions are contained in .m-files
Programming Fundamentals
Expressions and Control Flow in JavaScript
Chapter 8: Control Structures
Agenda Control Flow Statements Purpose test statement
Chapter 10 Programming Fundamentals with JavaScript
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: if Conditional
Concepts From Alice Switching to Java Copyright © Curt Hill.
Control Structures: for & while Loops
PHP.
Coding Concepts (Basics)
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6 Control Statements: Part 2
Three Special Structures – Case, Do While, and Do Until
Cmdlets “Command-lets”
Statement-Level Control Structures
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Python Primer 1: Types and Operators
The Java switch Statement
Chapter 5 Decisions.
Decisions, decisions, decisions
Introduction to Programming
Selection Statements Chapter 3.
PHP an introduction.
The Selection Structure
CSC215 Lecture Control Flow.
DATA TYPES AND OPERATIONS
Presentation transcript:

PowerShell Flow of Control Copyright © 2016 – Curt Hill

Introduction We have seen some of the basics of scripting now onto flow of control Everyone in this class knows C++ and one or more additional programming languages Covering flow of control should be comparatively easy The syntax may be different than what you have seen previously but the concepts should be easy to understand Copyright © 2016 – Curt Hill

Blocks PowerShell uses the braces { } in a similar way as the C family of languages They create a block of statements that is considered as a unit for inclusion in a decision or looping statement In the console entering a { keeps the item from being executed until the terminal brace is entered The scope rules are different from C++ and more similar to C Will be discussed later Copyright © 2016 – Curt Hill

Conditions Flow of control statements typically need some type of expression that generates a true/false value to drive them This is a problem for the traditional comparisons < and > are special characters for PowerShell Therefore PowerShell uses a FORTRANish set of operators Copyright © 2016 – Curt Hill

Comparison Operators Use a dash followed by two letters to indicate the operator == is –eq < is –lt <= is –le != is –ne > is –gt >= is –ge Copyright © 2016 – Curt Hill

Logical Operators We also have Boolean operators: Not is –not And is –and Or is –or Exclusive or is –xor Copyright © 2016 – Curt Hill

Other Operators There are others that are neither comparison nor Boolean operators Yet still handy -like and –notlike Uses wildcards -match and –notmatch Uses regular expressions -in and –notin -contains and –notcontains -is and –isnot Copyright © 2016 – Curt Hill

Case Sensitivity These operators are not case sensitive As are most MicroSoft comparisons If case sensitivity is desired prefix the operator with a c Thus: $item –eq “stuff” is case insensitive $item –ceq “Stuff” is case sensitive Copyright © 2016 – Curt Hill

Example Copyright © 2016 – Curt Hill

Decisions The language contains several flow statements If Switch For While Do foreach Copyright © 2016 – Curt Hill

If Other than the condition the simple forms look like C if($t –eq $other) { … } else { … } Do I need to say more? Copyright © 2016 – Curt Hill

Yes, I do PowerShell requires the braces after the then or else Makes parsing easier PowerShell also has an elseif Often used to reduce braces for nested if then elses Copyright © 2016 – Curt Hill

Examples Copyright © 2016 – Curt Hill

Switch Allows certain types of nested if then elses to be collapsed Different than that of C in that it is more clearly a modified if No case keyword instead use a conditional expression that references $_ which is the path selector Consider the following example Copyright © 2016 – Curt Hill

Switch example Clear enough? Copyright © 2016 – Curt Hill

For loops Very similar to that of C with the exception of variable names and conditionals See the next slide Copyright © 2016 – Curt Hill

For example Copyright © 2016 – Curt Hill

Whiles Similarly the while and do while look like those of C Copyright © 2016 – Curt Hill

While Example Copyright © 2016 – Curt Hill

Do While Example Copyright © 2016 – Curt Hill

More There is also a Pascalish Do Until Same as do while except condition is reversed Do while quits when condition is false Do until quits when condition is true Copyright © 2016 – Curt Hill

Foreach The foreach is an iterator through a collection The most interesting collections are returned by other objects Such as files in a directory You may create a collection with a comma separated list or a subrange Then the in operator may be used Copyright © 2016 – Curt Hill

Collections A subrange is two numeric values separated by a two dot ellipsis, such as: 4..9 If these are non-integer they will be truncated to an integer A comma separated list may be of any type Copyright © 2016 – Curt Hill

Example Copyright © 2016 – Curt Hill

Finally There are a few more things that we have to wait for a subsequent presentation Copyright © 2016 – Curt Hill