Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
(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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Shell Control Statements and More
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Linux+ Guide to Linux Certification, Second Edition
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Shell Control Structures
REPETITION CONTROL STRUCTURE
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Presented by, Mr. Satish Pise
Essential Shell Programming
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue statements exit statement

Control - Flow Commands Control-Flow commands alter the course of executions within a script. This lesson will look at the following control- flow commands test if / else / elif until / while / for case / break / continue / exit We will also look at exit status (a condition code)

test The test utility is used to assign a status 0 if the condition is true or (not zero) if the condition is false. The variable $? Is used to display the status of the last test. See example below: if test $# = 0 then echo “You must provide at least 1 argument” exit fi

if Used to test the status of a condition and proceed with an action of the status of the condition is true Examples: if test conditionorif [ condition ] then then action action fifi Note: fi marks end of if block. Square brackets [ ] are used as a “shortcut” for test command! Note space between condition & brackets!

Advanced Use of Test Command You can also use square brackets to represent the “test” command For example: if test -f “$filename” or if [ -f “$filename” ] if [ ! -f “$filename” ] Refer to examples of advanced test command in /home/msaul/ops224/advanced_test Tests to see if contents of filename variable is a file in current directory This tests for opposite. (not a file)...

Advanced Use of Test Command The Test command can be used with options to determine status of files: test -f file = true if "file" is a regular file test -d file = true if "file" is a directory test -r file = true if "file" is readable test -w file = true if "file" is writeable test -x file = true if "file" is executable test -s file = true if "file" is not empty test -z string = true if length of string is 0 test -n string = true if length of "string" is non-zero NOTE: test string = true if "string" is non-null

else Creates a two-way branch to perform one action if the status of the condition is true and perform another action if the status of the condition is false. See example below: if [ condition ] then action else other action fi Notice no then command after “else” statement was issued!

elif (i.e.“else if”) Creates a nested set of “if-then-else” structures. See example below: if [ condition ] then action 1 elif [ condition ] then action2 else action3 fi Notice then command appears after “elif” statement was issued!

for The for statement is used to repeat operation for known or “fixed” values Unlike C programming, the for loop usually repeats tasks for “arguments” that are either issued from script, or stated directory after for statement.

for The for command uses a loop-index to take on the value of each argument: for varname in arg1 arg2 arg3 … arg11 do echo $varname done Task will repeat until all arguments “used-up”. Variable “varname” is takes on value of each argument

for The for statement can be used to assign a value to a variable within the loop that takes on values from positional parameters that were issued as arguments from a script or set by “command substitution: for varname do echo “Your name is: $varname” done

while / until statements While and until statements are useful for “looping” until a particular condition occurs. This method allows “flexibility” if the number of tasks repeated (such as a for loop) is uncertain. The next two slides explain the difference between the while and until statements

while The while command tests and executes a series of commands as long as a condition is true. See example below: num=0 read name while [ $num -lt 7 ] do echo “number is $num” num=`expr $num + 1` done Notice space between mathematical operator!

until statement The until command is similar to the while command. The until command differs by continuing the loop until a true condition exists. See example below: password=today name=nobody until [ “$name” = “password” ] do echo “Enter Password” read name done

case The case command provides a multiple- branch decision structure based on matching or non-matching patterns. See example below on next slide.

case case $name in pattern1) commands ;; pattern2) commands ;; esac An asterisk “*” can be used as a pattern to match all other situations. Also notice two semicolons ;; instead of one!

break statement The break command is used to break the execution of the loop such as a for, while or until loop. The break command transfers control to the next line after the “done” statement. Although you can “break” a loop, it is not always considered to be a good programming practice.

break statement Example: for num in do if [ $num = 4 ] then break fi echo $num done Output of this script would be: 1 2 3

continue statement The continue command is used to return control to the done statement which essentially “skips” the execution of the commands between “do” and “done” Therefore, you can use an if statement to with a continue statement to “skip” certain comands within a for, while and until statement

exit statement The exit statement is used to “terminate” a script that is running. Although your C programming instructor may indicate that this is “poor programming practice”, it can be used in shell scripting in many different ways. if [ $# = 0 ] then echo “Redo Command!” exit 1 fi The script will stop if it was run with no arguments after the script name

exit status Note : exit command by itself represents the exit status of the last command executed in the script exit command followed by a zero represents a true exit status that can be recalled as a variable $? exit command by a non-zero number (such as 1) will terminate a script (even within logic statements) and store false exit variable in $?