CSC 352– Unix Programming, Fall 2012

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Advertisements

● Perl reference
CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Linux+ Guide to Linux Certification, Second Edition
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
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.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
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 8 Shell.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Linux+ Guide to Linux Certification, Third Edition
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
(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.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Random Bits of Perl None of this stuff is worthy of it’s own lecture, but it’s all a bunch of things you should learn to use Perl well.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Linux+ Guide to Linux Certification, Second Edition
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Input from STDIN STDIN, standard input, comes from the keyboard.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Unix Scripting Session 4 March 27, 2008.
CSE 303 Concepts and Tools for Software Development
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
CSC 352– Unix Programming, Fall 2012
What is Bash Shell Scripting?
Perl for Bioinformatics
Unix Scripting Dave Yearke
CSC 352– Unix Programming, Spring 2016
Control Structures: for & while Loops
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Unix Scripting Session 2 March 13, 2008.
Presentation transcript:

CSC 352– Unix Programming, Fall 2012 Weeks 13 & 14, 2012 Perl Programming (Highlights only), Chapter 15

Project 5 Project 5 is a redo of Project 3 in Perl. ~parson/unix/perl5.zip has the handout code. perl5/diffcheck is the assignment 3 difference checker as a Bash script. perl5/diffcheck.pl is the assignment 5 difference checker as a Perl script. Use diffcheck and diffcheck.pl as a Rosetta Stone. It shows you Bash scripting constructs and their Perl counterparts. perl5/replacestr is my assignment 3 solution as a Bash script. perl5/replacestr.pl is your starting point for a Perl script.

diffcheck.pl part 1 $USAGE="USAGE: diffcheck.pl DIRNAME1 DIRNAME2"; Perl assignments use a $ for defining and using any Perl variable. Perl terminates each command with a semicolon. Do not leave spaces around the = operator.

diffcheck.pl part 2 if ($#ARGV != 1) { } print STDERR "$USAGE\n"; print "NUMARGS $#ARGV\n"; exit 1; } $#ARGV is the index of the final command-line argument; it is one less than Shell’s $#. if, while, and foreach programming control constructs require { curly braces } around their code blocks, just like C++. Use print, not echo as in Shell. Put double quotes around the string, and redirect to STDERR if needed. Note the \n.

diffcheck.pl part 3 $DIR1=shift; $DIR2=shift; if (! (-d $DIR1)) { } print STDERR "ERROR. $DIR1 is not a valid directory.\n"; exit 1; } shift returns the next command line argument and shifts it away (discards it). -d tests for a directory. See Table 15.2 in the text. <, <=, ==, >=, > and != are for numeric comparison. lt, le, eq, ge, gt and ne are for string comparison. This is the opposite of the Shell!

diffcheck.pl part 4 $STARTDIR=`pwd`; # note the starting directory chomp($STARTDIR); # Get rid of any trailing (\n). Using backticks runs a command via the Shell. The returned result includes the trailing newline char (\n). Use chomp to strip that \n out of the returned string. foreach $file (`find . -type f -print`) { The foreach command loops over every element in an array. A backticked Shell command that returns multiple lines actually returns a Perl array, instead of a Shell-like string. Always use chomp on anything returned via backticks.

diffcheck.pl part 5 foreach $file (`find . -type f -print`) { } chomp($file); # strip away leading & trailing white space in file name print "DIFF $file ../$DIR2/$file\n"; system("diff $file ../$DIR2/$file 2>&1"); } Note use of chomp on each string returned from a backticked Shell command. system() function runs its string in the Shell without returning a value.

While loop from my replacestr.pl while ($#ARGV > 0) { This really says, “while the number of command line arguments is > 1”. $#ARGV is the index of the last element in the command line array, where the array starts at element 0. When $#ARGV == 0, there is in fact 1 element in the command line array. Use something like $myvar=shift; to read the next command line argument into your variable called $myvar and to shift it out of the command line array. This shift decrements $#ARGV. The while loop uses curly brace delimiters, just like the if and foreach programming constructs; if also supports else and elsif, see Figure 15.2 in the textbook.

Handout replacestr.pl foreach $file (`find $DIRNAME -type f -print | xargs grep -l "$PAT"`) { chomp($file); Because the backticks return an array of strings, we will combine the find and the grep in a single backtick to the Shell. The xargs command takes the stdout of the previous command in the pipeline and plugs those strings in as command line arguments for the command that xargs runs. In this case find supplies a list of regular files to xargs, which plugs them in as command line arguments for grep. Otherwise, we would have to join the array as shown on the next page.

You won’t need to do this: @allarray=`find $DIRNAME -type f -print`; Get all regular file paths into array @allarray. chomp(@allarray); Discard the \n on every element in the array. $all=join(" ", @allarray); Join the file paths into a single, space-separated string. @matchesarray=`grep -l "$PAT" $all`; Find matching files using grep –l, save into @matchesarray. foreach $file (@matchesarray) { Within loop chomp($file); and do other replacestr.pl steps.

Assignment 5 Assignment 5 is due by the end of Saturday, December 8 via gmake turnitin. There is another handout for the assignment. The comments in replacestr.pl outline your code, just like replacestr in assignment 3. You have my Shell file replacestr in this assignment handout. You need to use Perl syntax, control constructs, and command line argument processing. Use backticks to run Shell command lines similar to the solution in replacestr.