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.

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

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
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.
Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Control Structures: if Conditional
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
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Essential Shell Programming
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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 on a condition, you change the flow of the script. The commands are called flow control commands.

Types if statement case statement

if list1 then list2 elif list3 then list4 else list5 fi if list1 ; then list2 ; elif list3 ; then list4 ; else list5 ; fi ;

if commandt then command command... fi

Exit Status Whenever any program completes execution under the Unix system, it returns an exit status back to the system. This status is a number that usually indicates whether the program successfully ran. By convention, an exit status of zero indicates that a program succeeded, and nonzero indicates that it failed. In a pipeline, the exit status is that of the last command in the pipe. So in who | grep fred

The $? Variable $ cp phonebook phone2 $ echo $? 0 Copy "succeeded

Test command test expression String Operators test "$name" = julio The = operator is used to test whether two values are identical. –It's good programming practice to enclose shell variables that are arguments to test inside a pair of double quotes (to allow variable substitution). $ name= $ test $name = julio sh: test: argument expected $ $ test "$name" = julio

String operators string1 = string2 string1 is identical to string2. string1 != string2 string1 is not identical to string2. string string is not null. -n string string is not null (and string must be seen by test). -z string string is null (and string must be seen by test).

example $ day="monday " $ test $day = monday $ echo $?

You can test to see whether a shell variable has a null value with the third operator test "$day" This returns true if day is not null and false if it is.

-n and –z $ nullvar= $ nonnullvar=abc $ test -n "$nullvar“ Does nullvar have nonzero length? $ echo $? 1 No $ test -n "$nonnullvar“ And what about nonnullvar? $ echo $? 0 Yes $ test -z "$nullvar" Does nullvar have zero length? $ echo $? 0 Yes $ test -z "$nonnullvar" And nonnullvar? $ echo $? 1 No $

Special test X"$symbol" = X The X in front of symbol prevents test from interpreting the characters stored in symbol as an operator.

An Alternative Format for test [ expression ] $ [ -z "$nonnullvar" ] $ echo $? 1 $

Continue… if [ "$name" = julio ] then echo "Would you like to play a game?" fi

Integer Operators int1 -eq int2 int1 is equal to int2. int1 -ge int2 int1 is greater than or equal to int2. int1 -gt int2 int1 is greater than int2. int1 -le int2 int1 is less than or equal to int2. int1 -lt int2 int1 is less than int2. int1 -ne int2 int1 is not equal to int2.

Comparison between string and integer operators $ x1="005" $ x2=" 10" $ [ "$x1" = 5 ] String comparison $ echo $? 1 False $ [ "$x1" -eq 5 ] Integer comparison $ echo $? 0 True $ [ "$x2" = 10 ] String comparison $ echo $? 1 False $ [ "$x2" -eq 10 ] Integer comparison $ echo $? 0 True $

File operators -d file file is a directory. -e file file exists. -f file file is an ordinary file. -r file file is readable by the process. -s file file has nonzero length. -w file file is writable by the process. -x file file is executable. -L file file is a symbolic link.

[ -f /users/steve/phonebook ]

The Logical Negation Operator ! The Logical AND Operator –a Parentheses –[ \( "$count" -ge 0 \) -a \( "$count" -lt 10 \) ] The Logical OR Operator –o The -o operator has lower precedence than the - a operator, meaning that the expression "$a" -eq 0 -o ("$b" -eq 2 -a "$c" -eq 10)

If…else if commandt then command command... else command command... fi

Terminate program Exit 1

elif if commandt then command command... Elif then command command... else command command... fi

Case case word in pattern1) list1 ;; pattern2) list2 ;; esac

FRUIT=kiwi case "$FRUIT" in apple) echo "Apple pie is quite tasty." ;; banana) echo "I like banana nut bread." ;; kiwi) echo "New Zealand is famous for kiwi." ;; esac

if [ "$#" -ne 1 ] then echo "Usage: number digit" exit 1 fi case "$1" in 0) echo zero;; 1) echo one;; 2) echo two;; 3) echo three;; 4) echo four;; 5) echo five;; 6) echo six;; 7) echo seven;; 8) echo eight;; 9) echo nine;; *) echo Bad argument;; esac

case "$char" in [0-9] ) echo digit;; [a-z] ) echo lowercase letter;; [A-Z] ) echo uppercase letter;; ? ) echo special character;; * ) echo Please type a single character;; esac

Null command : 1.: 2.The && and || Constructs