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.

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.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
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 Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
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 and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
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 Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
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.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
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.
Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
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.
CGI programming in Perl Learning Objectives: 1. To understand how a CGI program works in Perl and how to make it runnable in web browsers 2. To learn how.
More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
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.
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
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.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
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.
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.
CS465 - UNIX The Bourne Shell.
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.
Shell Programming. The UNIX Shell  The UNIX shell listens to what you type and executes commands at your request. User command: lpr file UNIX Shell UNIX.
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.
Chapter 4 LINUX Shells. Table 4.1 Shell Locations and Program Names.
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.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition
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.
Linux Administration Working with the BASH Shell.
Input from STDIN STDIN, standard input, comes from the keyboard.
The UNIX Shell Learning Objectives:
Perl I/O Learning Objectives:
Linux Shell Script Programming
Shell Programming.
Introduction to Perl Learning Objectives:
CSC 4630 Meeting 4 January 29, 2007.
Perl Process Management
Presentation transcript:

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 in a Perl program

COMP111 Lecture 23a / Slide 2 Table of Content  Running Shell Commands - system  Backquotes  Using Processes as Filehandles Perl Process Management

COMP111 Lecture 23a / Slide 3  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 (1)

COMP111 Lecture 23a / Slide 4  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 $ Running shell command - system (2)

COMP111 Lecture 23a / Slide 5  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 (3)

COMP111 Lecture 23a / Slide 6  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 (4)

COMP111 Lecture 23a / Slide 7  In the previous example, $where is replaced with its value (by Perl, not the shell).  If you want to reference a shell variable named $where, you need to put a backslash in front of the dollar sign.  The invoked shell command will inherit the current working directory from the Perl program. Running shell command - system (5)

COMP111 Lecture 23a / Slide 8  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"); Running shell command - system (6)

COMP111 Lecture 23a / Slide 9 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).

COMP111 Lecture 23a / Slide 10 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 $

COMP111 Lecture 23a / Slide 11 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) $

COMP111 Lecture 23a / Slide 12  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 ( ) { ($who, $where, $when) =/(\S+)\s+(\S+)\s+(.*)/; print "$who on $where at $when\n"; } Using Processes as Filehandles (input)

COMP111 Lecture 23a / Slide 13  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 it to different recipients: $ cat mail1 #!/usr/local/bin/perl5 -w open(FILE, "letter1");# open the = ;# read the contents = foreach $ open(MAIL, "|mail -s \"hi!\" $ "); #open message print MAIL "Hi $ ,\n"; # add header to message print # add content to message close MAIL; # send message! } $ mail1 Using Processes as Filehandles (1)

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