CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.

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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
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,
CISC3130, Spring 2013 Dr. Zhang 1 Bash Scripting: Advanced Topics.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
An Introduction to Unix Shell Scripting
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.
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 Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
CS465 - UNIX The Bourne 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)
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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
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]
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.
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.
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"
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
Lecture 9 Shell Programming – Command substitution
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
Shell Control Structures
Shell Control Structures
Introduction to Bash Programming, part 3
Presentation transcript:

CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures

Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loops; break, continue case construct 2

Bash special characters 3 Globbing: filename expansion E.g., ls *.cpp, rm *.o, etc. Shell expands filename patterns or templates containing special characters Can be turned off: set –f, shopt Shell special characters *, ?, [,], |, >, > Quotations: single quote, double quote, back quote

SHELL Variables 4 Different types of variables Environment variables: HOME, PATH, PS1, PS2 … Parameter variables: $1, $*, … User defined variables: student, file, x,.. Recall we set PATH variable in lab1 … PATH=$PATH:.:~zhang/bin export PATH # make PATH an environment variable, which is inherited by all subprocesses Example: ~]$ x=1 ~]$ export x ~]$ bash ~]$ echo $x 1

Shell parameter variables 5 If your script is invoked with parameters, shell sets parameter variables $#: number of parameters $0, $1, …: command/script name, first/second parameters $*, Represents all command-line arguments at once. They can be used to pass command-line arguments to a program being run by a script or function.

Shell parameter variables (2) 6 "$*“: all command-line arguments as a single string. Equivalent to "$1$2 …". The first character of $IFS is used as separator for different values, printf "The arguments were %s\n" "$*" all command-line arguments as separate, individual strings. Equivalent to "$1" "$2" …. best way to pass arguments on to another program, since it preserves any whitespace embedded within each argument. lpr Print each

Set command 7 set command, a shell builtin command display current variables, “set” set shell options, “set –f”, “set –n”.. set position parameters (no options), ~]$ set Hello world; echo $1, $2 Hello world Combine command substitution and set command ~]$ set `who am i` ~]$ echo Welcome, $1! You logged in from $5. ~]$ set `date` ~]$ echo The year is $6 The year is 2013

$ set -- hello "hi there" greetings Set new positional parameters $ echo there are $# total arguments ##Print the count there are 3 total arguments $ for i in $* #Loop over arguments individually > do echo i is $i > done i is hello Note that embedded whitespace was lost i is hi i is there i is greetings 8

$ set -- hello "hi there" greetings Set new positional parameters $ for i in # Without quotes, $* and are the same > do echo i is $i > done i is hello i is hi i is there i is greetings $ for i in "$*" # With quotes, $* is one string > do echo i is $i > done i is hello hi there greetings 9

$ for i in With quotes, preserves exact argument values > do echo i is $i > done i is hello i is hi there i is greetings $ 10

User defined variables 11 Declare variables by using them, e.g., ~]$ for letter in a b c > do > echo "Letter $letter" > done Letter a Letter b Letter c

Read variable value from input 12 ~]$ read timeofday Morning ~]$ echo Good $timeofday! Good Morning! ~]$ read greeting Good morning# don’t need to quote ~]$ echo $greeting ~]$ Good morning ~]$ echo “$greeting” is \$greeting. What will be the output ?

Command Substitution 13 Command substitution: substitute output of a command (a string) into another context, i.e., command Syntax: enclose command using backquote, or $() As argument for another command rm `ls *.o` ## same as rm *.o To set a variable time1=$(date); echo $times1 ## set the output of date to variable times To be used in “for” construct for file in `ls *`; do ## for every file in current directory, do something … done

Variable’s default type: string 14 Variables values are stored as strings ~]$ number=7+5 ~]$ echo $number 7+5 ~]$ x=2; y=3 ~]$ z1=x+y; z2=$x+$y ~]$ echo $z1 $z2 # What will be the output?

Arithmetic Evaluation 15 arithmetic expression: ~]$ x=1 ~]$ x=$[$x+1] ## x now has value of 2 ~]$ y=$((2*$x+16)) ## y now has value of 20 Note: spaces around operators optional Complex expressions supported No spaces around equals sign, as with any bash variable assignment

16 From highest precedence to lowest Relational operators (<, <=, …) produces a numeric result that acts as a truth value

Arithmetic Evaluation (2) 17 Or use command expr (less efficient) ~]$ x=`expr $x + 1` # increment x by 1 ~]$ x=$(expr $x * 2 ) Recall: two diff. syntaxes for command substitution spaces before and after operators, why? No spaces around equals sign, as with any bash variable assignment E.g., convert 38 F to Celsius degree expr \( \) \* 5 / 9 ## need to escape *, (, ) echo $(((38-32)*5/9))

