Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Notes by Michael.

Slides:



Advertisements
Similar presentations
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
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.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
Chapter 8 The Bourne Again Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes.
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 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.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
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.
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)
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.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
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.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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]
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Presented by, Mr. Satish Pise
Chapter 8 The Bourne Again Shell
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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 Weeks Updated by Xuan Guo CSC

Xuan Guo Creating/Assigning a Variable Name=value –Variable created if it does not exist –Need quotes if value contains spaces –e.g. x=”one two” Access variable with $ in front –e.g., echo $x –Also, echo ${x}3 CSC mweeks$ x="one two" mweeks$ echo $x one two mweeks$ echo ${x}3 one two3

Xuan Guo Access Variations If name not set, replace with word Variable x is set, so we see it displayed Variable x is not set, so we see “three” CSC ${name-word} $ echo ${x-three} one two $ echo ${z-three} three

Xuan Guo Access Variations If name is set, replace with word Variable x is set Variable z is not set, so nothing is printed CSC ${name+word} $ echo ${x+three} three $ echo ${z+three}

Xuan Guo Access Variations If name not set, assign with word Variable x is set, and printed Variable z is not set, so now it is set to “three” CSC ${name=word} $ echo ${x=three} one two $ echo ${z=three} three $ echo $z three

Xuan Guo Access Variations If name is set, return it Variable x is set If name is not set, print word to standard error (and quit) ‏ Variable w is not set CSC ${name?word} $ echo ${x?three} one two $ echo ${w?three} -bash: w: three

Xuan Guo Reading from Standard Input read [-p] {variable}+ Reads 1 line Examples CSC $ read v1 v2 one two $ echo $v1 one $ echo $v2 two $ read v1 v2 one two three four $ echo $v1 one $ echo $v2 two three four

Xuan Guo Example Reading Multiple Lines CSC $ cat readme.sh #!/bin/bash # read multiple lines read v1 read v2 echo "you said $v1" echo "then you said $v2" $./readme.sh one two three four five you said one two then you said three four five

Xuan Guo Exporting Variables export [name[=value]] Makes variables available in environment e.g. export x CSC $ v1="one two" $ export v1 $ sh sh-3.1$ echo $v1 one two sh-3.1$

Xuan Guo Predefined Locals CSC $ cat predefined.sh echo You passed $# parameters. echo These are: echo process ID of last background process = $! echo process ID of this shell = $$ notAcommand echo Last command returned with $? as the status. $./predefined.sh one two three four You passed 4 parameters. These are: one two three four process ID of last background process = process ID of this shell = /predefined.sh: line 7: notAcommand: command not found Last command returned with 127 as the status.

Xuan Guo Arithmetic $ expr expression Bourne shell does not directly do math Command expr evaluates expressions –Supports Multiplication (\*), Division (/), Remainder (%) ‏ Add (+), Subtract (-) ‏ Equal (=), Not Equal (!=) ‏ Less (\ ), Greater/Eq (\>=) ‏ And (\&), Or (\|) ‏ CSC

Xuan Guo expr Command expr also evaluates expressions STRING : REGEXP Anchored pattern match of REGEXP in STRING match STRING REGEXP Same as STRING : REGEXP substr STRING POS LENGTH Substring of STRING, POS counted from 1 index STRING CHARS Index in STRING where any CHARS is found, or 0 length STRING Length of STRING CSC

Xuan Guo test Command $ test expression OR just expression –Returns 0 if true –Returns nonzero if false -e file True if file Exists. -f file True if file is a regular File. -r file True if file is readable. -w file True if file is writable. -x file True if file is executable. -d file True if file is a Directory. String1 = String2 True if the strings are equal. See page 193 for a more complete list CSC

Xuan Guo Case Structure CSC case word in pattern1) Statement(s) to be executed if pattern1 matches ;; pattern2) Statement(s) to be executed if pattern2 matches ;; pattern3) Statement(s) to be executed if pattern3 matches ;; … *) Statement(s) to be executed if no pattern matches ;; esac

Xuan Guo Case Structure Example CSC $ cat testcase.sh echo "Type out the word for 1 or 2:" read v1 case $v1 in [Oo]ne)‏ echo "You entered 1" ;; [Tt]wo) echo "You entered 2" ;; *) echo "sorry“ exit 1 ;; esac $./testcase.sh Type out the word for 1 or 2: two You entered 2 $./testcase.sh Type out the word for 1 or 2: Two You entered 2 $./testcase.sh Type out the word for 1 or 2: three sorry

Xuan Guo For Loop Loop where name gets each value in word, in turn Uses if no word given End loop: break Go to next iteration: continue CSC for name in {word}* do command list done

Xuan Guo For Loop Example CSC $ cat testfor.sh for value in $params do echo param: $value done $./testfor.sh one two tree param: one param: two param: tree

Xuan Guo If.. Then CSC if [ expression ] then Statement(s) to be executed if expression is true fi if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi

Xuan Guo If.. Then CSC if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi

Xuan Guo If.. Then Example CSC $ cat testif.sh echo "enter a word: " read v1 if [ -e $v1 ] then echo "A file by that name exists." else echo "No file by that name exists." fi $./testif.sh enter a word: dummy A file by that name exists. $./testif.sh enter a word: two No file by that name exists.

Xuan Guo Responding to a Signal $ trap command {signal}+ Executes command when signal is received Example: $ trap 'echo CTRL-C; exit 1' 2 –When user types Control-C (signal 2) ‏ –Print “CTRL-C” –Exit with status 1 CSC

Xuan Guo Until.. do.. done End loop: break Go to next iteration: continue CSC until command do Statement(s) to be executed until command is true done

Xuan Guo Until.. do.. done Example CSC $ cat testuntil.sh x=1 until [ $x -gt 3 ] do echo x = $x x=`expr $x + 1` done $./testuntil.sh x = 1 x = 2 x = 3

Xuan Guo While Loop End loop: break Go to next iteration: continue CSC while command do Statement(s) to be executed if command is true done

Xuan Guo While Loop Example CSC $ cat testwhile.sh x=1 while [ $x -lt 4 ] do echo x = ${x}, less than four x=`expr $x + 1` done $./testwhile.sh x = 1, less than four x = 2, less than four x = 3, less than four

Xuan Guo Review Variable assignment and access Reading standard input Arithmetic and pattern matching (expr) ‏ Control structures (case, for, if, while, until) ‏ CSC