bash Execution Control COMP2101 Winter 2019

Similar documents
Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell programming. Introduction to Operating Systems

Conditional Control Structures. Dr.T.Logeswari

CSC 2500: Unix Lab Fall 2016

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Bourne Shell Reference

Lab 4: Shell scripting

COMP 4/6262: Programming UNIX

Title:[ Variables Comparison Operators If Else Statements ]

A shell can be used in one of two ways:

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Vi & Shell Scripting

Control Structures. CIS 118 Intro to LINUX

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Computer Systems and Architecture

UNIX shell scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Bash scripting basics

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Bash shell programming Part II Control statements

Shell Programming (bash)

Shells & Shell Programming (Part B)

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

Linux shell programming for Raspberry Pi Users - 2

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

CS Unix Tools & Scripting

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Grep and Shell Programming

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Shell Programming (Part 2)

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Computer Systems and Architecture

Програмиранев UNIX среда

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

Manual Shell Script Linux If Not Equal String Comparison

Week 6 Lesson 1: Control-Flow Statements (Continued)

9.2 Linux Essentials Exam Objectives

Shell Start-up and Configuration Files

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Useful Unix Commands Cheat Sheet

Lecture 02 The Shell and Shell Scripting

Essential Skills for Bioinformatics: Unix/Linux

Essentials for Scientific Computing: Bash Shell Scripting Day 3

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Shell Programming (ch 10)

bash Scripting Introduction COMP2101 Winter 2019

Assignment clarifications

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Practical 04: UNIX / LINUX Shell Programming 1

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

There are some string operators that can be used in the test statement to perform string comparison.

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

RHCE BOOT CAMP. System Administration

Shells and Shell Programming

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Shells and Shell Programming

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Last Time. on the website

More Scripting Techniques Scripting Process Example Script

Practice 5 Batch & Shell Programming

Lecture 5. Essential skills for bioinformatics: Unix/Linux

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

What is Bash Shell Scripting?

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

30 Bash Script Examples

Perl. Interview Questions and Answers

CSC209H1S Day Midterm Solutions Winter 2010

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Shell Control Structures

LOG ON TO LINUX AND LOG OFF

Assignment 3, Due October 4

Bash command shell language interpreter

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

BASH Programming Introduction

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Chapter 5 The Bourne Shell

Building Powerful Workflow Automation with Cherwell and PowerShell

Linux 系统介绍 (III) 袁华

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Basic Shell Scripting

Programming Fundamentals

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Transcription:

bash Execution Control COMP2101 Winter 2019

Bash Execution Control Scripts commonly can evaluate situations and make simple decisions about actions to take Simple evaluations and actions can be accomplished using && and Complex evaluations and multi-step actions are better handled using more sophisticated execution control commands Regardless of the complexity of a test, the resulting action command(s) may need to be run more than once, in a loop We may have multiple data items on which we need to perform one or more commands, also in a loop

Basic Topics Testing data if and case while and for

Test Command The test command evaluates an expression and sets its exit status based on whether the expression evaluates as true or false The test command can also be run using the [ alias, which uses a ] to mark the end of the expression on the command line Multiple expressions can be evaluated by putting -a (and) or -o (or) between expressions test -d /etc && echo "/etc exists" [ -d /etc ] && echo "/etc exists" [ -d /etc -a -r /etc ] && echo "/etc exists and is readable"

File Testing The following are commonly used file tests, although there are more not included here -e filename : filename exists as any kind of filesystem object -f filename : filename exists and is a regular file (can hold data) -d filename : filename exists and is a directory -r filename : filename exists and is readable by whoever is doing the test -w filename : filename exists and is writable by whoever is doing the test -h filename : filename exists and is a symbolic link -s filename : filename exists and is not empty Putting! in front of the test operator ( the letter with a dash in front of it ) inverts the test

Lab 3 -filetests.sh

Test Expressions Test is generally used to either test data (usually in variables), or to test file attributes Test expressions may test single things for some characteristic, or attribute - this is known as a unary test Test expressions may compare two things - this is known as a binary test Unary test expressions take the form -x thing, where -x is the test operator specifying what kind of test to perform Binary test expressions take the form thing1 operator thing2 where operator tells the test command what kind of comparison to perform Putting! in front of the test operator inverts the test

