bash 3.x Advanced Shell Scripting

Similar documents
Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

A shell can be used in one of two ways:

INTRODUCTION TO SHELL SCRIPTING ITPART 2

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

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Useful Unix Commands Cheat Sheet

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Shells & Shell Programming (Part B)

Linux shell programming for Raspberry Pi Users - 2

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

CSE 390a Lecture 5. Intro to shell scripting

Shell programming. Introduction to Operating Systems

More Scripting Todd Kelley CST8207 Todd Kelley 1

Bash scripting basics

Shells and Shell Programming

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Bash command shell language interpreter

Shells and Shell Programming

Output with printf Input. from a file from a command arguments from the command read

CS Unix Tools & Scripting

CSE 391 Lecture 5. Intro to shell scripting

Basic Linux (Bash) Commands

Shell Start-up and Configuration Files

COMP 4/6262: Programming UNIX

Shell Programming (ch 10)

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

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

Title:[ Variables Comparison Operators If Else Statements ]

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

Assignment clarifications

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

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

Chapter 5 The Bourne Shell

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

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

Linux shell scripting Getting started *

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Bourne Shell Reference

Shell Programming (bash)

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

UNIX System Programming Lecture 3: BASH Programming

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

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

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

bash Execution Control COMP2101 Winter 2019

Bash shell programming Part II Control statements

bash Data Administrative Shell Scripting COMP2101 Fall 2017

EECS2301. Lab 1 Winter 2016

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Chapter 4. Unix Tutorial. Unix Shell

UNIX Shell Programming

Pro Bash Programming. Scripting the GNU/Linux Shell. Second Edition. Jayant Varma. Chris F. A. Johnson. Hi mm mm. mmmm mmmm HHi

Last Time. on the website

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

d. 1 e. test: $a: integer expression expected

9.2 Linux Essentials Exam Objectives

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

5/20/2007. Touring Essential Programs

The Unix Shell & Shell Scripts

GNU Bash. An Introduction to Advanced Usage. James Pannacciulli Systems (mt) Media Temple

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Lab 2: Linux/Unix shell

Vi & Shell Scripting

Introduction of Linux. Huang Cheng-Chao Dept. of Comput. Sci. & Tech. East China Normal University

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

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

elinks, mail processes nice ps, pstree, top job control, jobs, fg, bg signals, kill, killall crontab, anacron, at

Multiple Choice - 58 Questions - 10 of 15%

Getting your department account

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

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Topic 2: More Shell Skills

Introduction to Linux

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Bash Reference Manual

Introduction to Linux

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

Linux & Shell Programming 2014

CSE 15L Winter Midterm :) Review

Week 5 Lesson 5 02/28/18

Shell Programming (Part 2)

ScazLab. Linux Scripting. Core Skills That Every Roboticist Must Have. Alex Litoiu Thursday, November 14, 13

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

Introduction to Linux

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Introduction: What is Unix?

Introduction to Unix: Fundamental Commands

Bash Check If Command Line Parameter Exists

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.

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

Conditional Control Structures. Dr.T.Logeswari

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

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

Lecture #10 - Interactive Korn Shell (Chapter 11)

Transcription:

bash 3.x Advanced Shell Scripting Michael Potter October 24, 2006 (see copyright notice on last slide) 1

Why bash? Simple to get started. Actively developed and ported. Includes advanced features. 2

Goals for Tonight 3

Understanding the Demos 4

The demo script #!/opt/local/bin/bash set -o option echo "My process list:" >outputfile.txt ps -ef 2>&1 grep "^$USR" >outputfile.txt echo "script finished: return code is $?" 5

noclobber demo 6

What did we learn? set -o noclobber used to avoid overlaying files set -o errexit used to exit upon error, avoiding cascading errors 7

command1 command2 8

pipefail demo 9

What did we learn? set -o pipefail unveils hidden failures set -o errexit can exit silently trap command ERR corrects silent exits $LINENO enhances error reporting 10

nounset demo 11

What did we learn? set -o nounset exposes unset variables 12

the final demo script #!/opt/local/bin/bash set -o noclobber set -o errexit set -o pipefail set -o nounset trap 'echo error at about $LINENO' ERR mv outputfile.txt outputfile.bak echo "My process list:" >outputfile.txt ps aux 2>&1 grep "^$USER" >>outputfile.txt echo "script finished: return code is $?" 13

the final demo script #!/opt/local/bin/bash../stringent.sh exit 1 mv outputfile.txt outputfile.bak echo "My process list:" >outputfile.txt ps aux 2>&1 grep "^$USER" >>outputfile.txt echo "script finished: return code is $?" 14

# stringent.sh set -o errexit set -o noclobber set -o nounset set -o pipefail stringent.sh function traperr { echo "ERROR: ${BASH_SOURCE[1]} \ at about line ${BASH_LINENO[0]}" } set -o errtrace trap traperr ERR 15

BASH_SOURCE/BASH_LINENO echo "ERROR: ${BASH_SOURCE[1]} \ at about line ${BASH_LINENO[0]}" ERROR:./buggy.sh at about line 7 ${FUNCNAME[$i]} was called at ${BASH_LINENO[$i]}in ${BASH_SOURCE[$i]} 16

