Today s Lecture. The Unix Shell. Unix Architecture (simplified) Lecture 3: Unix Shell, Pattern Matching, Regular Expressions

Similar documents
5/20/2007. Touring Essential Programs

Pattern Matching. An Introduction to File Globs and Regular Expressions

Pattern Matching. An Introduction to File Globs and Regular Expressions. Adapted from Practical Unix and Programming Hunter College

CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

CS Unix Tools & Scripting

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Lab #0 Getting Started Due In Your Lab, August 25, 2004

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Regular Expressions. Regular Expression Syntax in Python. Achtung!

Lecture 18 Regular Expressions

System Programming. Unix Shells

Review of Versioning. Configuration Management. Relation of Versioning to CM. Lecture 14: Configuration Management & Midterm Review

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

COMP 4/6262: Programming UNIX

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Wildcards and Regular Expressions

CS395T: Introduction to Scientific and Technical Computing

Essential Linux Shell Commands

Perl and R Scripting for Biologists

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Scripting Languages Course 1. Diana Trandabăț

CST Lab #5. Student Name: Student Number: Lab section:

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

Regular Expressions 1

Introduction to UNIX Part II

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Linux shell programming for Raspberry Pi Users - 2

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Filenames, globbing, greping, and regexp

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

COMP2100/2500 Lecture 16: Shell Programming I

Linux Command Line Interface. December 27, 2017

Oxford University Computing Services. Getting Started with Unix

CST Algonquin College 2

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

Vi & Shell Scripting

EECS2301. Lab 1 Winter 2016

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Unix/Linux: History and Philosophy

Awk & Regular Expressions

Assignment clarifications

CS Unix Tools. Fall 2010 Lecture 5. Hussam Abu-Libdeh based on slides by David Slater. September 17, 2010

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Reuse in a Unix Environment. Creating a Library. Libraries. Lecture 10: Unix Libraries

UNIX files searching, and other interrogation techniques

ITST Searching, Extracting & Archiving Data

Regular Expressions. Regular expressions match input within a line Regular expressions are very different than shell meta-characters.

System & Network Engineering. Regular Expressions ESA 2008/2009. Mark v/d Zwaag, Eelco Schatborn 22 september 2008

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

UNIX II:grep, awk, sed. October 30, 2017

Shells and Shell Programming

Overview of the UNIX File System. Navigating and Viewing Directories

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Shells and Shell Programming

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Fundamentals of Programming Session 4

5/8/2012. Exploring Utilities Chapter 5

UNIX System Programming Lecture 3: BASH Programming

Processes and Shells

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Regexs with DFA and Parse Trees. CS230 Tutorial 11

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

Computer Systems and Architecture

The Online Unix Manual

Introduction: What is Unix?

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

5/8/2012. Specifying Instructions to the Shell Chapter 8

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Shell Programming Overview

PESIT Bangalore South Campus

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Module 8 Pipes, Redirection and REGEX

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

SOFTWARE ARCHITECTURE 3. SHELL

1. The Mac Environment in Sierra Hall 1242

Transcription:

Lecture 3: Unix Shell, Pattern Matching, Regular Expressions Today s Lecture Review Lab 0 s info on the shell Discuss pattern matching Discuss regular expressions Kenneth M. Anderson Software Methods and Tools CSCI 3308 - Fall Semester, 2003 September 5, 2002 University of Colorado, 2003 2 Unix Architecture (simplified) Shell Processes (programs) ls Netscape The Kernel (Device Drivers, Virtual Memory, Files, etc.) Disk Printer Keyboard The Unix Shell A shell is a program that presents the user with an interpreted programming environment; there are many many shells! It provides Variables and built-in commands The ability to execute external commands (e.g. programs) The ability to redirect input and output Shortcuts such as aliases and wildcards In this class we are going to be using tcsh Its an exted version of csh (The C Shell); the tcsh author added the t after adding features from the TENEX and TOP-10s operating systems to the vanilla C shell The purpose behind csh was to emulate the syntax and operators of the C programming language September 5, 2002 University of Colorado, 2003 3 September 5, 2002 University of Colorado, 2003 4

Variables More on Spaces % set x = ken % echo x x % echo $x ken % set y = (bananas apples kiwi) This creates a 1-based array (first element indexed with a 1) An array is separated by spaces (which can cause problems) and surrounded by parentheses What happens if you leave the parentheses out? Certain constructs can take advantage of an array %echo $y[2] apples %foreach fruit ($y) foreach? echo $fruit foreach? bananas apples kiwi %set y[2] = oranges %echo $y bananas apples kiwi Arrays are often used to iterate over the contents of directories in the file system Since the space character is used as a delimiter for arrays, you need to watch out for spaces that appear in file and directory names See example next slide September 5, 2002 University of Colorado, 2003 5 September 5, 2002 University of Colorado, 2003 6 Example Directory Structure Tenure Review/ Tenure Talk Tenure Demo %set z = (`find Tenure\ Review -type d -print`) %echo $z Tenure Review Tenure Review/Tenure Talk %echo $z[1] Tenure %echo $z[2] Review This is not what we want! How to fix? Use the shell quoting mechanism Already saw one example when I used the string Tenure\ Review in the find command The backslash escapes the space and allows the two words to be treated as a single directory name You will learn more about the quoting mechanism in your labs; In addition, there is a lot of inforamtion about the quoting mechanism in your reference text book September 5, 2002 University of Colorado, 2003 7 September 5, 2002 University of Colorado, 2003 8

