day 11 system interaction
play

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


  1. 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

  2. CS 368 – Intro to Scripting Languages Homework Review Summer 2009 Cartwright, De Smet, LeRoy 2

  3. CS 368 – Intro to Scripting Languages Problem DB File ? App App #1 #2 Summer 2009 Cartwright, De Smet, LeRoy 3

  4. CS 368 – Intro to Scripting Languages Problem DB File Perl script App App #1 #2 Summer 2009 Cartwright, De Smet, LeRoy 4

  5. CS 368 – Intro to Scripting Languages Arguments • $0 : command name • @ARGV : command-line arguments #!/usr/bin/perl use strict; use warnings; foreach my $arg (@ARGV) { # process argument } print "$0: arguments parsed\n"; Summer 2009 Cartwright, De Smet, LeRoy 5

  6. CS 368 – Intro to Scripting Languages Arguably Better Arguments % 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]; Summer 2009 Cartwright, De Smet, LeRoy 6

  7. CS 368 – Intro to Scripting Languages Environment • %ENV : environment variables • readable and writable my @path = split(/:/, $ENV{'PATH'}); my @new = grep(! m{/sbin}, @path); $ENV{PATH} = join(':', @new); delete $ENV{'BLAH'}; Summer 2009 Cartwright, De Smet, LeRoy 7

  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. system("gzip $output_file"); (does a fork & exec, FWIW) Summer 2009 Cartwright, De Smet, LeRoy 8

  9. CS 368 – Intro to Scripting Languages Sneaky system() Subtleties • To shell or not to shell? # no system('ls'); # yes system('ls >> my_file'); system('ls', '>>', 'my_file'); # no • Children do not affect parent system('pwd'); system('cd subdirectory'); system('pwd'); Summer 2009 Cartwright, De Smet, LeRoy 9

  10. CS 368 – Intro to Scripting Languages Sneaky system() Subtleties II • Watch out for quoting system("echo 'don'\\''t say \"no\"!'");  echo 'don'\''t say "no"!'  don't say "no"! Summer 2009 Cartwright, De Smet, LeRoy 10

  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… system(…) && die('FAIL!'); # confusing or !system(…) || die('FAIL!'); # may miss ! • So, strive for clarity: system(…) == 0 or die(); if (system(…) != 0) { die(); } Summer 2009 Cartwright, De Smet, LeRoy 11

  12. CS 368 – Intro to Scripting Languages Errors • return value ≡ $? ≡ exit code << 8 • $? == -1 means it failed to execute • $! is the system error message if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { print "died with signal"; } else { print "exited " . ($? >> 8) . "\n"; } Summer 2009 Cartwright, De Smet, LeRoy 12

  13. CS 368 – Intro to Scripting Languages Getting Output my $sys_date = `date`; • Like system() but returns stdout • $? and $! are set in the same way • Need stderr, too? my $sys_date = `date 2>&1`; • List context: one element per line • Backticks interpolate! my @files = `find $directory`; Summer 2009 Cartwright, De Smet, LeRoy 13

  14. CS 368 – Intro to Scripting Languages Miscellaneous • Quit script with given exit code exit 1; • Exit nonzero with message die('message'); • Print to standard error print STDERR 'message'; Summer 2009 Cartwright, De Smet, LeRoy 14

  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

  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? system("mv $foo $bar"); Summer 2009 Cartwright, De Smet, LeRoy 16

  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

Recommend


More recommend