Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 10 Dr. Jerry Shiao, Silicon Valley University.

Slides:



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

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.
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
Chapter 15 Mr. Mohammad Smirat SHELL PROGRAMMING (Bourne Shell)
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
Chapter 15 Introductory Bash Programming
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.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
An Introduction to Unix Shell Scripting
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.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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)
Linux+ Guide to Linux Certification, Second Edition
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 15 Introductory Bourne Shell Programming.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Implementation of a simple shell, xssh
Implementation of a simple shell, xssh (Section 1 version)
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Implementation of a simple shell, xssh
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
CHAPTER 8 (skip , ) CHAPTER 10
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 10 Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 2 Summer 2015 Introduction UNIX/Linux Course Section 10 Bourne Shell Programming  Bourne Shell: More than Command Interpreter Programming Language used to perform tasks that cannot be performed by one or sequence of commands. Nonsequential execution of commands (if, then, elif, else). Repeated execution of commands (for, while, until).  Running Bourne Shell Script  Shell Variables Reading and Writing Shell Variables Command Substitution Exporting Environment Resetting Variables Creating Read-Only User-Defined Variables Reading From Standard Input  Passing Arguments to Shell Scripts

SILICON VALLEY UNIVERSITY CONFIDENTIAL 3 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Shells are Command Interpreter. Shells also can execute scripts (UNIX file) consisting of shell commands and program control flows.  Program Control Flows: allow nonsequential execution of commands in a shell script and repeated execution of block of commands.  Similar to C programming language. Shell Variables:  Read/Write storage place used as temporary storage when executing shell script.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 4 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Execution of a Bourne Shell:  Add execute permission to access permission for file. $ chmod u+x script_file $ script_file  Execute the script file as parameter to bash shell. $ /bin/sh script_file  Specify the shell in the script file. File: script_file #!/bin/sh Current shell executing the file finds “#!” as the first line, the rest of the line is the absolute path for the shell to be used to execute the file. NOTE: For C shell, use “:” instead of “#!”. C shell will use Bourne shell.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 5 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Shell Variables  Main memory location that is given a name.  Name: First character is letter or underscore. Name has digits, letters, and underscore.  Value: Always string of characters Shell Environment Variables  Customize environment of the current shell and subshells.  Set by System Administrator or user to create common environment for all users.  Login Session: ~/.bash_profile or ~/.profile  Non-login Session: ~/.bashrc User-Defined Variables  Temporary value used in the current shell whose values change when shell executes.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 6 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Writeable Bourne Shell Environment Variable.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 7 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Read-Only Shell Variables  Command Line Arguments (parameters passed to a shell script at the command line).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 8 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming “set” Command Displays all Shell Variables.  Displays Environment and User-Defined Variables $ set BASH=/bin/bash BASH_VERSION='4.1.7(1)-release' DISPLAY=:0.0 ‘ HOME=/home/sau “env” Command Displays all Shell Variables.  Display the Environment Variables $ env HOME=/home/sau HOSTNAME=buildbed-vm MAIL=/var/spool/mail/sau PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/kde3/bin:/u sr/lib/mit/bin:/usr/lib/mit/sbin:/usr/lib/qt3/bin

SILICON VALLEY UNIVERSITY CONFIDENTIAL 9 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Reading/Writing Shell Variables variable1=value1 [variable2=value2 …]  NO space before or after “=“ character.  Value containing spaces require quotes. (“) Double quotes : Variable substitution but metacharacters are treated literally. (‘) Single quotes: Whole string is treated literally. $variable  Variable substitution and the value of variable is executed as shell command. $ command=pwd $ $command /home/sau/class

