1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

Slides:



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

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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
Bourne Shell1-1 Borne Shell  Background m Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. m Basic shell provided with many commercial.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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 Control Structures CSE 2031 Fall August 2015.
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.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
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]
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
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.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
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.
Shell Control Structures
CSC 352– Unix Programming, Fall 2012
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
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
Presented by, Mr. Satish Pise
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
More Shell Programming
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell

2 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions

3 © 2001 John Urrutia. All rights reserved. Control Structures Selections  if … then … fi  One-Way Selection  if … then … else … fi  Two-Way Selection  if … then … elif … fi  Multi -Way Selection The above builtins must be on separate lines or separated by ;

4 © 2001 John Urrutia. All rights reserved. Control Structures Syntax: if test expression or [ expression ] then command(s) fi … Must Evaluate True or False Boolean Expression Executes if expression is true test expression or [ expression ] if then Executes if expression is false fi

5 © 2001 John Urrutia. All rights reserved. Boolean Expressions Algebra created by George Boole Always evaluates to a binary state  Generally:  1 is TRUE  0 is FALSE

6 © 2001 John Urrutia. All rights reserved. Boolean or Logical Operators And  -a Or  -o Not  !

7 © 2001 John Urrutia. All rights reserved. and Boolean Truth Tables Expr 1Expr 2 -a True FalseTrue False TrueFalse

8 © 2001 John Urrutia. All rights reserved. or Boolean Truth Tables True FalseTrue FalseTrue False Expr 1Expr 2 -o

9 © 2001 John Urrutia. All rights reserved. not Boolean Truth Tables Expr ! FalseTrue FalseTrue

10 © 2001 John Urrutia. All rights reserved. test ing, test ing, test ing test expression or [ expression ]  Evaluates the expression and returns a Boolean true or false.  expression can be simple or compound  Criteria:  String  Integer  Filename  File descriptor number

11 © 2001 John Urrutia. All rights reserved. Test Criteria String expressions – applies to string variables or literals  Is null or Length is zero (-z) or Length is > 0 (-n)  string1 = or != string2  If you are comparing literals quote them

12 © 2001 John Urrutia. All rights reserved. Testing Strings …]$ if test "not" != "equal";then echo Not Equal;fi Not Equal …]$ if [ "not" = "equal“ ];then echo Equal;fi …]$

13 © 2001 John Urrutia. All rights reserved. Test Criteria (cont.) Integer relationship  -gt greater than  -ge greater than or equal to  -eq equal to  -ne not equal to  -le less than or equal to  -lt less than

14 © 2001 John Urrutia. All rights reserved. Testing Integers …]$ if test 123 -ne 234;then echo Not Equal;fi Not Equal …]$ if [ 123 -eq 234 ];then echo Equal;fi …]$

15 © 2001 John Urrutia. All rights reserved. Test Criteria (cont.) Filename expression – file exists and has specific attributes  drewsx  -d irectory  -r eadable  -e xists  -w ritable  -s ize – file has data (length >0)  -x executable

16 © 2001 John Urrutia. All rights reserved. Testing Files …]$ if [ ! -e “TestFile” ];then echo Not found;fi Not found …]$ …]$ if test -e “TestFile”;then echo Found it;fi …]$

17 © 2001 John Urrutia. All rights reserved. Bourne - if, then if  Establishes a control structure  Followed by a test command then  Commands executed if test is true

18 © 2001 John Urrutia. All rights reserved. Bourne – else, fi else  Commands executed if test is false fi  Terminates this if statement

19 © 2001 John Urrutia. All rights reserved. if …then … else … fi Start Test 1 False True

20 © 2001 John Urrutia. All rights reserved. Bourne - elif elif  “else if” structure  Linear in nature  Similar to the case or switch in selection but completely different in execution.

21 © 2001 John Urrutia. All rights reserved. if …then … elif … else … fi Start Test 2 False True Test 1 True if then elifelse

22 © 2001 John Urrutia. All rights reserved. if … then … [ else or elif ] … fi 2 structures  if […] then cmds if […] then cmds else cmds else cmds fi  if […] then cmds elif […] then cmds else cmds fi

23 © 2001 John Urrutia. All rights reserved. if [ $a –gt 2 ] then if [ $a –gt 3 ] then echo value is 4 else echo value is 3 fi else if [ $a –gt 1 ] then echo value is 2 else echo value is 1 fi if [ $a –gt 3 ] then echo value is 4 elif [ $a –gt 2 ] then echo value is 3 elif [ $a –gt 1 ] then echo value is 2 else echo value is 1 fi

24 © 2001 John Urrutia. All rights reserved. Control Structures Iterations  for … in  for

25 © 2001 John Urrutia. All rights reserved. for… and for …in Assign element Do commands More elements Done

26 © 2001 John Urrutia. All rights reserved. Here we go loop-de-loop for x in a b c  do cmd cmd cmd done Substitutes the x variable with each in variable and executes the commands between do and done

