Perl Process Management

Slides:



Advertisements
Similar presentations
More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.
Advertisements

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
Using Linux Commands Lab 4 1. Create empty files To create an empty file $ touch apple banana grape grapefruit watermelon $ ls -l Using file-matching.
CHAPTER 2 THE UNIX SHELLS by U ğ ur Halıcı. layers in a unix system 1 Users Standard utility programs (shell, editors, compilers, etc.) Standard utility.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
The UNIX Shell Software Tools. Slide 2 Basic Shell Syntax command [-[options]] [arg] [arg] … l The name of the command is first l Options are normally.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
UNIX Utilities Software Tools. Slide 2 Getting Started on UNIX The machines in CS Lab2 are named csl2wk01 through csl2wk41. csl2wk01 means “CSLab2, workstation#1”
The UNIX Shells 1. What is a Unix shell? 2. A few common shells in the Unix & Linux. A. Bourne shell B. Korn shell C. C shell D. Bash-the default shell.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
UNIX Utilities Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn Input/Output.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Working with Files Chapter 5. Display a Calendar Display a calendar for a specific month – cal Display a calendar for a specific year – cal 2000.
Linux in More Detail Shirley Moore CPS5401 August 29,
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
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.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Chapter Four I/O Redirection1 System Programming Shell Operators.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI The UNIX System Shell Data Handling: Redirection and Piping
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
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.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Week 3 Redirection, Pipes, and Background
Process API COMP 755.
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 2 Introduction to C++ Programming
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
CSE 303 Concepts and Tools for Software Development
Programming Assignment # 2 – Supplementary Discussion
Perl I/O Learning Objectives:
Linux Shell Script Programming
Shell Programming.
Lecture 4 Redirecting standard I/O & Pipes
More Shell Programming
CSC 352– Unix Programming, Fall 2012
Chapter 3 The UNIX Shells
Introduction to Perl Learning Objectives:
CSC 4630 Meeting 4 January 29, 2007.
Presentation transcript:

Perl Process Management Learning Objectives: To learn the different Perl’s commands for invoking system process To learn how to perform process management in a Perl program Running Shell Commands – system Backquotes Using Processes as Filehandles

Running shell command - system (1) Perl programs can execute shell commands (Bourne shell) using the system function. system("date"); When the command is finished, the system function returns 0 (not 1!) if the command completed successfully. if(system("date") != 0){ print "Wah! Date failed!\n"; }

Running shell command - system (2) Where does the command output go? Answer: The display screen, unless it is redirected. $ cat system1 #!/usr/local/bin/perl5 -w system("date"); $ system1 Thu Apr 15 14:30:03 HKT 1999 $ How to redirect the I/O? Answer: Same as the shell $ cat system2 #!/usr/local/bin/perl5 -w if(system("date >now")){ die "cannot create file now"; } $ system2 $ cat now Thu Apr 15 14:30:03 HKT 1999 $

Running shell command - system (4) system allows multiple commands, separated by semicolons or newlines. Processes that end in & are run as background jobs (Perl continues and does not wait for the job to finish). $ cat system3 #!/usr/local/bin/perl5 -w $where = "now"; # Perl variable, not shell variable system "(date; who) >$where &"; $ system3 $ cat now Thu Apr 15 14:58:56 HKT 1999 horner pts/0 Apr 15 14:28 (csz096.cs.ust.hk) $

Running shell command - system (6) How would you look for the string “Bill Gates” in the file bill? $ grep "Bill Gates" bill Answer: system allows the first argument to be the command, and the remaining arguments to be the arguments to the command. system("grep", "Bill Gates", "bill");

Backquotes (1) How do you assign the output of a command to a variable? Answer: Use backquotes. $now = `date`; The value of $now will contain the result of the date commend (including the trailing newline).

Backquotes (2) Another example: $ cat back1 #!/usr/local/bin/perl5 -w $where = `pwd`; print "The current directory is: $where"; $ back1 The current directory is /homes/horner $

Backquotes (3) If the backquoted command is used in a list context, you get a list of strings, each one being a line (terminated in a newline). $ who horner pts/0 Apr 15 14:28 (csz096.cs.ust.hk) $ cat back2 #!/usr/local/bin/perl5 -w foreach $_ (`who`){ ($who, $where, $when) = /(\S+)\s+(\S+)\s+(.*)/; print "$who on $where at $when\n"; } $ back2 horner on pts/0 at Apr 15 14:28 (csz096.cs.ust.hk) $

Using Processes as Filehandles (input) If we use a pipe instead of backquote, the Perl program reads input from the previous pipe stage and is therefore more memory efficient and need not wait for the previous program to finish. $ cat whopipe #!/usr/local/bin/perl5 -w open(INFILE, "who|") || die("Cannot run who…\n"); while (<INFILE>) { ($who, $where, $when) =/(\S+)\s+(\S+)\s+(.*)/; print "$who on $where at $when\n"; }

Using Processes as Filehandles (1) Another way to run a shell command looks similar to filehandles, using open, close, and print. The following example allows you to customize a letter and automatically email it to different recipients: $ cat mail1 #!/usr/local/bin/perl5 -w open(FILE, "letter1"); # open the file @letter = <FILE>; # read the contents into @letter close(FILE); @emailList = qw(horner@cs.ust.hk horner@ust.hk); foreach $email (@emailList){ open(MAIL, "|mail -s \"hi!\" $email"); #open message print MAIL "Hi $email,\n"; # add header to message print MAIL @letter; # add content to message close MAIL; # send message! } $ mail1

Using Processes as Filehandles (2) This example prints the letter: $ cat print1 #!/usr/local/bin/perl5 -w open(FILE, "letter1"); # open the file @letter = <FILE>; # read the contents into @letter close(FILE); @nameList = qw(Andrew Bill Bob); foreach $name (@nameList){ open(PRINT, "|lpr -Pcll3"); # open print job print PRINT "Hi $name,\n"; # add header to job print PRINT @letter; # add content to job close PRINT; # send print job! } $ print1