Forget Shell Scripts - PowerPoint PPT Presentation

About This Presentation
Title:

Forget Shell Scripts

Description:

Forget Shell Scripts – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 37
Provided by: Yah982
Category:
Tags: forget | scripts | shell | var

less

Transcript and Presenter's Notes

Title: Forget Shell Scripts


1
Forget Shell Scripts!
Mike Schilli, 06/16/2008 Yahoo!
2
Overview
  • Pros and cons of shell/Perl scripts
  • How to whip up Perl scripts like Shell scripts
  • How to create reusable scripts in Perl

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

4
Where Shell Scripts shine
  • Dont need to quote strings
  • Dont need to separate arguments by commas
  • Shell cmd a b
  • Perl cmd(a, b)

5
Where Shell Scripts shine
  • Dont need to quote strings
  • Dont need to separate arguments by commas
  • Shell cmd a b
  • Perl sub cmd
  • cmd a, b

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

7
Where Shell Scripts Suck
  • Horrid syntax pitfalls
  • var foo
  • echo var

8
Where Shell Scripts Suck
  • Horrid syntax pitfalls
  • var -n woah!
  • echo var

9
Where Shell Scripts Suck
  • Horrid syntax pitfalls
  • var -n woah!
  • echo Xvar sed s/X//
  • (Source Gnu Autoconf, Automake, and Libtool,
    Chapter 21, Writing portable bourne shell)

10
Shell Programming gone wild!!!
11
Shell Programming gone wild!!!
12
Challenges with Perl Scripts
  • Dont run everywhere
  • but Linux distros come with perl already
  • but there might be missing CPAN modules
  • Lots of typing (open/read/close, error handling)

13
Reusable one-off Perl scripts
  • !/usr/local/bin/perl
  • use SysadmInstall qw(cp)
  • cp foo, bar

14
Reusable one-off Perl scripts
  • !/usr/local/bin/perl w
  • use strict
  • use SysadmInstall qw(cp)
  • cp foo, bar

15
Reusable one-off Perl scripts
  • !/usr/local/bin/perl
  • use SysadmInstall qw(all)
  • cp foo, bar
  • mv bar, baz

16
SysadmInstall commands
  • cp (copy)
  • mv (move)
  • untar (tar zxf )
  • slurp (file to string)
  • blurt (string to file)
  • cd (with cdback())
  • make (call make)

17
SysadmInstall commands
  • Run a shell command
  • (stdout, stderr, rc)
  • tap ls, -R

18
SysadmInstall commands
  • In-place-edit of a file
  • pie sub s/foo/bar/ _ ,
  • file.dat
  • Process a file
  • plough
  • sub print if /foobar/ ,
  • file.dat

19
SysadmInstall commands
  • answer ask Proceed, y
  • Proceed y?
  • picked pick prompt, \_at_choices, 3
  • 1 Foo
  • 2 Bar
  • 3 Baz
  • What now 3?

20
SysadmInstall commands
  • rmf()
  • mkd()
  • perm_set()
  • perm_get()

21
Enable Logging
  • /usr/local/bin/perl w
  • use strict
  • use SysadmInstall qw(cp)
  • use LogLog4perl (easy)
  • LogLog4perl-gteasy_init(DEBUG)
  • cp foo, bar
  • cp abc, xyz
  • my s slurp foo.dat

22
Enable Logging (output)
  • foo-script
  • 2008/06/10 014814 cp abc def
  • 2008/06/10 014814 cp ghi jkl
  • 2008/06/10 014814 slurp foo.dat param1 123\n
    param10 345

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

24
Raising errors by default
  • /usr/local/bin/perl w
  • use strict
  • use SysadmInstall 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

25
Keep it Clean
  • Exporting all vs. just what you need
  • use SysadmInstall qw(all)
  • use SysadmInstall (cp untar)

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

27
Additional Development Tips
28
Use main()
  • Make a script reusable, put your code in main()
  • sub main
  • some_function(a)
  • some_other_funtion(b)

29
Use main()
  • Make a script reusable
  • script1
  • main() if !caller()
  • sub main some_function()
  • sub some_function
  • 1

30
Use main()
  • Make a script reusable
  • script1
  • main() if !caller()
  • sub main some_function()
  • sub some_function
  • 1
  • Somewhere else
  • other script
  • require ./script1
  • some_function()

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

32
Get started quickly with new scripts
  • tmpl do-this-task
  • tmpl p MyTask.pm
  • tmpl pm FullyQualified
  • http//perlmeister.com/scripts/downloadl/tmpl
  • Use vim shortcuts

33
Portability tricks
  • PAR creates portable module bundles
  • PARPacker creates self-contained executables
  • pp o fooscript.exe fooscript
  • pp M IOZlib o foo.exe foo
  • Caveat Same architecture
  • Similar systems Use oldest to compile

34
The End
  • Proudly wear your Say No To /bin/sh logo ?

35
The End
  • Thanks!

36
The End
  • Slides http//perlmeister.com/talks.html
Write a Comment
User Comments (0)
About PowerShow.com