Scripting Languages James Brucker Computer Engineering Dept Kasetsart
Scripting Languages James Brucker Computer Engineering Dept Kasetsart University
Origins n n 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/spool/mqueue" for d in $DIRS; do find $d -atime +7 -type f -exec rm { } ; done
Characteristics of Scripts n Issue commands to other program(s). n Shell scripts: run operating system commands n Embedded script language: customize behavior of an application Example: VBA for MS Office Lisp for Emacs
Original Unix Script Languages n n n /bin/sh – Bourne shell /bin/bash – Bourne-again shell /bin/csh – C-shell (syntax is like C) Using helper commands: n grep - find strings in files n sed - stream editor n find - search for files n dirname, filename
Unix Toolkits for Windows n n Cygwin: http: //www. cygwin. com MSYS: http: //www. mingw. org
DOS "bat" (batch) files are scripts n Print the <title>. . . </title> line in all HTML files. FINDSTR /I /R "<title>. *</title>" *. html
Adding useful message to Batch file n n 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 h 1 n
Perl n n n 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 n almost no type checking n scripts can be difficult to read n not built on O-O foundation
Perl: download and save a URL n n 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 n 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 $filen"; } else { print "Failed. HTTP response code $rcn"; }
Python: an O-O language n n n Designed as O-O language Supports many common data structures Good C API Named after Monty Python Libraries almost as extensive as Perl Disadvantages n uses indentation to denote blocks & scope n unusual syntax
Python: download a URL #!/usr/bin/python import urllib import sys if (len(sys. argv) < 2): print "Missing URL namen" 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 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 get. Class( ) method print type( santa )
Ruby and Rails n n n Modern script language with many ideas from Perl Completely O-O Killer app: Rails web framework Disadvantages n slow n 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 n n 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 n n Learn Python: read http: //docs. python. org/tutorial install Python interpreter on your notebook
- Slides: 19