CS465 - UNIX The Bourne Shell.

Slides:



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

More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.
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 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
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 Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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.
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.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
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.
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.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford 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.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
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.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
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.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
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.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 15 Introductory Bourne Shell Programming.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Writing Shell Scripts ─ part 1
The UNIX Shell Learning Objectives:
Writing Shell Scripts ─ part 1
Linux Shell Script Programming
Writing Shell Scripts ─ part 1
Shell Control Structures
More Shell Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

CS465 - UNIX The Bourne Shell

Bourne Shell The Bourne shell was the first widely used UNIX shell. It is: the most common the easiest to learn used most often by beginning users This week we will cover the Bourne shell and its scripting features. Bourne scripts are portable to all versions of Unix.

Shells A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom programs)

Shell Scripts A shell script is just a file containing shell commands: The first line of a shell script should be a comment of the following form: #!/bin/sh (Bourne) #!/bin/ksh (Korn) A line beginning with a hash character (#) is usually a comment. But #! is a special case: Tells the shell which interpreter to run the lines of the script under If you leave it off, commands will be interpreted by your interactive shell (in our case, ksh)

Shell Scripts Each line in a script file contains a command you could have typed at the command prompt A shell script must be readable and executable. $ chmod +x scriptname $ chmod 700 scriptname The shell script file has to be “in your path” to be executed. If the current directory (symbolized by . ) is not in your PATH, you must type ./scriptname to run it.

Simple Shell Script Example Here is a “hello world” shell script: $ ls –lF h* -rwxr-xr-x 1 smith 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh # comment lines start with # character echo "Hello world" $ hello Hello world $

Variable Names The shell provides for variable creation and usage. There are basically two general categories of variables in UNIX: Shell Variables (Assigned by the Shell) User-Created Variables (Assigned by the user)

Displaying All Shell Variables Use the set command alone to display all variables: $ set HOME=/home/small000 LOGNAME=small000 MAIL=/usr/mail/small000 PATH=/bin:/usr/bin:/etc: PPID=24244 PS1='$ ' PS2='> ' : $

User-Created Variables A user-created variable name can be any sequence of letters, digits, and the underscore character, but the first character must be a letter. You do not have to “declare” variables. To create a new variable, simply assign a value to a variable name. variableName=value

Assigning User-Created Variables Assigning values to a variables: number=25 job=engineer name="John Doe" There cannot be any spaces before or after the equal sign (=). (And there is no semi-colon!) Use double quotes to store strings containing with white space. NOTE: Internally, ALL values are stored as strings (even the numbers)

Using User-Created Variables To use a variable’s value, precede the variable name with a $ $ cat test1 #!/bin/sh number=25 name="John Doe" echo "$number $name" echo '$number $name' $ test1 25 John Doe $number $name $

Using Backquotes with Variables Remember that backquotes (graves) force command execution. Can use backquotes for variable assignment: $ cat test2 #!/bin/sh user=`whoami` numusers=`who | wc -l` echo "Hi $user! $numusers users logged on." $ test2 Hi small000! 6 users logged on. $

De-assigning User-Created Variables To eliminate a previously stored variable value: - assign a “NULL” value to the variable OR - use unset command. Examples: name= unset number

Displaying Dollar Signs Use a backslash before the $ OR Use single quotes around the dollar sign $ cat test4 #!/bin/sh cost=18.50 echo "The total is: \$$cost " echo 'Again, the total is $' $cost $ test4 Enter amount: 18.50 The total is $18.50 Again, the total is $18.50 $

Displaying Quotes Enclose single quotes in double quotes Enclose double quotes in single quotes Or backslash the desired character $ cat test5 #!/bin/sh echo "Doubles protect 'singles' " echo 'Singles protect "doubles" ' echo Single \' or double \" $ test5 Doubles protect 'singles' Singles protect "doubles" Single ' or double " $

