1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.

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.
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
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
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.
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.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
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.
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.
(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.
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
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.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
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.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Shell Control Structures CSE 2031 Fall June 2016.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
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.
Presented by, Mr. Satish Pise
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
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX

2 Command Line Arguments Cont. $# contains the number of command line arguments. will be replaced by a string containing the command line arguments Example script echo.sh #!/bin/sh echo “The” $# “arguments entered:” Usage: echo.sh alpha beta gamma Output: The 3 arguments entered: alpha beta gamma

3 Testing Conditions There are two ways to test for conditions. The two general forms are: test or [ ] The latter method is easier to read. Remember to include a space before and after the bracket A condition can be reversed with a ! before the condition (this is the same as not condition)‏ [ ! ] A ‘:’ command in place of condition always returns true

4 Testing File Attributes To test if a file is readable [ -r prog.txt ] [ -r $1.c ] To test if a file is writeable [ -w specialfile.txt ] To test if a file is executable [ -x prog4.sh ] To test if a file exists [ -f temp.text ] Testing for the negation - use ! (eg. not writeable)‏ [ ! -w nochange.txt ]

5 Numeric Tests The following operators can be used for numeric tests: { -eq, -ne, -gt, -ge, -lt, -le } Examples [ $1 –lt $2 ] [ $1 –gt 0 ] [ $# -eq 2 ] [ $# -lt 3 ]

6 Simple If Statement General Form: if then one-or more commands fi Example: if [ -r tmp.text ] then echo “temp.text is a readable file” fi

7 General If Statement General form: if then one-or-more-commands elif then one-or-more-commands … else one-or-more-commands fi Note that you can have 0 or more elif statements and that the else is optional.

8 Testing Strings Performing string comparisons. It is a good idea to put the shell variable being tested inside double quotes. [ “$1” = “yes” ] [ “$2” != “no” ] Note that the following will give a syntax error when $1 is empty since: [ $1 != “no” ] becomes [ != “no” ]

9 Testing with Multiple Conditions && is the and operator || is the or operator checking for the and of several conditions [ “$1” = “yes” ] && [ -r $2.txt ] [ “$1” = “no” ] && [ $# -eq 1 ] checking for the or of several conditions [ “$1” = “no” ] || [ “$2” = “maybe” ]

10 Quoting Rules Using single quotes ‘xyz’ disables all special characters in xyz Using double quotes “xyz” disables all special characters in xyz except $, `, and \. using the backslash \x disables the special meaning of character x

11 Quoting Examples var1=“alpha”#set the variable echo $var1#prints: alpha echo “$var1”#prints: alpha echo ‘$var1’#prints: $var1 cost=2000 echo ‘cost:$cost’#prints: cost:$cost echo “cost:$cost”#prints: cost:2000 echo “cost:\$cost”#prints: cost:$cost echo “cost:\$$cost”#prints: cost:$2000

12 More on String Relational Operators The set of string relational operators are: { =, !=, >, >=, <, <= } The { >, >=, <, <= } operators assume an ASCII ordering (for example “a” < “c”). These operators are used with the expr command that computes an expression. The backslash has to be used before the operators so that they are not confused with I/O redirection

13 Using Exit The exit command causes the current shell script to terminate. There is an implicit exit at the end of each shell script. The exit command can set the status at the time of exit. If the status is not provided, the script will exit with the status of the last command. General form: exit or exit $? is set to the value of the last executed command Zero normally indicates success. Nonzero values indicate some type of failure. Thus, exit 0 is normally used to indicate that the script terminated without errors.

14 If Statement Examples if [ “$1” != “” ] || [ -r $1 ] then echo “the file” $1 “is not readable” fi if [ $var1 –lt $var2 ] then echo $var1 “is less than” $var2 elif [ $var1 –gt $var2 ] then echo $var1 “is greater than” $var2 else echo $var1 “is equal to” $var2 fi

15 Case Statement Compares stringvalue to each of the strings in the patterns. At a match, it does the corresponding commands. ;; indicates to jump to the statement after the esac (end of case). *) means the default case. Form: case stringvalue in pattern1) one or more commands;; pattern2) one or more commands;; … *) one or more commands;; esac

16 Case Statement Example echo “do you want to remove file $1?” echo “ please enter yes or no” read ans case $ans in “yes”) rm $1 echo “file removed” ;; “no”) echo “file not removed” ;; *) echo “do not understand your request” esac

17 while and until statements while form: while do one or more commands done until form: until do one or more commands done

18 while and until examples read cmd while [ $cmd != “quit” ] do … read cmd done read cmd until [ $cmd = “quit” ] do … read cmd done

19 Good reference on scripting