Chapter 5 The Bourne 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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
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.
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.
Lab 8 Shell Script Reference:
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.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford 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.
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.
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.
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.
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 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.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
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
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]
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.
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.
Chapter 9 Shell Programming
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)
UNIX Reference Sheets CSE 2031 Fall 2010.
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 8 The Bourne Again Shell
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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

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 mweeks$ x="one two" mweeks$ echo $x one two mweeks$ echo ${x}3 one two3

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” ${name-word} $ echo ${x-three} one two $ echo ${z-three} three

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

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” ${name=word} $ echo ${x=three} one two $ echo ${z=three} three $ echo $z

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 ${name?word} $ echo ${x?three} one two $ echo ${w?three} -bash: w: three

Reading from Standard Input Command read Reads 1 line Examples $ 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

Example Reading Multiple Lines $ 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

Exporting Variables Command export Makes variables available in environment e.g. export x $ v1="one two" $ export v1 $ sh sh-3.1$ echo $v1 one two sh-3.1$

Predefined Locals $ 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 = 21712 ./predefined.sh: line 7: notAcommand: command not found Last command returned with 127 as the status.

Arithmetic Bourne shell does not directly do math Command expr evaluates expressions Supports Multiplication (\*), Division (/), Remainder (%)‏ Add (+), Subtract (-)‏ Equal (=), Not Equal (!=)‏ Less (<), Less/Eq (<=), Greater (>), Greater/Eq (>=)‏ And (&), Or (|)‏ Index (locate substring)‏ Match

expr Command expr also evaluates expressions Locate substring Index string charList Test for a match (returns 0 or length)‏ Match string regExp Get substring Substr string start length Length of string Length string

test Command Command test expression OR just expression Examples Returns 0 if true Returns nonzero if false Examples File exists: -e filename File has >=1 size: -s filename Strings are equal: str1 = str2 See page 193 for a more complete list

Case Structure case expression in pattern)‏ list ;; pattern2)‏ list2 ... *) # default list_n esac

Case Structure Example $ 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" esac $ ./testcase.sh Type out the word for 1 or 2: two You entered 2 Two three sorry

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 for name [in {word}*] do command list done

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

If .. Then Execute list1 If last command succeeds, do list2 if list1 then list2 elif list3 list4 else list5 fi Execute list1 If last command succeeds, do list2 If last command (of list1) fails, try list3, etc.

If .. Then Example $ 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. two No file by that name exists.

Responding to a Signal Command 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

Until .. do .. done Keep doing list1 until its last line works Otherwise, do commands in list2 End loop: break Go to next iteration: continue until list1 do list2 done

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

While Loop Execute list2 as long as the last command of list1 succeeds End loop: break Go to next iteration: continue while list1 do list2 done

While Loop Example $ 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

null Command The null command does nothing Used in case statements To handle a particular case by doing nothing

Grouping Commands Use parentheses to group commands Executed as a subshell Redirect-able as a group Pipe-able as a group

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