Long Lines Within a script, a command can be "wrapped" to the next line by making the last character a backslash: Example: $ cat homedir echo "My home directory is" \ $HOME ls $HOME $ homedir My home directory is /home/smith file1 file2 file3 $

Reading User Input read is the shell's input command; it reads a line of input and stores the result in one or more variables read’s arguments are the list of variables to store the input in: If you only list one variable, the entire line of input will be stored in it If you list more than one variable, the line will be split into words, using white space as the delimiter, and each variable will get one word If there are more words than variables, the last variable will store all the remaining words

Reading User Input Use the echo command to prompt the user Then use the read command to read a line of text into a variable. Example: echo –n "Enter your full name: " read fullname -n option prompts user for data on same line as message...

User Input within Scripts $ cat test2 #!/bin/sh echo "Enter name: " read name echo "How many classes have you taken? " read number echo "$name has taken $number classes " $ test2 Enter name: Joe Smith How many classes have you taken? too many Joe Smith has taken too many classes! $

Missing User Inputs $ cat test3 #!/bin/sh echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: Joe Joe has taken classes! $ $ test3 Enter name and number of classes: 25 25 has taken classes! $

Dividing Multiple User Inputs $ cat test3 #!/bin/sh echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: Joe Smith 25 Joe has taken Smith 25 classes! $ Spaces indicate the end of input AND the last variable will read the input all the way to the end of the line.

Quoting Multiple User Inputs $ cat test3 #!/bin/sh echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: "Joe Smith" 25 "Joe has taken Smith" 25 classes! $ Quoting the input does NOT override spaces as “end of variable input” indicators.

Variable Processing Special access ${var} Same as $var, but useful when variable name is followed by text $ dept=CS $ echo UNIX is ${dept}465 UNIX is CS465 $ $ echo ${USER}\'s home dir is $HOME brown345's home dir is /home/brown345 $

Positional Parameter Shell Variables Shell variables are used to store commands and related arguments You can use the set command to assign values to shell variables When you create shell scripts, the arguments after the script name are automatically stored into shell variables

Using the set command $1 to $9 are the first nine positional parameters To use the set command: $ set Hello there class! Results: $1 = Hello $2 = there $3 = class!

Positional Parameters The output of a command can be used to set the positional shell variables: $ date Fri May 16 11:04:03 MDT 2003 $ set `date` $ echo The time is $4 The time is 11:04:03 $ $* matches all variables $1 - $9 echo $* Fri May 16 11:04:03 MDT 2003 $ $ echo Today is $3 $2 $6 Today is 16 May 2003 $

Positional Parameters within Scripts $0 is name of command used to call script $1 to $9 are first nine positional parameters $* represents all positional parameters as one long string "$*" will put single pair of double quotes around the one long string $@ will put double quotes around each individual parameter $# contains number of parameters on the command line

$0 parameter $0 holds the name of the command the user typed to invoke the shell script: $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $ ./print1 This script is called ./print1 $ ~/print1 This script is called /usr/smith/print1 $

Using Positional Parameters Example: A command to swap two files: $ cat swap #!/bin/sh mv $1 tempfile mv $2 $1 mv tempfile $2 rm tempfile $ cat notes1 contents of notes1 $ cat notes2 contents of notes2 $ swap notes1 notes2 $ cat notes1 contents of notes2 $ cat notes2 contents of notes1 $

Using $* and $# $* lists all the command line args: $ cat args1 #!/bin/sh echo The args are $* $ args1 a1 a2 a3 a4 a5 a6 a7 a8 The args are a1 a2 a3 a4 a5 a6 a7 a8 $ $# contains the number of args: $ cat args2 echo The number of args is $# $ args2 a1 a2 a3 a4 a5 a6 a7 a8 The number of args is 8

