CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

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
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
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.
CSCI 330 T HE UNIX S YSTEM Bash Programming. B ASIC S HELL P ROGRAMMING A script is a file that contains shell commands data structure: variables control.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The 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.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
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 Scripting AFNOG IX Rabat, Morocco May 2008.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
#!/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.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
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.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
(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.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Control Statements and More
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
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
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.
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 Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
UNIX Reference Sheets CSE 2031 Fall 2010.
Essential Shell Programming
Shell Control Structures
Chapter 5 The Bourne Shell
The Bash Shell Bash Programming
Shell Control Structures
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II

Unit Overview how to debug ? Decision case Repetition while, until for Functions 2CSCI UNIX and Network Programming

Debug shell Scripts Debugging is troubleshooting errors that may occur during the execution of a program/script 2 commands can help to debug: echo use explicit output statements to trace execution set trace execution path 3CSCI UNIX and Network Programming

Debugging using “set” “set” command is a shell built-in command has options to allow tracing of execution -v print shell input lines as they are read -x option displays expanded commands and its arguments -n read commands but do not execute, checks for syntax errors options can turned on or off To turn on the option: set -xv To turn off the options: set +xv options can also be set via she-bang line #! /bin/bash -xv 4CSCI UNIX and Network Programming

The case Statement to make decision that is based on multiple choices Syntax: case word in pattern1) command-list1 ;; pattern2) command-list2 ;; patternN) command-listN ;; esac 5CSCI UNIX and Network Programming

case pattern checked against word for match may also contain: * ? [ … ] [:class:] multiple patterns can be listed via: | 6CSCI UNIX and Network Programming

Example: case Statement #! /bin/bash echo "Enter Y to see all files including hidden files" echo "Enter N to see all non-hidden files" echo "Enter Q to quit" read -p "Enter your choice: " reply case "$reply" in Y|YES) echo "Displaying all (really…) files" ls -a ;; N|NO) echo "Display all non-hidden files..." ls ;; Q) exit 0 ;; *) echo "Invalid choice!"; exit 1 ;; esac 7CSCI UNIX and Network Programming

The while Loop executes “command-list” as long as “test-command” evaluates successfully Syntax: while test-command do command-list done 8CSCI UNIX and Network Programming

Example: Using the while Loop #!/bin/bash # script shows user’s active processes cont="y" while [ "$cont" = "y" ]; do ps read -p "again (y/n)? " cont done echo "done" 9CSCI UNIX and Network Programming

Example: Using the while Loop #! /bin/bash # copies files from home- into the webserver- directory # a new directory is created every hour PICSDIR=/home/carol/pics WEBDIR=/var/www/carol/webcam while true; do DATE=`date +%Y%m%d` HOUR=`date +%H` mkdir $WEBDIR/$DATE while [ "$HOUR" != "00" ]; do mkdir $WEBDIR/$DATE/$HOUR mv $PICSDIR/*.jpg $WEBDIR/$DATE/$HOUR sleep 3600 HOUR=`date +%H` done 10CSCI UNIX and Network Programming

The until Loop executes “command-list” as long as “test-command” does not evaluate successfully Syntax: until test-command do command-list done 11CSCI UNIX and Network Programming

Example: Using the until Loop #!/bin/bash # script shows user’s active processes stop="n" until [ "$stop" = "y" ]; do ps read -p "done (y/n)? " stop done echo "done" 12CSCI UNIX and Network Programming

The for Loop executes “commands” as many times as the number of words in the “word-list” Syntax: for variable in word-list do commands done 13CSCI UNIX and Network Programming

Example 1: for loop #!/bin/bash for index in do echo $index done 14CSCI UNIX and Network Programming

Example 2: Using the for loop #!/bin/bash # compute average weekly temperature TempTotal=0 for day in do read -p "Enter temp for $day: " Temp TempTotal=$((TempTotal+Temp)) done AvgTemp=$((TempTotal/7)) echo "Average temperature: " $AvgTemp 15CSCI UNIX and Network Programming

Example 3: Using the for loop #!/bin/bash # compute average weekly temperature TempTotal=0 for day in `seq 7` do read -p "Enter temp for $day: " Temp TempTotal=$((TempTotal+Temp)) done AvgTemp=$((TempTotal/7)) echo "Average temperature: " $AvgTemp 16CSCI UNIX and Network Programming

Example 4: Using the for Loop #!/bin/bash # compute average weekly temperature TempTotal=0 for day in Mon Tue Wed Thu Fri Sat Sun do read -p "Enter temp for $day: " Temp TempTotal=$((TempTotal+Temp)) done AvgTemp=$((TempTotal/7)) echo "Average temperature: " $AvgTemp 17CSCI UNIX and Network Programming

Example 5: Using the for Loop #!/bin/bash # compute average weekly temperature TempTotal=0 for day in `cat day-file` do read -p "Enter temp for $day: " Temp TempTotal=$((TempTotal+Temp)) done AvgTemp=$((TempTotal/7)) echo "Average temperature: " $AvgTemp 18CSCI UNIX and Network Programming

looping over arguments simplest form will iterate over all command line arguments: #! /bin/bash for parm do echo $parm done 19CSCI UNIX and Network Programming

break and continue interrupt for, while or until loop break statement terminate execution of the loop transfers control to the statement AFTER the done statement continue statement skips the rest of the current iteration continues execution of the loop 20CSCI UNIX and Network Programming

Shell Functions must be defined before they can be referenced usually placed at the beginning of the script Syntax: function-name () { statements } 21CSCI UNIX and Network Programming

Example: function #!/bin/bash funky () { # This is a simple function echo "This is a funky function" } # declaration must precede call: funky 22CSCI UNIX and Network Programming

Function parameters Need not be declared Arguments provided via function call are accessible inside function as $1, $2, $3, … $# reflects number of parameters $0 still contains name of script (not name of function) 23CSCI UNIX and Network Programming

Example: function with parameters #! /bin/bash checkfile() { for file do if [ -f "$file" ]; then echo "$file is a file" else if [ -d "$file" ]; then echo "$file is a directory" fi done } checkfile. funtest 24CSCI UNIX and Network Programming

Local Variables in Functions Variables defined within functions are global, i.e. their values are known throughout the entire script Keyword “local” inside a function defines variables that are “local” to that function, i.e. not visible outside 25CSCI UNIX and Network Programming

Example: function #! /bin/bash global="pretty good variable" foo () { local inside="not so good variable" echo $global echo $inside global="better variable" } echo $global foo echo $global echo $inside 26CSCI UNIX and Network Programming

return from function Syntax: return [status] ends execution of function optional numeric argument sets return status default is “ return 0 ” 27CSCI UNIX and Network Programming

return example #! /bin/bash testfile() { if [ $# -gt 0 ]; then if [ ! -r $1 ]; then return 1 fi } if testfile funtest; then echo " funtest is readable " fi 28CSCI UNIX and Network Programming

Unit Summary Debugging Decision case Repetition while, until for Functions 29CSCI UNIX and Network Programming