Writing Shell Scripts ─ part 3

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
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.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
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.
Introduction to UNIX / Linux - 11
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
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.
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
CS465 - UNIX The Bourne Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
CSC 4630 Meeting 5 January 31, Next Time Enhance the steps that you used to clean the Moby Dick chapter to create a shell script that takes any.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Shell Control Statements and More
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.
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.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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]
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Writing Shell Scripts ─ part 1
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
Copyright © 2003 Pearson Education, Inc.
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
Designing and Debugging Batch and Interactive COBOL Programs
Writing Shell Scripts ─ part 1
Linux Shell Script Programming
Writing Shell Scripts ─ part 1
Shell Control Structures
Shell Control Structures
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Essential Shell Programming
Presentation transcript:

Writing Shell Scripts ─ part 3 EECS 2031 14 September 2018

Debugging Tools sh –v myscript arguments v: verbose displays each command it finds in the script as it encounters it (before substitution). allows you to find which particular line in your code has the syntax error. Displaying will stop at this point and the script exits. Example: sh -v show_shift a b c d e f

Debugging Tools (2) sh –x myscript x: execute similar to –v, but displays a command only when it is executed (before execution but after substitution). useful for debugging control structures (if, case, loops). if no control structures then x and v display the whole program. puts a plus sign (+) in front of any command that gets processed (easier to read than –v). Examples: sh -x show_shift a b c d e f sh -x chkex ghost # compare with -v

Debugging Tools (3) sh –xv myscript Both options may be used at the same time. To check variable substitutions. Example: sh -xv show_shift a b c d e f To view the whole program and its execution. sh -xv chkex ghost

Debugging Tools (4) sh –n myscript Reads the commands but does NOT execute them. Useful for “compiling” the script to detect syntax errors. Example uses: a good working script will modify/delete files. interactive input from user is required. very long scripts.

Changing Values of Positional Parameters Positional parameters $1, $2, … normally store command line arguments. Their values can be changed using set command , for example, set `date` The new values are the output of date command. 6

Example % cat setparm #!/bin/sh echo "Hello, $1. You entered $# command line argument(s). Today's date is ..." date set `date` echo There are now $# positional parameters. The new parameters are ... echo \$1 = $1, \$2 = $2, \$3 = $3, \$4 = $4, \$5 = $5, \$6 = $6. % setparm Amy Tony Hello, Amy. You entered 2 command line argument(s). Today's date is ... Sat Nov 27 11:55:52 EST 2010 There are now 6 positional parameters. The new parameters are ... $1 = Sat, $2 = Nov, $3 = 27, $4 = 11:55:52, $5 = EST, $6 = 2010. 7

Shell Functions Similar to shell scripts. Stored in shell where it is defined (instead of in a file). Executed within sh no child process spawned Syntax: function_name() { commands } Allows structured shell scripts

Example #!/bin/sh # function to sample how many users are logged on { echo “Users logged on:” >> users date >> users who >> users echo “-----------” >> users } # taking first sample log # taking second sample (30 min. later) sleep 1800 In assignment 2, display menu by writing a function.

Shell Functions (2) Make sure a function does not call itself causing an endless loop. % cat makeit #!/bin/sh … sort() { sort $* | more } Avoid using existing Unix commands as function names. Alternative fix (but not recommended): % cat makeit #!/bin/sh … sort() { /bin/sort $* | more }

Environment and Shell Variables Standard UNIX variables are divided into 2 categories: shell variables and environment variables. Shell variables: apply only to the current instance of the shell; used to set short-term working conditions. displayed using ‘set’ command. Environment variables: set at login and are valid for the duration of the session. displayed using ‘env’ command. By convention, shell variables have lower case names. environment variables have UPPER case names

Environment and Shell Variables (2) In general, environment and shell variables that have “the same” name (apart from the case) are distinct and independent, except for possibly having the same initial values. Exceptions: When home, user and term are changed, HOME, USER and TERM receive the same values. But changing HOME, USER or TERM does not affect home, user or term. Changing PATH causes path to be changed and vice versa.

Variable path PATH and path specify directories to search for commands and programs. cd # move to home directory countargs a b # in C2031/Lect_UNIX, so failed echo $path set path=($path C2031/Lect_UNIX) countargs a b # successful To add a path permanently, add the line to your .cshrc file after the list of other commands. set path=($path .) # to avoid typing ./a.out

set Command Summary Displays shell variables Set variables (e.g., set path) Changing command-line arguments set `date`

break and continue Interrupt loops (for, while, until) break transfers control immediately to the statement after the nearest done statement terminates execution of the current loop continue transfers control immediately to the nearest done statement brings execution back to the top of the loop Same effects as in C.

break and continue Example #!/bin/sh while true do echo “Entering ‘while’ loop ...” echo “Choose 1 to exit loop.” echo “Choose 2 to go to top of loop.” echo -n “Enter choice: ” read choice if test $choice = 1 then break fi echo “Bypassing ‘break’.” if test $choice = 2 then continue fi echo “Bypassing ‘continue’.” done echo “Exit ‘while’ loop.”

$* versus $@ $* and $@ are identical when not quoted: expand into the arguments blanks in arguments result in multiple arguments. They are different when double-quoted: “$@” each argument is quoted as a separate string. “$*” all arguments are quoted as a single string.

$* versus $@ Example % cat displayargs #!/bin/sh echo All the arguments are "$@". countargs "$@" echo All the arguments are "$*". countargs "$*" % cat countargs echo Number of arguments to countargs = $# % sh -xv displayargs Mary Amy Tony

Next lecture ... C again: program structure Review Reading for this lecture: 3.6 to 3.8, UNIX textbook Posted tutorial on standard UNIX variables See Chapter 5, UNIX textbook for more examples.