27 © 2001 John Urrutia. All rights reserved. California the land of … echo “California the land of – “ for info in fruits nuts cheese wine do echo –n “ $info” done echo –e “\n And of course Hollywood”

28 © 2001 John Urrutia. All rights reserved. Here we go loop-de-loop for x do cmd cmd cmd done Substitutes the x with command line arguments and executes the commands between do and done

29 © 2001 John Urrutia. All rights reserved. California the land of … echo “California the land of – “ for arg in fruits nuts cheese wine do echo –n “ $arg” done echo “ And of course Hollywood” This modification causes the script to display all command line arguments again

30 © 2001 John Urrutia. All rights reserved. Control Structures Iterations  while  until

31 © 2001 John Urrutia. All rights reserved. While I’m here – Until it’s gone while […] (initial state true - go) until […] (initial state true – stop)  do cmd cmd cmd done

32 © 2001 John Urrutia. All rights reserved. While you were out… cold While Test do commands Done Pre-Test Loop

33 © 2001 John Urrutia. All rights reserved. Until … The cows come home until Test do commands Done Pre-Test Loop

34 © 2001 John Urrutia. All rights reserved. Continue to Break continue and break  Used in for while and until structures  break – terminates the structure  continue – terminates this iteration

35 © 2001 John Urrutia. All rights reserved. continue for idx in do if [ $idx –le 3 ] ; then echo “continue” continue fi echo $idx done Continues three times Echoes 4 – 10

36 © 2001 John Urrutia. All rights reserved. break for idx in do if [ $idx –gt 6 ] ; then echo “break” break fi echo $idx done Stops for loop after sixth iteration Echoes 1 – 6

37 © 2001 John Urrutia. All rights reserved. Bourne – case The case command tests for multiple values in a variable Allows the use of “wild cards” First match wins

38 © 2001 John Urrutia. All rights reserved. Get on this case case $var in test-var1 ) cmd1;cmd2 cmd3 ;; test-var2 ) cmds ;;  ) cmds ;; esac

39 © 2001 John Urrutia. All rights reserved. case “$variable” in A|a ) echo Entered A or a ;; [0-9] ) echo Entered number ;; ?z* ) echo Entered z in 2 nd position ;; esac

40 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions

41 © 2001 John Urrutia. All rights reserved. Here boy << The Here Document  Allows in-stream data to feed a script.  Must start with << and a data delimiter character  Data delimiter character on line by itself - terminates

42 © 2001 John Urrutia. All rights reserved. Here in the script #!/bin/bash grep –i “$1” <<+ AlexJune 22 BabsFebruary 3 LeroyJanuary 20 + echo “All Done now”

43 © 2001 John Urrutia. All rights reserved. Bundle script #!/bin/bash echo “# To unbundle sh this file for i do echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i” done

44 © 2001 John Urrutia. All rights reserved. Bundle script -- output # To unbundle sh this file echo 1 st filename 1>&2 cat > 1 st filename <<‘End of 1 st filename ’ Contents of 1 st filename.. End of 1 st filename

45 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions

46 © 2001 John Urrutia. All rights reserved. Expanding Null or Unset Vars. ${ name } – expands to the value of the variable. If variable is null or not set the expansion produces a null string (\0) ${ name :- default } – expands to the value of the variable or the default value if null.

47 © 2001 John Urrutia. All rights reserved. Expanding Null or Unset Vars. ${ name := default } – expands to the value of the variable or the default value if null and sets the variable to the default value. : ${ name := default } – if null sets the variable to the default value. ${ name :? message } – if null or unset sends message to stdout.

48 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions

49 © 2001 John Urrutia. All rights reserved. The execute command The exec command  Executes scripts or programs  Runs under the same PID  Provides access to the original environment variables  Terminates current process.

50 © 2001 John Urrutia. All rights reserved. The execute command The exec command  Can be used to redirect stdin, stdout and stderr from inside a script. exec < infile exec > outfile 2> errfile

51 © 2001 John Urrutia. All rights reserved. I feel like trap – let’s kill a PID trap ’ action ’ [ signal number ]  64 signals available 0 – 63  1 – Phone Hangup  2 – Pressing an interrupt key ( ^c)  3 – Pressing the quit key (Ctrl+Shft+| or \)  9 – Kill command (without mercy ^d)  15 – Terminate process (kill command)  20 – Job control STOP key (^z)

52 © 2001 John Urrutia. All rights reserved. I feel like trap – let’s kill a PID kill - [ signal number ] [ pid ]  Sends a signal to one or more PIDs  By default sends signal 15 (SIGTERM)  Using signal 9 is un-trappable Get-out … NOW!  kill –9 0

53 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions

54 © 2001 John Urrutia. All rights reserved. Play that Function music Functions are like pre-processed scripts. Can be stored in.profile or in script files. Generally used by more than 1 script.

55 © 2001 John Urrutia. All rights reserved. Function declaration Function-name () { commands } Functions can be called with arguments and process them the same way a command line would.