Declare variable One can explicitly declare a variable: declare OPTION(s) VARIABLE=value Option -a: variable is an array -f : use function names only -i: variable is to be treated as an integer; arithmetic evaluation is performed when variable is assigned a value -l: when assigned a value, all upper-case characters are converted to lower-case. -r Make names readonly. These names cannot then be assigned values by subsequent assignment statements or unset. … 18

Example of numerical variable ~]$ declare -i x ~]$ x=10 ~]$ x=x+1 ~]$ echo $x 11 ~]$ read x 30 ~]$ x=x*2 ~]$ echo $x 60 ~]$ 19

A bash based calculator 20 First, let’s implement addition echo ″ calculate x+y ″ echo –n ″ x= ″ ## -n option asks echo not to print newline read x echo –n ″ y= ″ read y echo ″ x+y= ″ $(($x + $y))

A bash based calculator 21 to implement subtraction, multiplication and division echo –n ″ x= ″ read x echo –n “operator(+,-,*,/): ” read op echo –n ″ y= ″ read y ## if the operator is +: echo $ x $op $y = $(($x + $y)) ## if operator is -: echo $ x $op $y = $(($x - $y)) … ## How to test condition, and choose different course of actions?

Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loops; break, continue case structure 22

Control Structures & Conditions 23 Control structures in bash if … then … fi if … then … else … fi if … then …elif … else … fi for … in … do … done while … do … done until … do … done case … in … esac break, continue Conditions (tests): used in if structures, while, until structures, similar to boolean expression in C/C++ Dots shown in red are to be replaced with conditions

Conditions in shell Exit status of a command, script or shell function, e.g., if diff file1 file2 >& /dev/null ## if file1 and file2 are the same … test command: used to perform a variety of test, e.g., test file attributes, compares strings and numbers. if test –e tmp.o ## if there is file named test.o … Compound condition: combine above using ! (negation), && (and), || (or) if !grep pattern myfile > /dev/null … 24

Exit status command/script/function Exit Status: every command (built-in, external, or shell function) returns a small integer value when it exits, to the program invoked it. Convention: command/program returns a zero when it succeeds and some other status when it fails How to return value from shell script? exit command, syntax exit [exit-value] Return an exit status from a shell script to its caller If exit-value is not given, exit status of last command executed will be returned. If this is what you want, do so explicitly using exit $? ? A special variable stores exit status of previous command. 25

26

test command Used to perform a variety of test in shell scripts, e.g., test file attributes, compares strings and numbers. Provide no regular output, used exclusively for its exit status Syntax: test expression [ expression ] Note: space between [, ] and expression … 27

28

29

30 Numerical tests work on integers only.

Test status of file: file conditionals File conditionals: unary expressions examining status of a file if test –e /etc/.bashrc ## same as if [ -e /etc/.bashrc ] ## do something if /etc/.bashrc exists then ## do something else if it doesn’t fi More testing -d file: true if the file is a directory -e file: true if the file exists -f file: true if the file is a regular file. -s file: true if the file has nonzero size 31

numerical comparison: example echo -n "Enter your age: " read age if [ $age -lt 18 ] then echo "You need to be 18 or older to apply for account" else echo "Choose your preferred account name" fi 32

Numeric Comparison: check arguments 33 Often in shell script, need to check # of command line arguments: #! /bin/bash ## this script … ## check arguments … if [ $# -lt 2 ] ## less than 2 command lines arguments are specified … then echo “usage: $0 file1 file2” exit 1 ## 0 means succeed, otherwise failed fi …

string comparison 34 Exercise #1: Write a script that read from standard input a string, and check if it’s the same as your secret password “secret”; if yes, print out “welcome!”; print out “Go away” if not.

Logical NOT, AND, and OR if !grep pattern myfile > /dev/null ## Negation then # pattern not here fi if grep pattern1 file && grep pattern2 file then ##contain both pattern Fi if grep pattern1 file || grep pattern2 file then ## contain at least one pattern fi 35

Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loops; break, continue Case structure 36

Selection in shell 37 if condition1 then commands1 elif condition2 commands2 else commands3 fi case … in … esac Can be repeated… Implementing branch: change flow of control based on conditions if condition then commands fi if condition then commands1 else commands2 fi

if control structure Single-line Syntax if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi Multi-line Syntax if TEST-COMMANDS then CONSEQUENT-COMMANDS fi Recall: command line terminates with NEWLINE, ;, &. 38

Testing in interactive shell 39 Write a script that read from standard input a string, and check if it’s the same as your secret password “secret”; if yes, print out “welcome!”; print out “Go away” if not. test it out in interactive shell: ~]$ read string secret ~]$ if [ $string == "secret" ]; then echo "Welcome"; else echo "Go away"; fi Welcome

~]$ cat checkps #!/bin/bash echo -n "Enter your password: " read password if [ $password == "secret" ] then echo "Welcome!" else echo "Go away!" fi 40 ~]$ checkps Enter your password: secret Welcome! ~]$ checkps Enter your password: guess Go away! ~]$ Important note: When spanning multiple lines, do not need the ;