BASH_COMMAND function traperr { echo "ERROR: $1 ${BASH_SOURCE[1]} \ at about line ${BASH_LINENO[0]}" } BROKEN trap traperr $BASH_COMMAND ERR 17

PIPESTATUS bash-3.1$ ps -ef 2>&1 grep "^$USR" >/dev/null bash-3.1$ echo "PIPESTATUS = ${PIPESTATUS[*]} \$? = $?" PIPESTATUS = 1 0 $? = 0 bash-3.1$ bash-3.1$ bash-3.1$ set -o pipefail ps -ef 2>&1 grep "^$USR" >/dev/null bash-3.1$ echo "PIPESTATUS = ${PIPESTATUS[*]} \$? = $?" PIPESTATUS = 1 0 $? = 1 ps aux 2>&1 grep "^$USER" >/dev/null bash-3.1$ echo "PIPESTATUS = ${PIPESTATUS[*]} \$? = $?" PIPESTATUS = 0 0 $? = 0 bash-3.1$ echo "PIPESTATUS = ${PIPESTATUS[*]} \$? = $?" PIPESTATUS = 0 $? = 0 18

Variables 19

Integer Demo 20

What did we learn stringent.sh Proven to be a good idea declare -i variable non-integer values caught sooner unset variables used as an int are 0 unless caught with set -o nounset $((... )) arithmetic syntax 21

gotcha declare -i MyInt1=012 declare -i MyInt2=0x12 echo "Value1 = $MyInt1" echo "Value2 = $MyInt2" printf "%o %x\n" $MyInt1 $MyInt2 Value1 = 10 Value2 = 18 12 12 22

Arithmetic Syntax inta=$(( ($intb + 5) * 2 )) Allowed anywhere a variable is allowed let "inta = ($intb + 5 ) * 2" returns 0 or 1 (( inta = ($intb + 5) * 2 )) equivalent to let inta=\($intb+5\)*2 no spaces allowed Special characters must be escaped inta must be declare -i inta=$[ ($intb + 5) * 2 ] deprecated 23

local variables weak good enough not just local, local and below two ways to declare: declare local $1, $2,... are not scoped the same 24

Scoping declare Var= Zero" MyPrintVars 1 Func1 MyPrintVars 5 function Func1 { declare Var1= One" declare Var2= Pair" declare Var4="Four" MyPrintVars 2 Func2 MyPrintVars 4 } function Func2 { declare Var3= Tri" declare Var4= Quad" Var2= Two" MyPrintVars 3 } 25

Handling undefined variables function PrintVars { echo -n Var1=${Var1:-notset} echo -n Var2=${Var2:-notset} echo -n Var3=${Var3:-notset} echo -n Var4=${Var4:-notset} echo Var5=${Var5:-notset}" } 26

Scoping declare Var= Zero" MyPrintVars 1 Func1 MyPrintVars 5 function Func1 { declare Var1= One" declare Var2= Pair" declare Var4="Four" MyPrintVars 2 Func2 MyPrintVars 4 } function Func2 { declare Var3= Tri" declare Var4= Quad" Var2= Two" MyPrintVars 3 } 1 Var=Zero Var1=notset Var2=notset Var3=notset Var4=notset 2 Var=Zero Var1=One Var2=Pair Var3=notset Var4=Four 3 Var=Zero Var1=One Var2=Two Var3=Tri Var4=Quad 4 Var=Zero Var1=One Var2=Two Var3=notset Var4=Four 5 Var=Zero Var1=notset Var2=notset Var3=notset Var4=notset 27

readonly variables Two ways to declare declare -r readonly One way trip Used with -i to create readonly integers readonly can be used on system variables e.g. keep users from changing their prompt not documented! 28

conditionals if command if (( )) if [ ] if test if [[ ]] 29

if command set +o errexit grep Jim /etc/passwd declare -i Status=$? set -o errexit if (( $Status == 0 )) then echo Jim is a user fi if grep Jim /etc/passwd then echo Jim is a user fi! grep Jim /etc/passwd declare -i Status=$? if (( $Status!= 0 )) then fi echo Jim is a user 30

What did we learn? set +o errexit turns off errexit Save $? to a permanent variable! turns off errexit for a single command zero is true, non-zero is false if (( )) used for numeric tests 31

gotcha if [[ $Age > 21 ]] # bad > is a string comparison operator if [ $Age > 21 ] # bad > is a redirection operator if [[ $Age -gt 21 ]] # good fails in strange ways if $Age is not numeric if (( $Age > 21 )) # best $ on Age is optional 32

