CISC3130, Spring 2013 Dr. Zhang 1 Bash Scripting: Advanced Topics.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

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
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
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.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
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.
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.
 Prepared by: Eng. Maryam Adel Abdel-Hady
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
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Linux Shell Script Programming
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Presentation transcript:

CISC3130, Spring 2013 Dr. Zhang 1 Bash Scripting: Advanced Topics

Outline 2 Review HW1, HW2 and Quiz2 Review of standard input/output/error How to redirect them ? Pipeline Review of bash scripting Functions Here documents Arrays

Homework 2 match phone numbers in text file , , (718)817, , or 817,4484, (01)718,817,4484, 01, grep -f phone.grep file.txt, where phone.grep: [^0-9][0-9]\{10\}$ [^0-9][0-9]\{10\}[^0-9] [^0-9][0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$ [^0-9][0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}[^0-9] [^0-9][0-9]\{3\}\,[0-9]\{4\}$ [^0-9][0-9]\{3\}\,[0-9]\{4\}[^0-9] [^0-9][0-9]\{3\}\-[0-9]\{4\}$ [^0-9][0-9]\{3\}\-[0-9]\{4\}[^0-9] 3 Match 10 digits at end of line Match 10 digits, and a non-digit char ,4484 at end of line

Homework 2 [^0-9]*\([0-9]\{2\}\)\([0-9]\{3\}\)[0-9]\{3\}\,[0-9]\{4\}$ [^0-9]*\([0-9]\{2\}\)\([0-9]\{3\}\)[0-9]\{3\}\,[0-9]\{4\}[^0-9] [^0-9]*\([0-9]\{2\}\)\([0-9]\{3\}\))?[0-9]\{3\}\-[0-9]\{4\}$ [^0-9]*\([0-9]\{2\}\)\([0-9]\{3\}\))?[0-9]\{3\}\-[0-9]\{4\}[^0-9] [^0-9]*[0-9]\{2\}\,[0-9]\{3\}\,[0-9]\{3\}\,[0-9]\{4\}$ [^0-9]*[0-9]\{2\}\,[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}[^0-9] [^0-9]*[0-9]\{2\}\,[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$ [^0-9]*[0-9]\{2\}\,[0-9]\{3\}\,[0-9]\{3\}\,[0-9]\{4\}[^0-9] 4

Homework 2 Write a sed script file that remove all one-line comments from C/C++ source code files. Note that such comments starting with //, and ends at the end of line. You need to take care the cases where // appears in double quote, or single quote, in thsse cases, what comes after // is not comment. rmcnt.sed : #!/bin/sed -f ## remove one-line comments from C/C++ code /^[^'"]*\/\// s/\/\/.*$/ /g rmcnt.sed sample.cpp 5 Apply to lines that contain // not preceding by ' or " Replace // and following chars with space

Quiz 2 How to write to standard output in shell script: #!/bin/bash echo "Hello world"; echo "Something is wrong" 1>& 2 ls ABCDEF 2>&1 Try it out:./test_redirect.sh 2> err_out ## what happens?./test_redirect.sh > std_out ## what happens?./test_redirect.sh > std_out 2>&1 6

Quiz2 Mark constants with #/bin/bash # First find numerica constants in the code #grep -E '[^a-zA-Z_][0-9]+\.?[0-9]+' $1 # now mark constants with <> echo mark constants in file $1 sed 's/\([^a-zA-Z0-9_]\)\([0-9][0-9]*\.\{0,1\}[0-9][0-9]*\)/\1\ /g' $1 7 The char before constant: not alphabet, not _, and not digit A numeric constant: Optional decimal points: \.\{0,1\} cannot use ?, as sed use BRE

Standard input/output/error 8 By default, link to keyboard and terminal window respectively Can be redirected to files Can be redirected to pipeline input can be redirected to reading end of a pipe output and error can be redirected to writing end of a pipe When a bash script’s input/output/error is redirected: E.g., headtail 3 10.bash_profile > output ls –l | headtail | wc –l input/output/error for every command in the script are redirected !

Save standard input if necessary 9 #!/bin/bash # Count # of lines, and search for phone in a file; if a file is # not specified, process standard input set -x ## turn on execution tracing if [ $# -eq 0 ] then cat > stdinput ## save standard input to a file set stdinput fi ## so that we can use as many times as we want wc –l $* grep -f phone.grep $* rm stdinput exit 0 Or use –x in first line, i.e., #!/bin/bash –x Or type $ bash -x countlines_searchphoneno.sh to run the scripts Code at: countlines_searchphoneno.sh

Redirection can be applied to loop 10 rm all_shellscripts for i in `ls *.sh` do echo $i >>all_shellscripts cat $i >>all_shellscripts done for i in `ls *.sh` do echo $i cat $i done > all_shellscripts Similar for <, |

case construct: branching case construct is analogus to switch in C/C++. case "$variable" in shellpattern1 ) command... ;; shellpattern2) command … ;; shell pattern n) command... ;; esac 11 Quoting variables is not mandatory Each pattern can contain shell wildcard (*,?,[a-z]), ends with a ) Each condition block ends with ;; If a condition tests true, then associated commands execute and the case block terminates. entire case block ends with an esac

