Agenda Control Flow Statements Purpose test statement

Slides:



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

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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 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.
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.
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 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.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
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]
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.
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"
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
EGR 2261 Unit 4 Control Structures I: Selection
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Scripts & Functions Scripts and functions are contained in .m-files
Lecture 4B More Repetition Richard Gesick
CHAPTER 8 (skip , ) CHAPTER 10
Arrays, For loop While loop Do while loop
Additional Control Structures
The Linux Command Line Chapter 27
Iteration: Beyond the Basic PERFORM
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
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CST8177 Scripting 2: What?.
Essential Shell Programming
Using C++ Arithmetic Operators and Control Structures
Introduction to Bash Programming, part 3
Loops CGS3416 Spring 2019 Lecture 7.
Review The Unix Shells Graham Glass and King Ables,
The Linux Command Line Chapter 27
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 Statements Control-Flow Statements (commands) alter the course of executions within a script. This lesson will look at the following control-flow commands test / if / else / elif / case (logic statements) until / while / for (loops) break / continue / exit (termination / skipping) We will also look at exit status (a condition code)

test Many logic statements perform a test to see if the condition is true or false and take appropriate action based on that outcome. 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 most recent test. See example below: set one two three test $# = 0 echo $? 1 Three arguments are stored as positional parameters. The test statement is testing the condition that there are zero arguments. The $? Variable (exit status) is displayed as non-zero (1) thus the condition is false.

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 condition or if [ condition ] then then action action fi fi 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/spr720/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!

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!

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

Notice space between mathematical operator! 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=$(($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

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.

Output of this script would be: 1 2 3 break statement Example: for num in 1 2 3 4 5 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 $?