Day 11: System Interaction Suggested reading: Learning Perl (6th Ed.) - - PowerPoint PPT Presentation

day 11 system interaction
SMART_READER_LITE
LIVE PREVIEW

Day 11: System Interaction Suggested reading: Learning Perl (6th Ed.) - - PowerPoint PPT Presentation

Computer Sciences 368 Introduction to Perl Day 11: System Interaction Suggested reading: Learning Perl (6th Ed.) Chapter 16: Process Management 2012 Summer Cartwright 1 Computer Sciences 368 Introduction to Perl Homework Review 2012 Summer


slide-1
SLIDE 1

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Day 11: System Interaction

Suggested reading: Learning Perl (6th Ed.) Chapter 16: Process Management

1

slide-2
SLIDE 2

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework Review

2

slide-3
SLIDE 3

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Problem

3

DB File App #1 App #2

?

slide-4
SLIDE 4

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

(One) Solution

4

DB File App #1 App #2 Perl Script

slide-5
SLIDE 5

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Introducing … The Shell

5

slide-6
SLIDE 6

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

The Shell

6

  • Is a program (e.g., /bin/sh) – interactive or scripted

– Another interpreted scripting language (like Perl)

while (1) { print '% '; my $cmd = <STDIN>; do($cmd); }

  • Mostly, finds & runs other programs
  • Has variables, control structures, functions, …

if [ $# -lt 3 ]; then echo 'Too few args!'; fi

  • Allows control over input and output

gcc foo.c -o foo > compile.log 2> errors.log

  • Supports basic workflows

grep '^[0-9]' file.txt | sort | uniq -c

slide-7
SLIDE 7

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Common Shell Commands

7

Built-in

cd, echo

Manual pages

man

Show file entries

ls, find

Change file privileges

chown, chmod

Manipulate whole files

cp, mv, rm, ln, unlink

Manipulate directories

mkdir, rmdir

View files

cat, more, less, wc

Filter/change files

grep, sed, awk

Edit text

pico/nano, vi, emacs

Scripting

perl, python, ruby

slide-8
SLIDE 8

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

8

Shells and Commands

% foo -x

command line (shell)

PWD: ~/scripts PATH: /usr/bin:/usr/local/bin:…

chdir 'data'; system('bar');

foo (Perl)

PWD: ~/scripts/data PATH: /usr/bin:/usr/local/bin:…

printf("hi\n"); exit(2);

bar (C)

PWD: ~/scripts/data PATH: /usr/bin:/usr/local/bin:…

%

slide-9
SLIDE 9

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

The System ⇒ Your Script

9

slide-10
SLIDE 10

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

How to Get Input Into Your Script

10

  • Define (hard-code) input in script

+ Easy to code – Must change script to change input

  • Ask user for terminal input

+ Easy to code, no script changes each run – Cannot automate (easily) — user must type input each run

  • Read input from files

+ No script changes each run, can automate – Must parse file contents; separate file to manage

  • Accept command-line arguments

+ No script changes each run, can automate – Must parse arguments; cumbersome for many or long inputs

  • Read environment variables

+ No script changes each run, can automate – Must parse arguments; cumbersome for many or long inputs

slide-11
SLIDE 11

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Command-Line Arguments

11

$0

command (script) name

@ARGV

command-line arguments

#!/usr/bin/perl use strict; use warnings; if (scalar(@ARGV) != 3) { die "usage: $0 PATH VALUE NAME\n"; } my ($path, $value, $name) = @ARGV; % script.pl data.txt 1200 'Tim Cartwright'

slide-12
SLIDE 12

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

(Required) Arguments and Options

12

use Getopt::Long; my $lo = 0; my $with = ''; GetOptions('lo' => \$lo, 'with=s' => \$with, # etc.); my $file = $ARGV[0]; # do after GetOptions

  • Use Getopt::Long for options (starting with -)
  • Required arguments remain in @ARGV

% script.pl --lo --with foo -aXb file1

slide-13
SLIDE 13

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Environment

  • %ENV : environment variables
  • Readable and writable

13

while (my ($key, $value) = each %ENV) { print "$key => $value\n"; } my @path = split(':', $ENV{'PATH'}); my @new = grep(! m{/sbin}, @path); $ENV{'PATH'} = join(':', @new); delete $ENV{'SOME_OTHER_VAR'};

slide-14
SLIDE 14

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Your Script ⇒ The System

14

slide-15
SLIDE 15

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Running a Command

15

system(…)

  • Runs the given command as a subprocess
  • May create a shell to parse command
  • Waits for command to finish
  • Command inherits environment, etc.

system("gzip $output_file"); system('Rscript', 'foo.R', 1200, 42); system("octave test-script > my_oct.log");

slide-16
SLIDE 16

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Sneaky system() Subtleties

Create a shell?

16

system('ls'); # no system('ls >> my_file'); # yes system('ls', '>>', 'my_file'); # no

Children do not affect parent

system('pwd'); # => /home/cat/foo system('cd bar'); system('pwd'); # => ???

slide-17
SLIDE 17

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Sneaky system() Subtleties II

Watch out for quoting issues

17

system("echo 'Don'\\''t say \"no\"!'"); [shell] echo 'Don'\''t say "no"!' [output] Don't say "no"!

slide-18
SLIDE 18

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Return Values

  • system() returns exit code × 256 (<< 8)
  • Exit code of 0 is good in shell,
  • But 0 is false in Perl, so…

18

system(…) and die('fail'); # confusing !system(…) or die('fail'); # may miss !

  • … instead, strive for clarity:

system(…) == 0 or die('fail'); if (system(…) != 0) { die('fail'); }

slide-19
SLIDE 19

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Errors

  • system() return value ≡ $? ≡ exit code << 8
  • $? == -1 means it failed to execute
  • $! is the system error message
  • Rarely need to be this thorough:

19

if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { print "died with signal"; } else { print "exited " . ($? >> 8) . "\n"; }

slide-20
SLIDE 20

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Getting Output

20

my $sys_date = `date`;

  • Like system() but returns standard output
  • $? and $! are set in the same way

my $sys_date = `date 2>&1`;

  • In list context, returns list of lines in output
  • Backticks interpolate!

my @files = `find $directory`;

  • Need standard error, too?
slide-21
SLIDE 21

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Miscellaneous

Quit script with given exit code (0 = good)

21

exit 1;

Print message to standard error and exit (non-zero)

die 'message';

Print message to standard error

print STDERR 'message'; warn 'message';

slide-22
SLIDE 22

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

When To Use

  • Glue between existing commands
  • More powerful shell
  • Workflow management
  • Not to replace Perl functions!

– E.g., do not actually use `date`

  • Your ideas?

22

slide-23
SLIDE 23

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Last 2 Slides…

23

slide-24
SLIDE 24

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Other Scripting Languages

24

  • In command-line scripts, expect

– Command-line arguments – Environment – Standard in, out, and error – System calls – Exit with status – Windows will be different…

  • Embedded scripting (PHP, Lua, JavaScript, …)

– Expect more restrictions

slide-25
SLIDE 25

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework

  • Find IP addresses for image downloads
  • Use external commands for:

– Fetching an HTTP document (web page) – Looking up IP addresses

  • Middle section will involve regular expressions

25