Calculator using case block case "$op" in "+" ) result=$(($x + $y)) echo $x $op $y = $result;; "-" ) result=$(($x - $y)) echo $x $op $y = $result;; "*" ) result=$(($x * $y)) echo $x \* $y = $result;; "/" ) result=$(($x / $y)) echo $x $op $y = $result;; * ) echo Unknow operator $op;; esac 12

#!/bin/bash OPT=$1 # option FILE=$2 # filename # test -e and -E command line args matching case $OPT in -e|-E) echo "Editing $2 file..." # make sure filename is passed else an error displayed [ -z $FILE ] && { echo "File name missing"; exit 1; } || vi $FILE ;; -c|-C) echo "Displaying $2 file...“ [ -z $FILE ] && { echo "File name missing"; exit 1; } || cat $FILE ;; -d|-D) echo "Today is $(date)" ;; *) echo "Bad argument!" echo "Usage: $0 -ecd filename" echo " -e file : Edit file." echo " -c file : Display file." echo " -d : Display current date and time." ;; esac 13 test if string is null Lazy evaluation of && and ||

Case example case $1 in -f) ## case for –f option ;; -d | --directory) ## -f or –directory option ;; *) echo $1: unknown option >&2 exit 1; esac 14

More about bash loop structures 15

Infinite loop 16 while [ 1 ] do echo -n "Enter your password" read input if [ $input = "secret" ] then break ## break out of the loop else echo -n "Try again... " fi done

continue command Continue from the top of the for loop Ignore rest of commands in the loop, and continue the loop from the top again (for the next value in the list) i=1 for day in Mon Tue Wed Thu Fri Sat Sun do echo -n "Day $((i++)) : $day" if [ $i -eq 7 -o $i -eq 8 ]; then echo " (WEEKEND)" continue; fi echo " (weekday)" done 17

For loop without a list #!/bin/bash for i do echo hello $i done 18

For loop i=1 for username in `awk -F: '{print $1}' /etc/passwd` do echo "Username $((i++)) : $username" done 19

Loop through files/directories loop through files and directories under a specific directory i=1 cd ~ for item in * do echo "Item $((i++)) : $item" done 20

C-Style for loop for (( EXP1; EXP2; EXP3 )) do Command1 … Commandn done Example: #!/bin/bash for (( c=1; c<=5; c++ )) do echo "Welcome $c times“ done 21 EXP1: initializer EXP2: a loop-test or condition EXP3: counting expression

Select loop select construct: allows easy menu generation select WORD [in LIST] do RESPECTIVE-COMMANDS; done 1. List of items printed to standard error, each item preceded by a number. If in LIST is not present, positional parameters (command line arguments) are used 2. A prompt is printed, one line from standard input is read. 1. If input is a number corresponding to one of items, value of WORD is set to name of that item. 2. If line is empty, items and the PS3 prompt are displayed again. 3. If an EOF (End Of File) is read, loop exits. 3. RESPECTIVE-COMMANDS are executed after each selection 4. Go back to 1 22

select construct: example 23 #!/bin/bash OPTIONS="Hello Quit“ select opt in $OPTIONS; do if [ "$opt" = "Quit" ] then echo done exit elif [ "$opt" = "Hello" ] then echo Hello World else echo bad option fi done ~zhang/public_html/cs3130/Codes/select_ex

Next: 24 More advanced bash scripting Array Function Inline input, or here document

Array Bash provides one-dimensional array variables Assign values to array: array=( one two three ) files=( "/etc/passwd" "/etc/group" "/etc/hosts" ) limits=( ) Access array element : ${array_name[index]} indexed using integers and are zero-based. ${array[1]} To access all items in arary: ${array_name[*]}, To access array length : 25

To Iterate Through Array Values 26 #!/bin/bash # declare an array called array and define 3 vales array=( one two three ) for i in do echo $i done

Exercise/Example Write a script that read a sequence of numbers and save them in an array, print out the array content and size. Usage: EchoNumber [file] If no file is specified, read from standard input Example script: LargestSmallest.sh 27

#!/bin/bash i=0 if [ $# -eq 0 ] then echo "Enter the numbers, Ctrl-D to end"; cat > stdinput set stdinput fi while read num do a[$i]=$num i=$((i+1)) done < $1 echo Array is ${a[*]}, with ${#a[*]} numbers 28

Bash function Functions: to increase modularity and readability More efficient than breaking scripts into many smaller ones Syntax to define a function: function functionname() { commands.. } function is a keyword which is optional. functionname is the name of the function No need to specify argument in ( ) commands – List of commands to be executed I exit status of the function is exit status of last command executed in the function body. 29

Function call Call bash function from command line or script $ functionname arg1 arg2 When shell interprets a command line, it first looks into the special built-in functions like break, continue, eval, exec etc., then it looks for shell functions. function defined in a shell start up file (e.g.,.bash_profile ). available for you from command line every time you log on 30