Shifting Positional Parameters Under the Bourne shell, you can only refer directly to 10 positional parameters: $0 to $9 Note: Korn shell allows ${10} etc Can “pull” parameters beyond $9 into view with shift command: shift shifts parameters $1 - $9 left, allowing parameters beyond the first nine to be accessed Arguments "falling off" the left side are no longer available, so should be saved in other variables before the shift if they will be required later in the script

Shifting Positional Parameters After a shift $2 becomes $1 $3 becomes $2 .... ${10} becomes $9 Note: $0 does NOT change shift nn, where nn is a number, has the same effect as that number of shift commands

shift Example The previous $1 becomes inaccessible each time #!/bin/sh $ cat shiftargs #!/bin/sh echo $* echo "Args: \$0 = $0, \$1 = $1, \$2 = $2" shift $ shiftargs arg1 arg2 arg3 arg1 arg2 arg3 Args: $0 = shiftarg, $1 = arg1, $2 = arg2 Args: $0 = shiftarg, $1 = arg2, $2 = arg3 Args: $0 = shiftarg, $1 = arg3, $2 = The previous $1 becomes inaccessible each time

Locality of Variables By default, all shell variables you create are only available locally within your current shell (not from within a script or command subshells) $ cat showdept echo $dept $ dept=CS $ echo $dept CS $ showdept $ All scripts are run in subshells, and dept was created in the parent shell, it is local to the parent shell and not available to the subshell.

Exporting Variables How can we make a variable available to script and command subshells? Use the export command to export the variable to all subshells: $ export varname 

Exported Variable Example $ class=465 $ export class $ cat showclass echo CS $class $ showclass CS 465 $ class was created in the parent shell and exported. So it is availabe to use by any scripts running in subshells.

Exported Variable Values When a shell variable is exported, a COPY of the variable's value is made available to subshells. If the COPY's value is changed within the subshell, that change is NOT passed back to the parent shell The change is only accessible while you are still in the subshell.

Exported Variable Example $ class=Unix $ export class $ cat changeclass echo In $0 start: $class class=Pascal echo In $0 end: $class $ changeclass In changeclass start: Unix In changeclass end: Pascal $ echo $class Unix $ class has not changed in the parent shell.

Command Exit Values When a command completes it sets an integer called the return or exit status Conventionally: zero means no error non-zero means an error of some kind The shell control flow constructs will all take a zero status to mean true and a non-zero status to mean false

Script Exit Status $? is the exit status of the last command that was run Script can end with exit n where n is the exit status or condition code of the script Example: $ cat example echo Hello exit 0 $ example Hello $ echo $? $

Invoking a Shell Script $ scriptname Run a separate copy of the shell (a subshell) to run the script (like we have been doing) $ . scriptname (dot space scriptname) Run the script within the current invocation of the shell $ shellname scriptname Run a separate copy of the named shell to run the script

Special variable - $$ $$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive). $ ps PID TTY TIME CMD 892 pts/0 0:01 csh $ echo $$ 892 $ cat pidscript #!/bin/sh echo $$ $ $ pidscript 1154 1156 $

Running Under a Different Shell $ cat newsh ps echo script PID is $$ $ csh newsh PID TTY TIME CMD 15083 pts/16 0:00 csh 15042 pts/16 0:00 ksh script PID is 15083 $ echo $$ 15042 $ Note: Overrides #! Setting in script

Running without a Subshell (under the current shell) $ echo $$ 78392 $ cat testprog echo $0 PID is $$ $ . testprog testprog PID is 78392 $

wait command The wait command will cause a parent shell to wait until all children have completed. Can wait for a specific child if optional specific child PID is specified; otherwise waits for ALL children to complete. Format: wait <child-PID>

Using wait $ cat script1 echo $0 running. . . sleep 5 echo $0 done $ cat waitscript echo $0 waiting. . . script1 & wait echo child is done echo exiting $0 $ waitscript waitscript waiting. . . (parent) script1 running. . . (child) script1 done (child) child is done (parent) exiting waitscript (parent) $