Download Scripting - Department of Computer Engineering

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
File: cleantemp.sh
# Delete all files in the /tmp directory that
# have not been used in 7 days.
find /tmp -atime +7 -type f -exec rm { } \;
shell>
cleantemp.sh
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
Characteristics of Scripts

Issue commands to other program(s).

Shell scripts:
run operating system commands

Embedded script language:
customize behavior of an application
Example: VBA for MS Office
Lisp for Emacs
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
Unix Toolkits for Windows


Cygwin: http://www.cygwin.com
MSYS: http://www.mingw.org
DOS "bat" (batch) files are scripts

Print the <title>...</title> line in all HTML files.
FINDSTR /I /R "<title>.*</title>" *.html
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
Exercise
Write a BAT file to find all occurrences of a
given HTML tag.
Example: findtags h1

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
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
Perl: download and save a URL

uses LWP getstore( url, filename) function
#!/usr/bin/perl
# Download a URL and save as a file
use 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"; }
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
Python: download a URL
#!/usr/bin/python
import urllib
import sys
if (len(sys.argv) < 2):
print "Missing URL name\n"
sys.exit(-1)
# get a handle for URL
url = urllib.urlopen( sys.argv[1] )
# open output file
f = open("somefile.html", "w")
f.write( url.read() )
f.close()
url.close( )
Python: define a class
class Person:
# constructor
def __init__(self, name):
salf.name = name
# greet the person
def greet(self):
print "Hello " + self.name
#end of the class
santa = Person("Santa Claus")
santa.greet()
# get data type - like Java getClass( ) method
print type( santa )
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
Ruby: 4 ways to join strings
s = "hello " "world"
t = "hello " + " world"
u = "hello ".concat(" world")
v = "hello " << " world"
print s
print(t) # parenthesis are optional
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
Assignment


Learn Python: read
http://docs.python.org/tutorial
install Python interpreter on your notebook