if … then … elif … then …else if [[ "$op" == "+" ]] then result=$(($x + $y)) echo $x $op $y = $result elif [[ "$op" == "-" ]] then result=$(($x - $y)) echo $x $op $y = $result 41 elif [[ "$op" == "*" ]] then result=$(($x * $y)) echo $x \* $y = $result elif [[ "$op" == "/" ]] then result=$(($x / $y)) echo $x $op $y = $result else echo "Unknow operator $op" fi

if… statements can be nested #!/bin/bash # This script will test if we're in a leap year or not. year=`date +%Y` if [ $[$year % 400] -eq 0 ]; then echo "This is a leap year. February has 29 days." elif [ $[$year % 4] -eq 0 ]; then if [ $[$year % 100] -ne 0 ]; then echo "This is a leap year, February has 29 days." else echo "This is not a leap year. February has 28 days." fi else echo "This is not a leap year. February has 28 days." fi 42

Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loop; break, continue case structure 43

Loop structure: while loop 44 Multi-line Syntax: while condition do commands done Single-line Syntax (useful in interactive mode) while condition; do commands; done Note: condition and commands terminated with ;

while loop 45 declare –i i=1 ## an integer variable I while [ $i -le 10 ] do echo "loop $i" i=i+1 ## can use this since i is integer done If i is not declared as integer … i=$(($i+1)) i=$[$i+1]

~]$ cat checkps #!/bin/bash echo -n "Enter your password: " read password if [ $password == "secret" ] then echo "Welcome!" else echo "Go away!" fi 46 How to modify this to allow user to try until the password matches?

~]$ cat checkps #!/bin/bash while test $password != "secret" ## as long as password is not same as “secret” do echo -n "Enter your password: " read password done echo "Welcome!” 47 What if we give the user at most 3 tries? 1. use a variable to keep track number of tries … 2. modify condition …

until loop Tests for a condition, and keeps looping as long as that condition is false (opposite of while loop). until condition do command(s)... done e.g.: $until [ $passwd == "secret" ] ; do echo -n "Try again: "; read passwd; done Try again: guess Try again: password Try again: secret $ 48

Wait for a user to log in # wait for a user to log in, check every 30 seconds printf “Enter username:” read user until who | grep “$user” > /dev/null do sleep 30 done 49 Can you convert it to while loop?

Lazy evaluation Recall binary operators &&, || && has higher precendance than || Lazy evaluation: command1 && command2 //if command1 fails, command2 will not be executed command1 || command2 //if command1 succeeds, command2 will not be executed E.g., test 5 -eq 5 && echo Yes || echo No if [ test 5 –eq 5 ]; then echo yes; else echo No 50

Lazy evaluation(2) Rewrite the following using lazy evaluation if test –f /etc/resolv.conf then echo "File /etc/resolv.conf found." else echo "File /etc/resolv.conf not found.“ fi test -f /etc/resolv.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found.“ 51

For loops For loop: iterates over a list of objects, executing loop body for each individual object in the list for variable in a_list_of_objects do ## do something on $variable commands.. done e.g., for filename in lab1.cpp lab2.cpp lab3.cpp do indent $filename done 52

For loop: example 53 Save student account name in a file, all.txt for student in `cat all.txt` do echo “Hello, $student” > greeting_$student grep $student student_rec.txt >>greeting_$student write $student < greeting_$student rm greeting_$student done How to avoid using temporary file, greeting_$student ?

Using for loop Use for loop to print out 2’s power Command seq: print out a sequence of number #!/bin/bash # print out 2’s powers for a in `seq 1 10` do echo 2^$a=$((2**a)) done Note: ** is the exponent operator

Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loop; break, continue case structure 55

case construct: branching case construct is analogus to switch in C/C++. case "$variable" in shellpattern1 ) command... ;; shellpattern2) command … ;; shell pattern n) command... ;; esac 56 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 57

#!/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 58 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 59

Summary Variables: environment, user-defined, parameter variable (i.e., positional parameter) Default type is string Use declare to declare a variable of other type Variable asssignment: counter=1 set command can be used to set positional parameter Arithmetic operations: +,-,*,/,**, %, … x=$[$x+1] ## or x=$(($x+1)) x=x+1 ## if x is numerial type, declare –i x 60

Summary: bash test Any command/function/script that return 0 when succeed, non-zero if fail if ! cp file1.txt file1.txt.bak then echo “failed to back up file1.txt” fi Test command: use test, or [ ], or [[ ]] File testing: if [ -d $file ] ## if $file is a directory String comparison: if [ $input = “secret” ] Numerical comparison: if [ $i –eq 10 ] Logic operations: [[ $age –le 50 && $age –gt 18 ]] 61

Summary: control structure They can be nested (just like programming languages) They are commands, so, standard input/output redirection and pipeline can be applied to whole command e.g., while read line do if [[ "$line" =~ "if" ]] ## matching, regular expression style then echo Found in $line fi done < testleap