Running external programs

Slides:



Advertisements
Similar presentations
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
COMP234 Perl Printing Special Quotes File Handling.
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.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
More CGI Programming Multiple Submits Cookies ing File Uploading.
Operating Systems Review. User Computer, including HW and SW.
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,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
How shell works Shell reads in input, parse & process it, the resulting string is taken to be argv[] and executed echo a  shell sees “ echo a ” echo gets.
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Processes & Daemons Chapter IV / Part III. Commands Internal commands: alias, cd, echo, pwd, time External commands, code is in a file: grep, ls, more.
Guide To UNIX Using Linux Third Edition
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Shell Script Examples.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
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.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
Introduction to Perl Yupu Liang cbio at MSKCC
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Institute for Visualization and Perception Research 1 © Copyright 1998 Haim Levkowitz Perl primer... Syntax similar to C, various shell script langs. 3.
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.
An Overview of Perl A language for Systems and Network Administration and Management: An overview of the language.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
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.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 9 Acknowledgements: The syllabus and power point presentations are modified versions.
Process Management Azzam Mourad COEN 346.
CSCI 330 UNIX and Network Programming
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Introduction to the Linux Commandline
Shell Control Structures
UNIX Review CS 2204 Class meeting 15.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
Lecture 7 You’re on your own now...
UNIX PROCESSES.
CSE 303 Concepts and Tools for Software Development
Using Linux Commands Lab 3.
Sarah Diesburg Operating Systems CS 3430
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Fork and Exec Unix Model
Process Programming Interface
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
Module 6 Working with Files and Directories
More Variables and Data Types: references/matricies
CSC 352– Unix Programming, Fall 2012
Chapter 3 The UNIX Shells
Security Checks Using Perlcritic
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

Running external programs back-ticks, system(), and pipes

OpSys – like functions Those of you who have taken CSCI-4210, Operating Systems, should be aware that Perl has the OpSys functions you would expect: fork(), exec(), waitpid(), etc We will not be discussing these functions I don’t want to be responsible for the results feel free to examine perldoc perfunc for the full listing We may cover them during the "Advanced Perl Topics" lecture towards the end of the semester.

system(CMD, LIST) system(CMD_and_ARGS) Takes a pathname, followed by a list of arguments. Executes the program specified by the pathname To use shell meta-characters, give only one argument – a string containing the path and arguments. system ('myprog.pl', 'input.txt'); system ('myprog.pl ~john/input.txt'); Returns the return value of the command that was executed….. and some more You have to shift the result right 8 bits my $ret = system( … ); $ret = $ret >> 8;

backticks Third kind of quoting operator Enclose a pathname. Executes the command specified by the pathname Returns the output of that command. single string in scalar context one line per element in list context my @files = `ls –al`; @files contains one file listing per element

more on backticks Just like double quotes, backticks are subject to interpolation. $cmd = 'prog1.pl'; $output = `$cmd –h`; like q// for single quotes, and qq// for double quotes, backticks can be represented by qx// same delimiter rules apply

Pipes the open function can link a filehandle to a running process, instead of a file. To write to a process, specify mode of '|-' To read from a process, specify mode of '-|' open my $mh, '|-', '/bin/sendmail' or die(…); open my $ls, '-|', 'ls –al' or die(…); Alternatively (but not recommended), 2-arg version: open my $mh, '| /bin/sendmail' or die(…); open my $ls, 'ls –al |' or die (…); Once pipes have been opened, read/write them just as any other filehandle. while (<$ls>) { print $mh "File: $_"; }

Piping issues Why pipe instead of using ` `? with pipes, you can read one line of output at a time, and terminate process at any time, using close() Opening a process for bi-directional communication is more complicated. Involves using the IPC modules. See Camel page 430 for more info. perldoc –f open perldoc perlopentut perldoc IPC::Open2