Day 11: System Interaction suggested reading: Learning Perl (4th - - PowerPoint PPT Presentation

day 11 system interaction
SMART_READER_LITE
LIVE PREVIEW

Day 11: System Interaction suggested reading: Learning Perl (4th - - PowerPoint PPT Presentation

CS 368 Intro to Scripting Languages Day 11: System Interaction suggested reading: Learning Perl (4th Ed.), Chapter 14: Process Management Summer 2009 Cartwright, De Smet, LeRoy 1 CS 368 Intro to Scripting Languages Homework Review


slide-1
SLIDE 1

CS 368 – Intro to Scripting Languages

Day 11: System Interaction

suggested reading: Learning Perl (4th Ed.), Chapter 14: Process Management

Summer 2009 1 Cartwright, De Smet, LeRoy

slide-2
SLIDE 2

CS 368 – Intro to Scripting Languages

Homework Review

Summer 2009 Cartwright, De Smet, LeRoy 2

slide-3
SLIDE 3

CS 368 – Intro to Scripting Languages

Problem

Summer 2009 Cartwright, De Smet, LeRoy 3

DB App #1 App #2 File

?

slide-4
SLIDE 4

CS 368 – Intro to Scripting Languages

Problem

Summer 2009 Cartwright, De Smet, LeRoy 4

Perl script DB App #1 App #2 File

slide-5
SLIDE 5

CS 368 – Intro to Scripting Languages

Arguments

  • $0 : command name
  • @ARGV : command-line arguments

Summer 2009 Cartwright, De Smet, LeRoy 5

#!/usr/bin/perl use strict; use warnings; foreach my $arg (@ARGV) { # process argument } print "$0: arguments parsed\n";

slide-6
SLIDE 6

CS 368 – Intro to Scripting Languages

Arguably Better Arguments

Summer 2009 Cartwright, De Smet, LeRoy 6

% do-stuff -aXb --lo --with foo file1 use Getopt::Long; my $lo = 0; my $with = ''; GetOptions('lo' => \$lo, 'with=s' => \$with, # etc.); my $file = $ARGV[0];

slide-7
SLIDE 7

CS 368 – Intro to Scripting Languages

Environment

  • %ENV : environment variables
  • readable and writable

Summer 2009 Cartwright, De Smet, LeRoy 7

my @path = split(/:/, $ENV{'PATH'}); my @new = grep(! m{/sbin}, @path); $ENV{PATH} = join(':', @new); delete $ENV{'BLAH'};

slide-8
SLIDE 8

CS 368 – Intro to Scripting Languages

Running a Command

system()

  • May create a shell to parse command
  • Waits for command to finish
  • Command inherits environment, etc.

Summer 2009 Cartwright, De Smet, LeRoy 8

system("gzip $output_file");

(does a fork & exec, FWIW)

slide-9
SLIDE 9

CS 368 – Intro to Scripting Languages

Sneaky system() Subtleties

  • To shell or not to shell?

Summer 2009 Cartwright, De Smet, LeRoy 9

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

  • Children do not affect parent

system('pwd'); system('cd subdirectory'); system('pwd');

slide-10
SLIDE 10

CS 368 – Intro to Scripting Languages

Sneaky system() Subtleties II

  • Watch out for quoting

Summer 2009 Cartwright, De Smet, LeRoy 10

system("echo 'don'\\''t say \"no\"!'");  echo 'don'\''t say "no"!'  don't say "no"!

slide-11
SLIDE 11

CS 368 – Intro to Scripting Languages

Return Values

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

Summer 2009 Cartwright, De Smet, LeRoy 11

system(…) && die('FAIL!'); # confusing

  • r

!system(…) || die('FAIL!'); # may miss !

  • So, strive for clarity:

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

slide-12
SLIDE 12

CS 368 – Intro to Scripting Languages

Errors

  • return value ≡ $? ≡ exit code << 8
  • $? == -1 means it failed to execute
  • $! is the system error message

Summer 2009 Cartwright, De Smet, LeRoy 12

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

slide-13
SLIDE 13

CS 368 – Intro to Scripting Languages

Getting Output

  • Like system() but returns stdout
  • $? and $! are set in the same way
  • Need stderr, too?

Summer 2009 Cartwright, De Smet, LeRoy 13

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

  • List context: one element per line
  • Backticks interpolate!

my @files = `find $directory`;

slide-14
SLIDE 14

CS 368 – Intro to Scripting Languages

Miscellaneous

Summer 2009 Cartwright, De Smet, LeRoy 14

  • Quit script with given exit code

exit 1; die('message');

  • Print to standard error

print STDERR 'message';

  • Exit nonzero with message
slide-15
SLIDE 15

CS 368 – Intro to Scripting Languages

When To Use

  • To glue existing systems together
  • As a more powerful shell
  • NOT to replace Perl functions!

– I.e., `date` is probably a bad idea

  • Examples from my work:

– Automate a build – Run configuration scripts during install – Check command output for failure

  • Your ideas?

Summer 2009 Cartwright, De Smet, LeRoy 15

slide-16
SLIDE 16

CS 368 – Intro to Scripting Languages

Security Considerations

  • BE CAREFUL!
  • Especially with non-literal commands
  • use taint; may help (but is hard)
  • what could go wrong with this?

Summer 2009 Cartwright, De Smet, LeRoy 16

system("mv $foo $bar");

slide-17
SLIDE 17

CS 368 – Intro to Scripting Languages

Other Scripting Languages

  • 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, JS, …)

– Expect more restrictions

Summer 2009 Cartwright, De Smet, LeRoy 17