test and [ bash-3.1$ /bin/test which test bash-3.1$ which [ /bin/[ bash-3.1$ ls -l /bin/[ /bin/test -r-xr-xr-x 2 root wheel 18104 Aug 21 2005 /bin/[ -r-xr-xr-x 2 root wheel 18104 Aug 21 2005 /bin/test 33

So? 34

if [[ ]] 35

[ versus [[ [[ $a == z* ]] True if $a starts with an "z. [[ $a == "z*" ]] True if $a is exactly equal to z*. [ $a == z* ] Error if $a has a space. Error if more than one filename starts with z. True if a filename exists that starts with z and is exactly $a. True if no filenames exist that start with z and $a equals z*. [ "$a" == "z*" ] True if $a is exactly equal to z*. 36

37

the rules use [ when you want to use file globbing use (( when you want to do math use [[ for everything else 38

regular expressions Introduced with version 3.0 Implemented as part of [[ ]] Uses binary operator =~ Supports extended regular expressions Supports parenthesized subexpressions 39

regular expression declare MyStr="the quick brown fox" [[ $MyStr == "the*" ]] # false: must be exact [[ $MyStr == the* ]] # true: pattern match [[ $MyStr =~ "the" ]] # true [[ $MyStr =~ the ]] # true [[ $MyStr =~ "the*" ]] # true 40

subexpressions declare MyStr="the quick brown fox" if [[ $MyStr =~ "the ([a-z]*) ([a-z]*)" ]] then echo "${BASH_REMATCH[0]} # the quick brown echo "${BASH_REMATCH[1]} # quick echo "${BASH_REMATCH[2]} # brown fi 41

bad expressions declare MyStr="the quick brown fox" if [[ $MyStr =~ "the [a-z) ([a-z*)" ]] then echo "got a match" elif (( $? == 2 )) then traperr "Assertion Error: Regular expression error" exit 1 fi 42

gotcha cp $srcfile $dstfile broken if $srcfile has a space cp $srcfile $dstfile broken if srcfile begins with - cp -- $srcfile $dstfile 43

quoting declare MyVar="bob" echo \\ $MyVar \x41' echo $ \\ $MyVar \x41 echo \\ $MyVar \x41" bash-3.1$./quoting.sh \\ $MyVar \x41 \ $MyVar A \ bob \x41 44

quoting recommendation quote variables liberally extra quotes likely to cause a consistent error missing quotes are likely to cause inconsistent behavior Safe Exceptions within if [[ ]] Integer variables (define -i) within if (( )) 45

Handling undefined variables function PrintVars { echo -n Var1=${Var1:-notset} echo -n Var2=${Var2:-notset} echo -n Var3=${Var3:-notset} echo -n Var4=${Var4:-notset} echo -n Var5=${Var5:-notset}" } 46

unset & null variables ${parameter:-word} returns word ${parameter:+word} returns empty (returns word if set) ${parameter:=word} sets parameter to word, returns word ${parameter:?message} echos message and exits 47

unset & null variables ${parameter-word}. ${parameter+word}. ${parameter=word}. ${parameter?message}. 48

default variables function MyDate { declare -i Year=${1:?"$0 Year is required"} declare -i Month=${2:-1} declare -i Day=${3:-1} if (( $Month > 12 )); then echo "Error Month > 12" >&2 exit 1 fi if (( $Day > 31 )); then echo "Error Day > 31" >&2 exit 1 fi } echo "$Year-$Month-$Day" 49

sub strings declare MyStr="The quick brown fox" echo "${MyStr:0:3}" # The echo "${MyStr:4:5}" # quick echo "${MyStr: -9:5}" # brown echo "${MyStr: -3:3}" # fox echo "${MyStr: -9}" # brown fox 50

substr by pattern ${Var#pattern} ${Var%pattern} ${Var##pattern} ${Var%%pattern} 51

a jingle We are #1 because we give 110% 52

substr by pattern declare MyStr="/home/pottmi/my.sample.sh" echo "${MyStr##*/}" # my.sample.sh echo "${MyStr%.*}" # /home/pottmi/my.sample echo "${MyStr%/*}" # /home/pottmi echo "${MyStr#*/} #home/pottmi/my.sample.sh echo "${MyStr%%.*}" # /home/pottmi/my 53

search and replace ${Var/pattern/replace} 54

substr by pattern declare MyStr="the fox jumped the dog" echo "${MyStr/the/a}" echo "${MyStr//the/a} echo "${MyStr//the } # a fox jumped the dog # a fox jumped a dog # fox jumped dog 55

unintended subshells declare -i Count=0 declare Lines cat /etc/passwd while read Lines do echo -n "." ((Count++)) done echo " final count=$count"... final count=0 56

unintended subshells declare -i Count=0 declare Lines do echo -n "." ((Count++)) done </etc/passwd while read Lines echo " final count=$count"... final count=38 57

unintended subshells declare -i Count=0 declare Lines while read Lines do echo -n "." ((Count++)) done < <(cat /etc/passwd) echo " final count=$count"... final count=38 58

Learn more man bash O Reilly - Learning the Bash shell http://bashdb.sourceforge.net/bashref.html http://www.faqs.org/docs/abs/html/ Ask me to help! 59

Contact Information Michael Potter PO Box 469 Lisle, IL 60532 +1 630 926 8133 60

Copyright Notice This presentation is copyright Michael Potter 2006. No duplication is allowed without my permission. Contact me for permission, you just might get it. You are welcome to view and link to this presentation on the UniForum website. You are welcome to copy stringent.sh from the slide, but please add a comment with a link to this presentation in the source. 61