scripting languages james brucker computer engineering dept kasetsart university

19
Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Upload: bruce-davis

Post on 14-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Scripting Languages

James Brucker

Computer Engineering Dept

Kasetsart University

Page 2: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Origins

Used to create a list of commands for the operating system to execute.

Automate repetative tasks.

# Delete all files in the /tmp directory that

# have not been used in 7 days.

find /tmp -atime +7 -type f -exec rm { } \;

File: cleantemp.sh

shell> cleantemp.sh

Page 3: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

And the script grows...

# Delete all files in the /tmp and /var/tmp that

# have not been used in 7 days.

find /tmp -atime +7 -type f -exec rm { } \;

find /var/tmp -atime +7 -type f -exec rm { } \;

# Delete all files in several directories

# have not been used in 7 days.

DIRS="/tmp /var/tmp /var/spool/mqueue"

for d in $DIRS; do

find $d -atime +7 -type f -exec rm { } \;

done

Page 4: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Characteristics of Scripts

Issue commands to other program(s).

Shell scripts: run operating system commands

Embedded script language: customize behavior of an applicationExample: VBA for MS Office Lisp for Emacs

Page 5: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Original Unix Script Languages

/bin/sh – Bourne shell /bin/bash – Bourne-again shell /bin/csh – C-shell (syntax is like C)

Using helper commands: grep - find strings in files sed - stream editor find - search for files dirname, filename

Page 6: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Unix Toolkits for Windows

Cygwin: http://www.cygwin.com MSYS: http://www.mingw.org

Page 7: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

DOS "bat" (batch) files are scripts

Print the <title>...</title> line in all HTML files.

FINDSTR /I /R "<title>.*</title>" *.html

Page 8: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Adding useful message to Batch file

Print help if no parameters given Print a message if no matching files.

@ECHO OFF

IF "x%1" == "x" (

@ECHO Usage: showtitles filespac [...]

GOTO END

)

FINDSTR /I /R "<title>.*</title>" %*

IF ERRORLEVEL 1 @ECHO No match found

:END

Page 9: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Exercise

Write a BAT file to find all occurrences of a given HTML tag.

Example: findtags h1

Page 10: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Perl

Practical Extraction and Reporting Language Originally for systems admin work on Unix Often used for "CGI" web applications Excellent text processing (regex) support Extensive libraries: CPAN network

Disadvantages almost no type checking scripts can be difficult to read not built on O-O foundation

Page 11: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Perl: download and save a URL

Download a URL from the web and save it to a local file... without using a web browser!

Prerequisite: install LWP module

shell> cpan install LWP

Page 12: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Perl: download and save a URL

uses LWP getstore( url, filename) function

#!/usr/bin/perl# Download a URL and save as a fileuse LWP::Simple;

# get filename from URL$url = $ARGV[0];if ( $url =~ /.*\/([^\/]+)$/ ) { $file = $1; }else { $file = "noname.html"; }

$rc = getstore( $url, $file);

if ( is_success( $rc ) ) { print "URL saved as $file\n"; }else { print "Failed. HTTP response code $rc\n"; }

Page 13: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Python: an O-O language

Designed as O-O language Supports many common data structures Good C API Named after Monty Python Libraries almost as extensive as Perl

Disadvantages uses indentation to denote blocks & scope unusual syntax

Page 14: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Python: download a URL

#!/usr/bin/pythonimport urllibimport sys

if (len(sys.argv) < 2): print "Missing URL name\n" sys.exit(-1)

# get a handle for URLurl = urllib.urlopen( sys.argv[1] )# open output filef = open("somefile.html", "w")f.write( url.read() )f.close()url.close( )

Page 15: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Python: define a class

class Person:# constructordef __init__(self, name):

salf.name = name

# greet the persondef greet(self):

print "Hello " + self.name

#end of the classsanta = Person("Santa Claus")santa.greet()# get data type - like Java getClass( ) methodprint type( santa )

Page 16: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Ruby and Rails

Modern script language with many ideas from Perl

Completely O-O Killer app: Rails web framework

Disadvantages slow weird syntax and multiple commands to do

same thing

Page 17: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Ruby: 4 ways to join strings

s = "hello " "world"t = "hello " + " world"u = "hello ".concat(" world")v = "hello " << " world"print sprint(t) # parenthesis are optional

Page 18: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Groovy

Interpreted language based on Java syntax Can import and use any Java class Groovy compiler (groovyc) generates

Java .class files! Killer app: Grails web framework

Page 19: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

Assignment

Learn Python: readhttp://docs.python.org/tutorial

install Python interpreter on your notebook