Forget Shell Scripts Mike Schilli 06162008 Yahoo 1

  • Slides: 36
Download presentation
Forget Shell Scripts! Mike Schilli, 06/16/2008 Yahoo! 1

Forget Shell Scripts! Mike Schilli, 06/16/2008 Yahoo! 1

Overview l Pros and cons of shell/Perl scripts l How to whip up Perl

Overview l Pros and cons of shell/Perl scripts l How to whip up Perl scripts like Shell scripts l How to create reusable scripts in Perl 2

Where Shell Scripts shine l Fast to whip up l Running virtually everywhere (if

Where Shell Scripts shine l Fast to whip up l Running virtually everywhere (if you stick to /bin/sh syntax) 3

Where Shell Scripts shine l Don’t need to quote strings l Don’t need to

Where Shell Scripts shine l Don’t need to quote strings l Don’t need to separate arguments by commas: Shell: Perl: cmd a b cmd(“a”, “b”); 4

Where Shell Scripts shine l Don’t need to quote strings l Don’t need to

Where Shell Scripts shine l Don’t need to quote strings l Don’t need to separate arguments by commas: Shell: Perl: cmd a b sub cmd; cmd “a”, “b”; 5

Where Shell Scripts Suck l Can be written nicely, but mostly aren’t (see autoconf)

Where Shell Scripts Suck l Can be written nicely, but mostly aren’t (see autoconf) l Modularity l Debugging l Logging l No CPAN to share stuff with l Speed (interpreted line by line) 6

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “foo” echo $var 7

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “foo” echo $var 7

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “-n woah!” echo $var

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “-n woah!” echo $var 8

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “-n woah!” echo X”$var”

Where Shell Scripts Suck l Horrid syntax pitfalls: var = “-n woah!” echo X”$var” | sed ‘s/^X//’ (Source: “Gnu Autoconf, Automake, and Libtool”, Chapter 21, “Writing portable bourne shell”) 9

Shell Programming gone wild!!! 10

Shell Programming gone wild!!! 10

Shell Programming gone wild!!! 11

Shell Programming gone wild!!! 11

Challenges with Perl Scripts l Don’t run everywhere but Linux distros come with perl

Challenges with Perl Scripts l Don’t run everywhere but Linux distros come with perl already l but there might be missing CPAN modules l l Lots of typing (open/read/close, error handling) 12

Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm: : Install qw(cp); cp “foo”, “bar”; 13

Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm: : Install qw(cp); cp “foo”, “bar”; 13

Reusable one-off Perl scripts #!/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); cp

Reusable one-off Perl scripts #!/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); cp “foo”, “bar”; 14

Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm: : Install qw(: all); cp “foo”, “bar”;

Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm: : Install qw(: all); cp “foo”, “bar”; mv “bar”, “baz”; 15

Sysadm: : Install commands l l l l cp (copy) mv (move) untar (tar

Sysadm: : Install commands l l l l cp (copy) mv (move) untar (tar zxf …) slurp (file to string) blurt (string to file) cd (with cdback()) make (call make) 16

Sysadm: : Install commands l Run a shell command: ($stdout, $stderr, $rc) = tap

Sysadm: : Install commands l Run a shell command: ($stdout, $stderr, $rc) = tap “ls”, “-R”; 17

Sysadm: : Install commands l In-place-edit of a file: pie sub { s/foo/bar/; $_

Sysadm: : Install commands l In-place-edit of a file: pie sub { s/foo/bar/; $_ }, “file. dat”; l Process a file: plough sub { print if /foobar/ }, “file. dat”; 18

Sysadm: : Install commands l $answer = ask “Proceed”, “y”; Proceed [y]? l $picked

Sysadm: : Install commands l $answer = ask “Proceed”, “y”; Proceed [y]? l $picked = pick $prompt, @choices, 3 ; [1] Foo [2] Bar [3] Baz What now [3]? 19

Sysadm: : Install commands l rmf() l mkd() l perm_set() l perm_get() 20

Sysadm: : Install commands l rmf() l mkd() l perm_set() l perm_get() 20

Enable Logging #/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); use Log: :

Enable Logging #/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); use Log: : Log 4 perl (: easy); Log: : Log 4 perl->easy_init($DEBUG); cp “foo”, “bar”; cp “abc”, “xyz”; my $s = slurp “foo. dat”; 21

Enable Logging (output) $ foo-script 2008/06/10 01: 48: 14 cp abc def 2008/06/10 01:

Enable Logging (output) $ foo-script 2008/06/10 01: 48: 14 cp abc def 2008/06/10 01: 48: 14 cp ghi jkl 2008/06/10 01: 48: 14 slurp foo. dat [param 1: 123n … param 10: 345] 22

Raising errors by default (shell) #/bin/sh cp foo bar cp abc xyz 23

Raising errors by default (shell) #/bin/sh cp foo bar cp abc xyz 23

Raising errors by default #/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); cp

Raising errors by default #/usr/local/bin/perl –w use strict; use Sysadm: : Install qw(cp); cp “foo”, “bar”; #fails cp “abc”, “xyz”; $ foo-script Cannot copy abc to def (No such file or directory) at foo-script line 4 24

Keep it Clean l Exporting all vs. just what you need use Sysadm: :

Keep it Clean l Exporting all vs. just what you need use Sysadm: : Install qw(: all); l use Sysadm: : Install (cp untar); l 25

Install on Debian l apt-get install libsysadm-install-perl 26

Install on Debian l apt-get install libsysadm-install-perl 26

Additional Development Tips 27

Additional Development Tips 27

Use main() l Make a script reusable, put your code in main(): sub main

Use main() l Make a script reusable, put your code in main(): sub main { some_function(“a”); some_other_funtion(“b”); } 28

Use main() l Make a script reusable: # script 1 main() if !caller(); sub

Use main() l Make a script reusable: # script 1 main() if !caller(); sub main { some_function() } sub some_function { … } 1; 29

Use main() l Make a script reusable: # script 1 main() if !caller(); sub

Use main() l Make a script reusable: # script 1 main() if !caller(); sub main { some_function() } sub some_function { … } 1; l Somewhere else: # other script require “. /script 1”; some_function(); 30

Refactor l If a script turns out useful, refactor it to a module l

Refactor l If a script turns out useful, refactor it to a module l Use functions to encapsulate l Add quick docs (use templates) l Re-write the script to test the new module l Push to CPAN if useful 31

Get started quickly with new scripts l tmpl do-this-task l tmpl –p My. Task.

Get started quickly with new scripts l tmpl do-this-task l tmpl –p My. Task. pm l tmpl –pm Fully: : Qualified l http: //perlmeister. com/scripts/downloadl/tmpl l Use vim shortcuts 32

Portability tricks l PAR creates portable module bundles l PAR: : Packer creates self-contained

Portability tricks l PAR creates portable module bundles l PAR: : Packer creates self-contained executables: pp –o fooscript. exe fooscript l pp –M IO: : Zlib –o foo. exe foo l l Caveat: Same architecture l Similar systems: Use oldest to compile 33

The End l Proudly wear your “Say No To /bin/sh” logo 34

The End l Proudly wear your “Say No To /bin/sh” logo 34

The End Thanks! 35

The End Thanks! 35

The End l Slides: http: //perlmeister. com/talks. html 36

The End l Slides: http: //perlmeister. com/talks. html 36