1 / 19

Scripting Languages

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

axl
Download Presentation

Scripting Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scripting Languages James Brucker Computer Engineering Dept Kasetsart University

  2. 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

  3. 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

  4. 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

  5. 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

  6. Unix Toolkits for Windows • Cygwin: http://www.cygwin.com • MSYS: http://www.mingw.org

  7. DOS "bat" (batch) files are scripts • Print the <title>...</title> line in all HTML files. FINDSTR /I /R "<title>.*</title>" *.html

  8. 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

  9. Exercise • Write a BAT file to find all occurrences of a given HTML tag. Example: findtags h1

  10. 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

  11. 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

  12. 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"; }

  13. 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

  14. 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( )

  15. 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 )

  16. 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

  17. 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

  18. 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

  19. Assignment • Learn Python: readhttp://docs.python.org/tutorial • install Python interpreter on your notebook

More Related