1 © 2000 John Urrutia. All rights reserved. Session 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.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
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
Linux+ Guide to Linux Certification, Second Edition
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
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 8 Shell.
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.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
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.
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
1 © 2012 John Urrutia. All rights reserved. Chapter 8 The Bourne Again Shell Halleluiah!
(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.
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,
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.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
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]
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
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.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Introduction to Shells
Chapter 8 The Bourne Again Shell Halleluiah!.
Writing Shell Scripts ─ part 3
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.
Linux Shell Script Programming
Chapter 5 Bourne Shells Scripts
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell

2 © 2000 John Urrutia. All rights reserved. Overview Shell fundamentals Streams revisited Processes and Subshells Shell script basics Examples

3 © 2000 John Urrutia. All rights reserved. Shell fundamentals Command separation Command grouping Job control (limited in Bourne)

4 © 2000 John Urrutia. All rights reserved. Command Separation Newline (nl)X’0D0A’  ends command and initiates execution Semicolon (;)  just separates commands Backslash (\)X’5C0D0A’  at end of line and before you type return Allows command to be continued

5 © 2000 John Urrutia. All rights reserved. Command Separation ( cont. ) Ampersand (&)  execute task in the background Pipe ( | )  pipe

6 © 2000 John Urrutia. All rights reserved. Command Grouping Parenthesis used to group commands  causes Shell to create a subshell  additional processes are created as required when the subshell runs the commands within the parenthesis  (ls ; date; w) ; more  (ls ; date; w) | more

7 © 2000 John Urrutia. All rights reserved. Job Control Ampersand &  tells the Operating system to run the job in the background  User will still be able to interact with the shell  Pure Bourne shell has limited ability. Can not deal with a specific job it has put into background after initial creation. C shell much better.

8 © 2000 John Urrutia. All rights reserved. Job Control (continued) First two jobs in background, c in foreground  a & b & c Entire sequence put into background  a | b | c & All three jobs executed in background  a & b & c &

9 © 2000 John Urrutia. All rights reserved. Streams Revisited Three streams  standard in < or 0<  standard out > or 1>  standard error 2>

10 © 2000 John Urrutia. All rights reserved. Streams standard I/O cat x y  if x exists and y does not, contents of x and error message due to y are sent to terminal  both standard out and standard error default to the terminal

11 © 2000 John Urrutia. All rights reserved. Streams Continued cat x y 2>error.log  standard error is sent to a file to separate it from the expected results of the command cat x y 2>>newfile 1>>newfile  standard out is redirected to newfile

12 © 2000 John Urrutia. All rights reserved. Processes and Subshells A process is the execution of a command  login to UNIX  execution of a UNIX utility  execution of a shell script creates a new process  script commands each start a new process Process structure is hierarchical

13 © 2000 John Urrutia. All rights reserved. Process Flow User logs in: shell process is created User issues command, enters return  Shell creates a subshell  child process is forked or spawned  unless the command is built into the bourne shell process

14 © 2000 John Urrutia. All rights reserved. Process flow (cont.)  Subshell is a clone of the parent shell  Subshell tries to exec the command  If program, the program runs  If shell script, exec fails and subshell interprets commands.

15 © 2000 John Urrutia. All rights reserved. Process Flow Parent Shell sleeps until child shell finishes  (unless job was executed in background) Variables that are used in a parent can be sent to a child, but the reverse is not true.

16 © 2000 John Urrutia. All rights reserved. Process Flow Shell Scripts need to have execute permission if you just type the file name as you would a command Alternative (new subshell): sh file Alternative (current shell): file

17 © 2000 John Urrutia. All rights reserved. Shell Script Basics Creating a Shell Script Keyword Shell Variables User Created Variables Readonly Shell Variables

18 © 2000 John Urrutia. All rights reserved. Creating a Shell Script Use a text editor like vi First line should start with #! Followed by the absolute pathname of the shell that is to interpret the script. (default is C shell)  #!/bin/sh Lines which start with a # are comments  (except the special line mentioned above)

19 © 2000 John Urrutia. All rights reserved. Keyword Shell Variables HOME (contains login directory) PATH (Used by shell to locate commands you type in)  /usr/bin:/usr/sbin:/class/n01/bin: MAIL (contains name of central post office file for your mail) PS1, PS2 (primary and secondary prompts)

20 © 2000 John Urrutia. All rights reserved. Keyword Variables (continued) CDPATH  like PATH, except used by cd command TZ  timezone IFS  Internal field separator. Blanks and tabs are default

21 © 2000 John Urrutia. All rights reserved. User Created Variables Create a variable by giving a name of your choice and an optional value  name=charlie  NO blanks around the equal sign!! Remove variable  unset name Keep variable but remove value  name=

22 © 2000 John Urrutia. All rights reserved. Readonly Shell Variables Two types: user created variable that has been declared to be readonly  readonly name  keeps later statements from changing the value Special Shell variables  Positional Variables  Miscellanous variables

23 © 2000 John Urrutia. All rights reserved. Positional Variables $1 through $9  Keep the first nine arguments entered after the name of the shell script myscript aardvark dog cat  $1 will contain the word aardvark  $2 will contain the word dog  $3 will contain the word cat

24 © 2000 John Urrutia. All rights reserved. Miscellaneous Variables $* contains all arguments (not just the first one) similar to $*, except that it internally quotes each argument. $# total number of arguments $$ process id number (pid) of current process

25 © 2000 John Urrutia. All rights reserved. Shift command Promotes values of each positional variable to the left. Contents of $1 go to ‘bit bucket’ Contents of $2 go to $1 Contents of $3 go to $2 etc (etcetera, not etci)

26 © 2000 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

27 © 2000 John Urrutia. All rights reserved. Boolean Operators And  -a Or  -o Not  !

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

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

30 © 2000 John Urrutia. All rights reserved. not Boolean Truth Tables Expr ! FalseTrue FalseTrue

31 © 2000 John Urrutia. All rights reserved. test ing, test ing, test ing test expression or [ … ]  Evaluates the expression and returns a Boolean true or false.  expression can be simple or compound

32 © 2000 John Urrutia. All rights reserved. Test Criteria String expresions  Is null or Length is zero (-z) or Length is > 0 (-n)  string1 = or != string2

33 © 2000 John Urrutia. All rights reserved. Test Criteria (cont.) Filename expression  File exists and has specific attributes  -b lock -c haracter -d irectory  -size – file has data (length >0)

34 © 2000 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

35 © 2000 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

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

37 © 2000 John Urrutia. All rights reserved. Bourne - elif elif  “else if” structure  Linear in nature

38 © 2000 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

39 © 2000 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

40 © 2000 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

41 © 2000 John Urrutia. All rights reserved. Examples: Myscript #!/bin/sh # # # Script Name: myscript # Written by: Charlie Verboom # Date: 9/19/97 # Purpose: demonstrate the use of #variables and displays upon terminal # Arguments: 3 arguments used to set #variables to be displayed # #

42 © 2000 John Urrutia. All rights reserved. Myscript (continued) echo the first argument is $1 echo the second argument is $2 echo the third argument is $3 echo $# arguments were typed in echo the current process number is $$ echo script executed successfully> log.$$

43 © 2000 John Urrutia. All rights reserved. Additions to myscript echo Please type in your name read name echo You typed in $name echo Please type in your age age=`head -1` echo Your age is $age