About functions 31 Parameter passing: $1, $2, … Result returning Use echo command Through setting a variable return command: to return an exit status #! /bin/bash calsum(){ echo `expr $1 + $2` } x=1;y=2; sum=`calsum $x $y` calsum(){ sum=`expr $1 + $2` } x=1;y=2; calsum $x $y echo z=$sum

About functions 32 Local variable: its scope is within the function #! /bin/bash calsumsqr(){ local sum=`expr $1 + $2`; echo `expr $sum * $sum` } x=1;y=2; z=`calsum $x $y`

Exercise/Example Write a function that check whether a user is log on or not (CheckUser.sh) function UserOnline() { if who | grep $1 ## UserOnline takes a parameter then return 0 ## 0 indicates success else return 1 ##1 for failure, i.e., offline fi } if UserOnline $1 ## function’s return value as condition/test then echo User $1 is online else echo User $1 is offline fi 33

Here document (inline document) 34 A special way to pass standard input to a command: here document, i.e., from shell script itself Benefits: store codes and data together, easier to maintain #!/bin/bash cat <<!FUNKY! Hello This is a here Document !FUNKY! Here document starts with <<, followed by a special string which is repeated at the end of the document. Note: the special string should be chosen to be rare one.

Here document:2 35 Example: 411 script that looks up a phone book Usage example: 411 joke #!/bin/bash grep “$*” << End Dial-a-joke Dial-a-prayer Dial santa End

Phone # searching script #!/bin/bash cat > phone.pattern << PATTERNS [^0-9][0-9]\{10\}$ [^0-9][0-9]\{10\}[^0-9] [^0-9][0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$ [^0-9][0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}[^0-9] [^0-9][0-9]\{3\}\,[0-9]\{4\}$ PATTERNS grep -f phone.pattern $* rm phone.pattern ##no need to keep this file … 36

A case study: bundle program 37 Suppose a friend asks for copies of shell files in your bin directory $ cd ~/bin $ for i in *.sh > do > echo ===== This is file $i ============= > cat $i > done | mail Pipeline & input/output redirection can be applied to for, while, until loop.

Make it better ? 38 Construct a mail message that could automatically unpack itself, i.e., to generate the original files packed inside E.g., A shell script contains instructions for unpacking, and the files content themselves Use here document mechanism

A bash script contains two files 39 #To unbundle, bash this file echo file1 1>&2 cat >file1 <<'End of file1' A B C End of file1 echo file2 1>&2 cat >file2 <<'End of file2' end of file2 What does this script do? How to create such bundle file automatically? Use a script, bundle.sh file1 file2 file3 …

Bundle script 40 #!/bin/bash ## write a shell script that contains files specified in arguments echo '#!/bin/bash' echo ‘# To unbundle, bash this file’ for i ## without a list of items, loop through ## command line arguments do echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i” done codes/examples/bundle.sh For homework 3, use bundle.sh to generate a hw3_bundle file, and submit it.

Summary Review of shell scripting Examples Array, function, inline document 41

Outline 42 Coding standard: how to get good grades in lab assignment Review of standard input/output/error How to redirect them ? Pipeline Review of bash scripting

Bash scripting: general hints 43 Use echo command to trace (like cout, printf in C/C++) Sometimes there are alternatives ways to do things, choose one and remember it: $(( …)), and $[ … ] [[ ]] for test Be careful about typo, shell wont complain variable not declared/assigned … The price of freedom A walk-through of basic bash scripting

Bash Scripting 44 Variables Environment variable: affect behavior of shell User defined variable: default type is string, can declare it to be other type Positional variables: used to pass command line arguments to bash script Variable assignment: x=10 ## assign value 10 to variable x, no space around = x=$x+1 ## add 1 to x’s value and assign to x PATH=$PATH:.:~/bin To refer to a variable’s value, precede variable name with $

A script that display positional variable 45 echo All arguments: $* echo Number of arguments: $# echo Script name: $0 echo argument 1: $1 echo argument 2: $2 for arg in $* do echo argument $arg done

arithmetic operation 46 As variable’s default type is string, to perform arithmetic operation, use the following syntax $[$x+1] or $(($x+1)) For simpler syntax: declare variable to be numerical declare –i x x=$x*10+2 Above are for integer arithmetic operations only..

Command bc 47 An arbitrary precision calculator $ bc *10^ ^ sqrt(1000) 31 scale=4 sqrt(1000) quit An interactive calculator: * user input shown in normal font, * result shown in italics font Internal variable scale: * control the number of decimal points after decimal point

bc in command line/script 48 To evaluate an expression, simply send it using pipe to bc echo " " | bc Write a script that read a Fahrenheit degree from standard input, convert it to Celsius degree (up to 2 digits after decimal point): C=(F-32)*5/9 Base conversion, from base 10 (decimal) to base 16 (hexadecimal) echo "obase=16; ibase=10; 56" | bc

Test/Conditions 49 Any command or script, if it return 0, then test is successful if rm tmp.txt then echo file tmp.txt has been deleted else echo fail to remove file tmp.txt Fi Use ! to negate

Test Condition 50 Numerical comparisons -lt, -ne, -ge, … String comparision Pattern matching: using double brackets To test if first argument is “–” followed by a number: if [[ "$1" == -[0-9]* ]] then ….