Unary Operators The only unary operator that checks a variable is -v variablename which is true if the variable exists and false if it doesn't Text strings can be tested to see if they have no text in them with -z "sometext" or have some text with -n "sometext" Since it would make no sense to do such a test on literal text, it is easy to see that we would use some kind of dynamic data with the -n or -z tests

Binary Operators For Text Text strings can be compared using the following operators "string1" = "string2" is true if the two strings are identical "string1"!= "string2" is true if the two strings are not identical Strings consisting of digits are compared as text by these operators, not as numbers

Lab 3 - stringtests.sh

Binary Operators For Numbers To compare numbers, there are several binary test operators available X -eq Y is true if X and Y are the same number X -ne Y is true if X and Y are not the same number X -lt Y is true if X is numerically less than Y X -gt Y is true if X is numerically more than Y X -le Y is true if X is numerically less than Y or X is equal to Y X -ge Y is true if X is numerically more than Y or X is equal to Y

Lab 3 - numtests.sh

Conditional Script Block Execution A list of action commands can be run, or not run, based on the success or failure of a testing command list The test command can evaluate expressions, so it is the most common command for the testlist An alternate testlist can be specified using elif, and you can use as many alternate testlist/ actionlist elifs as necessary A default actionlist to run if no testlists are successful can be included by using else if [ expr ]; then actionlist fi if testlist; then actionlist elif testlist; then actionlist else actionlist fi

Lab 3 - passwordguesser.sh

Script Block Looping An action list can be executed repeatedly (known as a loop) based on the success or failure of a testlist using the while command The break or continue commands can be used in the action list to get out of a loop early while testlist; break jumps to the done command and continues the script past the loop do done actionlist continue jumps back to the while command to redo the testing list

Lab 3 - guessinggame.sh

Advanced Topics Operating a fixed number of times on something or on a list of data items Multiple reference value tests for a single variable Lists of data require arrays to hold the entire list as a single variable with a dynamic size

For Command The for command allows repeated execution of a list with each word from a word list The for command specifies a variable name The first word in the list is put into the variable and the action list is run Then the next word in the list is put into the variable and the action list is run, until there are no more words to get from the list for varname in wordlist; do list done

Lab 3 - improved-systemid.sh

For Command The for command allows repeated execution of an action list controlled by evaluating 2 commands and a test expression, and is commonly used for loops that use a counter The initial command is run once The test expression is tested before the action list executes The loop command is run once after the action list After the loop command runs, the test is done to see whether to execute the action list and loop command again for (( initial command; test expression; loop command )); do list done

Lab 3-4guessesgame.sh

Case Command The case command allows execution of a list based on the value of a variable or word Very commonly used to process the special variables $1, $2, $3, etc. It allows comparing a variable's contents to multiple reference values or patterns case $var in pattern ) list ;; pattern pattern ) list ;; * ) list ;; esac

COMMAND LINE ARGUMENTS Any command may have options and arguments The command line to run a script is accessible by the script, using the special variables $0, $1, $2, etc. $0 holds the command itself and $# holds a count of how many words are on the command line other than the command itself $1, $2, $3, etc. hold each of the remaining words from the command line On a bash command line, words are space-separated sequences of characters Quoting and the escape character can be used to create user-desired word boundaries on the command line (e.g. "My File" becomes a single word)

COMMAND LINE PROCESSING A loop can be used to cycle through the available command line arguments and interpret what is there We can use shift to renumber the command line variables each time through the loop Requiring arguments of the form -x or --option-name is known as using named arguments The case statement is better than the if statement for this while [ $# -gt 0 ]; do case "$1" in -h --help ) echo "Usage: $(basename $0) [-h --help]" exit ;; * ) echo "Unrecognized argument '$1'" exit 1 ;; esac shift done # Command line processed # Named arguments recognized and saved as needed

Lab 3 - cmdargs.sh

Show me your work to get the marks for it