scripting languages james brucker computer engineering dept kasetsart university
TRANSCRIPT
![Page 1: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/1.jpg)
Scripting Languages
James Brucker
Computer Engineering Dept
Kasetsart University
![Page 2: Scripting Languages James Brucker Computer Engineering Dept Kasetsart University](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082712/56649f425503460f94c6196e/html5/thumbnails/19.jpg)
Assignment
Learn Python: readhttp://docs.python.org/tutorial
install Python interpreter on your notebook