SILICON VALLEY UNIVERSITY CONFIDENTIAL 10 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming $ name=John $ echo $name John $ name=John Doe Doe: not found $ name=”John Doe” $ echo $name John Doe $ name=John* $ echo $name John.Bates.letter John.Johnsen.memo John. $ echo “$name” John* $ echo “The name $name sounds familiar!” The name John* sounds familiar! $ echo \$name $name $ echo ‘$name’ $name $ Shell variable assignment. “$” returns current value of shell variable. No space allowed between shell variable, “=“, and value. The word “Doe” is interpretted as shell command. Use quotes (“) to include space in the value. Without quotes, the asterisk (*) is processed as a metacharacter. Use quotes (“) to show the value, but does not interpret the metacharacters. Use “\” as escape character. Use single quote (‘) to process whole string literally.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 11 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Command Substitution `command`  Command enclosed in back quotes (`) will be executed by the shell and substitutes the output of the command with the command (including back quotes). $ command=pwd $ echo "The value of command is : $command." The value of command is : pwd. $ command=`pwd` $ echo "The value of command is : $command." The value of command is : /home/sau/class4. $ ~/class4> echo "The data and time is `date`." The data and time is Fri Nov 30 02:27:56 PST 2012.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 12 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Exporting Environment  Environment variable ONLY known to local Shell environment.  Shell script executed in local Shell environment does not get access to calling shell’s environment variables. Export [ name-list ]  Export the names and pass the value of one or more variables to any command, Shell script, subsequent shell and subshell executed from the local Shell environment.  Exported variable has a copy of the variable’s value in the called Shell script or subshell. Exported variable cannot be changed in the called Shell script or subshell. The calling Shell script variable remains the same.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 13 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Script file display_name has shell variable “name”. The calling Shell evironment also has shell variable “name”, but since it was not exported, the called script file, “display_name” does NOT get the value from the calling Shell environment. The calling Shell evironment exports the shell variable “name”. The called script file, “display_name” does get the value from the calling Shell environment and displays it. The calling Shell environment echos “$?”, readonly Shell environment variable.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 14 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming $ cat export_demo #!/bin/sh name="John Doe" export name./display_change_name./display_name exit 0 $ cat display_change_name #!/bin/sh echo $name name="Plain Jane" echo $name./display_name exit 0 $./export_demo John Doe Plain Jane John Doe $ Script file “export_demo” will call script file “display_change_name” and “display_name” with exported Shell variable, “name”. The script file “display_change_name” will change the value of the variable exported from calling script file, “name”. Calls script file “display_name” with changed value. The changed value is changed for the subsequent Shell script. The calling script file, “export_demo”, exported Shell variable does not change.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 15 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Resetting Variables unset [ name-list ]  Reset or remove the variable or function corresponding to the names in the ‘name-list’, where ‘name-list’ is a list of names separated by spaces. variable=  Assigns the variable to NULL value.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 16 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Read-Only Defined Variables readonly [ name-list ]  Prevent assignment of new values to the variables in “name-list”.  “readonly” displaces ALL read-only variables.  $ name=Jim  $ place=Ames  $ echo "$name $place"  Jim Ames  $ readonly name place  $ name=ART place="Ann Arbor"  bash: name: readonly variable  $ readonly  declare -ir EUID="1001"  declare -ir PPID="5228“  declare -ir UID="1001"  declare -r name="Jim"  declare -r place="Ames" Assigning value to readonly Shell variable returns error. “readonly” displays all read-only Shell variables.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 17 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Reading from Standard Input  read variable-list  Read one line from standard input and assign words in the line to variables in “name-list”

SILICON VALLEY UNIVERSITY CONFIDENTIAL 18 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Special Characters for the echo Command

SILICON VALLEY UNIVERSITY CONFIDENTIAL 19 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Passing Arguments to Shell Scripts First nine of the command line arguments or positional parameters are:  $1 through $9  Set to NULL if positional variable is not passed an argument $# : Total number of arguments passed to the script. $* : Contains values of all the arguments. : Contains values of all the arguments and the arguments are in quotes if used as $0 : Contains the name of the script file.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 20 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Passing Arguments to Shell Scripts $ cat cmdargs_demo #!/bin/sh echo “The command name is: $0.” echo “The number of command line arguments passed as parameters are $#.” echo “The value of the command line arguments are: $1 $2 $3 $4 $5 $6 $7 $8 $9.” echo “Another way to display values of all of the arguments: echo “Yet another way is: $*.” exit 0 $ cmdargs_demo a b c d e f g h I The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 9. The value of the command line arguments are: a b c d e f g h i. Another way to display values of all of the arguments: a b c d e f g h i. Yet another way is: a b c d e f g h i. $ cmdargs_demo One Two 3 Four 5 6 The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 6. The value of the command line arguments are: One Two 3 Four 5 6. Another way to display values of all of the arguments: One Two 3 Four 5 6. Yet another way is: One Two 3 Four 5 6. $ Display name of the script file. Display number of arguments.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 21 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Passing Arguments to Shell Scripts Set [ options ] [ argument-list ]  Set values of the positional arguments (i.e. $1, $2, etc) to the arguments in “argument-list”.  When executed without an argument, the set command displays names of all shell variables and their current values.  Options: -v = Verbose -n = noexec  set `date`  Sat Dec 1 00:28:45 PST 2012 $1 $2 $3 $4 $5 $6 $ date Sat Dec 1 00:28:45 PST 2012 $ set `date` $ echo Sat Dec 1 00:28:51 PST 2012 $ echo $2 $3, $6 Dec 1, 2012

SILICON VALLEY UNIVERSITY CONFIDENTIAL 22 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Passing Arguments to Shell Scripts shift [ N ]  Shift the command line arguments N positions to the left.  Once shifted, the argument cannot be recovered.  set `date`  Sat Dec 1 00:39:44 PST 2012 $1 $2 $3 $4 $5 $6 $ date Sat Dec 1 00:39:35 PST 2012 $ set `date` $ echo Sat Dec 1 00:39:44 PST 2012 $ echo $1 $2 $3 $4 Sat Dec 1 00:39:44 $ shift 2 $ echo $1 $2 $3 $4 1 00:39:44 PST 2012 $

SILICON VALLEY UNIVERSITY CONFIDENTIAL 23 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming $ cat shift_demo #!/bin/sh echo “The program name is $0.” echo “The arguments are: echo “The first three arguments are: $1 $2 $3” shift echo “The program name is $0.” echo “The arguments are: echo “The first three arguments are: $1 $2 $3” shift 3 echo “The program name is $0.” echo “The arguments are: echo “The first three arguments are: $1 $2 $3” exit 0 $ shift_demo The program name is shift_demo. The arguments are: The first three arguments are: The program name is shift_demo. The arguments are: The first three arguments are: The program name is shift_demo. The arguments are: The first three arguments are: $ Shift positional arguments 1 argument to the left. Shit positional arguments 3 arguments to the left.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 24 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Passing Arguments to Shell Scripts $ cat set_demo #!/bin/sh filename="$1" set `ls -il $filename` inode="$1" size="$6" echo "Name\tInode\tSize" echo echo "$filename\t$inode\t$size" exit 0 $ ls -il mydata rw-r--r-- 1 sau users :39 mydata $./set_demo mydata Name Inode Size Mydata $ Set values of the positional argument with command substitution (`). Overwrites previous $0 - $9. $1 $6