Example Revisited Tenure Review/ Tenure Talk Tenure Demo %set z = (` find Tenure\ Review -type d -print `) %echo $z Tenure Review Tenure Review/Tenure Talk %echo $z[1] Tenure Review %echo $z[2] Tenure Review/Tenure Talk Much better! September 5, 2002 University of Colorado, 2003 9 Math set treats the value as a string % set x = (2 + 3) % echo $x 2 + 3 % echo $x[2] + % Use @ to do math; Note: the space between the @ and the variable is REQUIRED % @ x = (2 + 3) % echo $x 5 tcsh supports most of C s expression operators (such as plus, minus, multiply, divide, less than, greater than, equal, etc.) + - * / > < == >= <= &&! ++ -- += -= *= /= September 5, 2002 University of Colorado, 2003 10 Input/Output Redirection Control Flow Constructs tcsh can redirect input and output it can also redirect error output (not shown) % date Sun Aug 20 11:11:10 MDT 2000 % date > today % more today Sun Aug 20 11:11:14 MDT 2000 % rev < today 0002 TDM 41:11:11 02 gua nus Conditional if ($x > 3) then echo true else echo false if Iteration while!($done) foreach directory (bin build lib) mkdir $directory September 5, 2002 University of Colorado, 2003 11 September 5, 2002 University of Colorado, 2003 12

Control Flow Constructs, cont. File Inquiry operations multi-branch switch ($char) case a: echo character is a breaksw default: echo character is not a breaksw sw tcsh does not have a for loop construct e.g., for (x = 0; x < 5; x++) use a while loop instead @ x = 0 while ($x < 5) @ x++ tcsh has file inquiry operators. They can be used in expressions. % set filename = ~/.cshrc % echo $filename /home/ken/.cshrc % if (-e $filename) echo true true % @ x = (5 + -e $filename) % echo x = $x x = 6 September 5, 2002 University of Colorado, 2003 13 September 5, 2002 University of Colorado, 2003 14 Job control When you invoke a program, you are executing a job You can find out what jobs are running with the jobs command Typically, a job susps the shell until it has finished running, e.g. when you invoke the ls program, the shell waits until ls generates its output You can run a job in the background with an ampersand &, e.g. % emacs & You can susp a running job using C-z (control-z) You can interrupt a running job using C-c A job can be brought into the foreground with fg and placed into the background with bg Wildcards The shell, and some other UNIX programs (such as find) make use of wildcard characters. The name wildcard comes from card games where a wild card can stand for any other card. We will also call wildcard characters, metacharacters. % ls graph.c graph.h main.c stack.c stack.h %ls *.c graph.c main.c stack.c Note: ls does not do the wildcard search, the shell does. If you do not want the shell to perform a wildcard search, then you need to quote all metacharacters %ls *.c ls: *.c: No such file or directory September 5, 2002 University of Colorado, 2003 15 September 5, 2002 University of Colorado, 2003 16

Wildcards and their meanings * - match 0 or more instances of any character? - match a single instance of any character [123ab] - match a single instance of any character within the brackets [0-9] - Shorthand for [0123456789] The range is based on the ASCII character set. So, [a-z] does not capture lowercase and uppercase letters. Use [a-za-z] instead [^0-9] - Match a single instance of any character except those specified in the brackets {pattern1,pattern2, } - Match one of the listed patterns September 5, 2002 University of Colorado, 2003 17 Wildcard Examples Consider a directory with the following files: aa aba a123aa baa Abbb a3ab % ls a* aa aba a123aa a3ab % ls a? aa % ls [ab][123][ab][ab] a3ab % ls *b[a-z] aba Abbb % ls {a?,*b} Abbb a3ab aa September 5, 2002 University of Colorado, 2003 18 Pattern Matching Wildcards are one form of pattern matching. Another form of pattern matching is based on a formalism known as regular expressions. We need to make this distinction since some programs, such as grep and awk, use regular expression pattern matching rather than wildcard pattern matching. Unfortunately, the syntax for each uses the same characters but in different ways! Actually, the situation is worse (especially for newcomers). Some metacharacters remain the same, but some are different in rather significant ways! Regular Expression Syntax. - match a single instance of any character except newline [123ab] - Match a single instance of any character within the brackets [0-9] - Shorthand for [0123456789] [^0-9] - Match a single instance of any character except those specified in the brackets pattern1 pattern2 Match one of the listed patterns Question: if [a-z] means match a single instance of a, b, c, d,, z, how do I match a - in a range expression? September 5, 2002 University of Colorado, 2003 19 September 5, 2002 University of Colorado, 2003 20

Regular Expression Syntax, cont. ^ - Match the beginning of the line $ - Match the of the line * - Match zero or more repetitions of the previous regular expression + - Match one or more repetitions of the previous regular expression (requires egrep)? - Match zero or one repetitions of the previous regular expression (requires egrep) \ - Remove the special meaning of the next character Wildcard and RE Differences In regular expressions * means something very different than it does in wildcards In wildcards a* matches a, aa, abc, a52b, In regular expressions a* matches only a, aa, aaa, aaaa, This also applies to + and?. They do not stand for any characters themselves, but rather modify the previous regular expression. So, how do you search for an *? a\* matches only a* September 5, 2002 University of Colorado, 2003 21 September 5, 2002 University of Colorado, 2003 22 Regular Expression Strategies When using a regular expression to find a desired search string, be aware of the following three quantities Hits Lines you wanted to match Omissions Lines you didn t match but wanted to match False Alarms Lines you matched but didn t want to match Looking for the word book %cat example This file tests for book in various places, such as book at the beginning of a line or at the of a line book as well as the plural books and handbooks %grep book example - matches only line 1 %grep book example - matches all lines How would we match lines 1, 2, and 3? September 5, 2002 University of Colorado, 2003 23 September 5, 2002 University of Colorado, 2003 24