Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");

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.
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.
Chapter 6 Dealing with quotes. Dealing with Quotes and slashes grep Susan phonebook grep Susan Goldberg phonebook  results in an error  grep ‘Susan.
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.
Perl File and Directory Access Software Tools. Slide 2 Changing Directories You can change the current working directory within Perl, just like the cd.
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.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
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”
CGI Programming in Perl Software Tools. Lecture 22 / Slide 2 CGI Programming l Last time we looked at designing a static web page. Today we will see how.
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.
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.
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.
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
Shell Script Examples.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
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.
An Introduction to Unix Shell Scripting
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.
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.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
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.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
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.
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Input from STDIN STDIN, standard input, comes from the keyboard.
The UNIX Shell Learning Objectives:
CSE 303 Concepts and Tools for Software Development
Perl I/O Learning Objectives:
Linux Shell Script Programming
Shell Programming.
More Shell Programming
Introduction to Perl Learning Objectives:
CSC 4630 Meeting 4 January 29, 2007.
Perl Process Management
Presentation transcript:

Perl Process Management Software Tools

Slide 2 system 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"; }

Slide 3 system l Where does the command output go? l 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 $

Slide 4 system l How to redirect the I/O? l 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 $

Slide 5 system 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) $

Slide 6 system 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. l The invoked shell command will inherit the current working directory from the Perl program.

Slide 7 system 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");

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

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

Slide 10 Backquotes l 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) $

Slide 11 Using Processes as Filehandles Another way to run a shell command looks similar to filehandles, using open, close, and print. l 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

Slide 12 Using Processes as Filehandles l 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 –Pcll2a"); # open print job print PRINT "Hi $name,\n"; # add header to job print # add content to job close PRINT; # send print job! } $ print1