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

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
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.
Linux+ Guide to Linux Certification, Second Edition
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.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
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.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
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.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Introduction To Repetition The for loop
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Scripts & Functions Scripts and functions are contained in .m-files
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Unix Scripting Dave Yearke
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Linux Shell Script Programming
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Unix Scripting Session 2 March 13, 2008.
Presentation transcript:

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

Control: The IF Statement (Quigley pages 886 – 896) The command is executed and its result (the same as $?) evaluated. If the result is zero, the then clause is executed; if it's not zero, it's skipped. This evaluation is then repeated in turn for each elif clause, if any. If no then clause is executed, the statements in the optional else clause is executed. if command then statements elif command then statements else statements fi

Numeric if example Full-form simple if statement declare -i x=1 if ((x==1)) then echo x is $x fi Or the compact form if ((x==1)); then echo x is $x; fi Or the recommended form: if ((x==1)); then echo x is $x fi

PDL for the if Control Statement The PDL is really quite simple. Be sure to describe WHAT is happening and/or WHY it's being done. IF command as PDL statements ELSE IF command statements ELSE statements ENDIF IF porridge burns PUT too hot ELSE if porridge gluey PUT too cold ELSE PUT just right ENDIF if ptemp > my_val; then echo too hot elif ptemp < my_val echo too cold else echo just right fi

Command if example Using a command (no brackets needed): if grep -q "string" file.txt; then echo \"string\" found in file.txt elif grep -q "something" file.txt; then echo \"something\" found instead else echo neither \"string\" nor \"something\" fi Be sure to get rid of any output to stdout (grep's -q). What PDL would have preceded this?

Control: The CASE Statement (Quigley pages 900 – 902) The variable is compared with the values using the shell wildcards ( ? * […] ), NOT regular expressions. All the statements are executed for the first matching value until the ending ;;. If no value matches, then the default *) case is executed, if present. case variable in value1) statements ;; value2) statements ;; *) statements ;; esac

PDL for the case Control Statement Some refuse to allow PDL for the case statement, in part because it's called different things in other languages (like switch) or because it's more like a long if-elif statement. I don't think that's reasonable, but it is necessary to describe it carefully. It's important to show the end of each clause as well as the end of the entire control construct. CHOOSE from variable description CHOICE value1 statements END CHOICE CHOICE value2 statements END CHOICE DEFAULT CHOICE statements END CHOICE END CHOOSE

case Example declare var="fred"... case "$var" in stuff) echo var was "stuff" ;; fred) echo var was "fred" ;; [fF]r?d) echo Fred, or something strange ;; *) echo default case ;; esac

Loops: do and done A do-done pair is use to mark the start and end of every type of loop in bash. They frame a block of statements to be executed each time control passes through the loop. Control over the loop is completely in the hands of whichever one of the control statements for, while, or until that precedes the do-done statement block: for/while/until loop control do statements done As with "then" in the if statement, we'll put the "do" after a semicolon in the control line. Similarly, the PDL will ignore the "do" and show the loop end as END FOR, END WHILE, or END UNTIL.

Loops: The for Statement (Quigley pages 903 – 907) The do-done block is executed once for each word in the wordlist, assigning each word in turn to the variable. If "in wordlist" is omitted, the positional parameters from the caller starting at $1 are used in its place. for variable in wordlist; do statements done As stated above, the PDL will look like this: FOR description of wordlist statements END FOR

for Loop Example (using $()) ls for x in $(ls); do cp $x $x'.backup' done ls The ls before the for statement shows: abc def ghi The ls after the for statement shows: abc abc.backup def def.backup ghi ghi.backup

for Loop Example (using declare -i count=0 for x; do let count+=1 echo Arg $count is $x done If the command line had been invoked as:./test-script apple banana cherry Then the output from the for loop will be: Arg 1 is apple Arg 2 is banana Arg 3 is cherry

Loops: The while and until statements (Pages 907 – 912) The do-done loop is executed as long as command returns a value of zero (the same as true from $?). while command; do statements done until is the opposite of while, and executes as long as command returns a non-zero value (false or error from $?). until command; do statements done

Numeric loops: a common idiom You will often see, or use yourself, a simple numeric loop. This example counts up, from 0 to $max, but others may count from 1 or may count down, from $max to 0 or 1. Note that counting often starts from 0. declare -i i=0... while (( i < $max )) do echo i has the value $i let i++ done This example also uses "post-incrementing". That is, the adding of 1 to i is done at the bottom of the loop. You will also see it incremented (or decremented) at the top of the loop, depending on the purpose of the script.

Numeric loops: using until This is the same loop, using until instead of while. Notice that it's only the comparison that changes with the command. declare -i i=0... until (( i >= $max )) do echo i has the value $i let i++ done

Note about loops and string compares If you try to use a simple comparison like: while (( $var==fred )) # numerical or while [[ $var==fred ]] # character It sometimes, as in once in a while, wont seem to work correctly. Bash can be very picky at times when dealing with strings. I've heard this error reported but it's not very likely. If you should run into it, use: while echo $var | grep -q "^fred$"

I/O Redirection and Subshells for Loops (Pages 923 – 927) The whole of a loop right down to the done statement can be treated as a unit for redirection and background processing, so that constructs like these are possible: Pipe data into or out of a loop: command | for do … done while do … done | command Redirect file output or input for a loop: until do … done >> filename while do … done < filename Run a loop entirely in the background: for do … done &

The break and continue statements (Pages 919 – 920) break exits from the current loop by executing the statement after the done. That is, it leaves from the bottom of the loop. break continue send control to the command after the do statement. That is, it returns to the top of the loop. continue Both have an optional number. When it's supplied, break breaks out of that many nested loops. continue returns to the top of the nth loop back. These are quite dangerous. Use with extreme caution. Better, don't use them! break number continue number

The null statement (Pages 898) The null statement : (colon) does nothing at all. Use it for empty then clauses, if you can't clearly reverse the test. Of course, the command-end character ; (semicolon) does pretty much the same thing. if some-test then : else # do something exciting fi

The select statement (Pages 912) Creating interactive text menus is much simplified by using the select loop and the PS3 prompt. select variable in wordlist; do... done Each item in wordlist (use quotes if an item includes blanks) is sent to stderr (not stdout) with a sequential number to its left, after a PS3 prompt. The number selected by the user is stored in the REPLY variable (like read) and the corresponding word is placed in variable. Use a case or an if-elif to check the menu input. Because select is a loop control, the break command must used to exit from it (or exit to end the script). There is no ending condition as there is in for, while, and until. Choose your prompt and set PS3 before your select.

Sample PDL for select SET prompt SELECT from list of characters CHOOSE from value returned CHOICE Bart PUT message END CHOICE CHOICE Homer PUT message END CHOICE DEFAULT CHOICE BREAK out of loop END CHOICE END CHOOSE END SELECT

Implementation of select example PS3="Whos Your favorite Simpson? " select x in "Bart" Homer 'Quit now'; do case $x in Bart) echo Eat my cow, man ;; Homer) echo "D'oh" ;; *) break # this choice exits loop ;; esac done