SILICON VALLEY UNIVERSITY CONFIDENTIAL 25 Pearson Addison-Wesley Introduction UNIX/Linux Course Bourne Shell Programming Comments and Program Headers  Comments in Shell scripts to describe the purpose of a particular set of commands. Similar to putting comments in program code.  Program header for Shell scripts: Name of the file containing the Shell script. Name of the author. Date written. Date last modified. Purpose of the script ( in one or two lines ). A brief description of the algorithm used to implement the solution to the problem at hand.  Use “#” to start a comment line. Comment line does not have to start at the beginning of the line. For example, the comment line could follow the command.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 26 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Comments and Program Headers Kernel Script File: #!/bin/bash SCRIPT_VERSION= ####################################################################### #Copyright (C) 2007 Free Software Foundation. #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation; either version 2 of the License, or #(at your option) any later version. … ####################################################################### #The script was written for 2 main reasons: # 1. Remove the need for the devs/helpers to ask several questions before we can easily help the user. # 2. Allow newer/inexperienced ALSA users to give us all the info we need to help them. …

SILICON VALLEY UNIVERSITY CONFIDENTIAL 27 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands  Used to determine the sequence in which statements in a Shell script execute. There are three basic types of statements for controlling flow of a script. -- Two-way Branching : if statement -- Multiway Branching : if statement and case statement -- Repetitive execution of one or more commands. for, while, and until statement

SILICON VALLEY UNIVERSITY CONFIDENTIAL 28 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Two-way Branching: Using if and then statements “expression” is a condition or evaluation using “test” command that will return a true or false.  True: “then-commands” is executed.  One space required before/after an operator, a parenthesis, bracket, or operand.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 29 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Two-way Branching:

SILICON VALLEY UNIVERSITY CONFIDENTIAL 30 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Two-way Branching: Using if / then / else statements  “if” “expression” is true, the commands in “then-command” is executed.  “if” “expression” is false, the commands in “else-command” is executed.  “fi” terminates the two-way branching.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 31 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Two-way Branching:

SILICON VALLEY UNIVERSITY CONFIDENTIAL 32 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Multi-way Branching: Using if / then / elif / else statements  “if” “expression” is true, the commands in “then-command” is executed OR “elif: “expression” is true,  “if” or “elif” “expression” is false, the commands in “else-command” is executed.  “fi” terminates the two-way branching. if expression then [elif expression then then-command-list ]... [else else-command-list ] fi if expression1 then then-commands elif expression2 elif1-commands elif expression3 elif2-commands... else else-command fi

SILICON VALLEY UNIVERSITY CONFIDENTIAL 33 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Multi-way Branching: Using if / then / elif / else statements

SILICON VALLEY UNIVERSITY CONFIDENTIAL 34 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Operators for the “test” Command

SILICON VALLEY UNIVERSITY CONFIDENTIAL 35 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Using “if”, “then”, and “test” expression. $1: Parameter 1. $# : Total command line arguments. $0: Name of program. $?: Exit status. -gt: a > b? -eq: a equal b? -d: file is directory? -f: ordinary file?

SILICON VALLEY UNIVERSITY CONFIDENTIAL 36 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Using “if”, then”, and “expression” as conditional. $1: Parameter 1. $# : Total command line arguments. $0: Name of program. $?: Exit status. -gt: a > b? -eq: a equal b? -d: file is directory? -f: ordinary file?

SILICON VALLEY UNIVERSITY CONFIDENTIAL 37 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Using “if”, then”, and “elif” as conditional. $1: Parameter 1. $# : Total command line arguments. $0: Name of program. $?: Exit status. -eq: a equal b? -d: file is directory? -f: ordinary file?

SILICON VALLEY UNIVERSITY CONFIDENTIAL 38 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The for Statement  Used for repetitive execution of a block of commands in shell script.  Assign each word in “argument-list” to “variable” in turn and execute “command-list”. If “argument-list” is ommitted, (values of all command line arguments) is assumed.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 39 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The for Statement Variable “people” is assigned words in “argument-list” one by one. Variable people is echoed.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 40 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The for Statement for variable [in arg-list]: Since arg- list is NULL, (values of all command line arguments) is assumed. $?: Exit status of most recent command. Once $user found, in /etc/passwd, display fifth column.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 41 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The while Statement  Repeated execution of a block of commands based on the condition of an expression. Continue executing “command-list” as long as “expression” evaluates to TRUE.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 42 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands $ cat while_demo #!/bin/sh secretcode=agent007 echo “Guess the code!” echo “Enter your guess: \c” read yourguess while [ “$secretcode” != “$yourguess” ] Do echo “Good guess but wrong. Try again!” echo “Enter your guess: \c” read yourguess Done echo “Wow! You are a genius!!” exit 0 $ while_demo Guess the code! Enter your guess: star wars Good guess but wrong. Try again! Enter your guess: columbo Good guess but wrong. Try again! Enter your guess: agent007 Wow! You are a genius!! $ Expression evaluates TRUE, continue execution. Expression evaluates FALSE, exit.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 43 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The until Statement  Repeated execution of a block of commands based on the condition of an expression. Continue executing “command-list” as long as “expression” evaluates to FALSE.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 44 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands Expression evaluates FALSE, continue execution. Expression evaluates TRUE, exit.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 45 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The break and continue Statements  Interrupt Sequential Execution of loop in a script

SILICON VALLEY UNIVERSITY CONFIDENTIAL 46 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The case Statement  Multiway branching similar to nested if statements.  Use when elifs exceeds 3 levels.  Compares value in “test-string” with values of all patterns.  “ ; ; “ delimits “command-list”, otherwise fall-through.  Default pattern is “ * ) ”, wild card pattern to handle exception (error) condition.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 47 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The case Statement

SILICON VALLEY UNIVERSITY CONFIDENTIAL 48 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The case Statement Case statement matches “option” with four patterns (upper or lower case). Default is the wild card pattern.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 49 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 10 Bourne Shell Programming Program Control Flow Commands The case Statement Case statement does not match patterns, but execution caught by “default” wild card pattern.