1 / 67

Introduction to Scripting

Introduction to Scripting. AE 6382. What is a scripting. Scripting is the process of programming using a scripting language A scripting language, like C, Fortran, and Java, has expressions, conditional statements, and loop statements. Unlike C, Fortran, and Java a scripting language has

sailor
Download Presentation

Introduction to Scripting

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. Introduction to Scripting AE 6382

  2. What is a scripting • Scripting is the process of programming using a scripting language • A scripting language, like C, Fortran, and Java, has expressions, conditional statements, and loop statements. • Unlike C, Fortran, and Java a scripting language has • Loose typing • Interpreted rather than compiled • Usually as some higher level abstractions or built-in functionality

  3. Scripting Features • Scripting languages are generally interpreted rather than compiled • This results in slower execution times compared to a compiled language • C and Fortran are generally fastest • Java is compiled to bytecode that runs on a virtual machine and is slower • The implementation of each individual interpreter varies greatly • Perl, Python, and Ruby are compiled at runtime into an internal format that increases performance • Shell scripts and MATLAB re-evaluate each statement every time • Development cycle is shortened – edit/run

  4. Scripting Features • Scripting languages do not, in general, use strong typing of variables • A variable may during the course of execution contain strings, integers, and objects • Scripting languages frequently build into the basic language higher order abstractions • Text processing • Regular expressions • System interface mechanisms • Most scripting interpreters can be embeddedt into other programs to provide scripting capability within that program • Microsoft Office uses Visual Basic for Applications

  5. Scripting Languages • Simple command/shell scripting, level 1, is the simplest form of scripting • Intended to provide a “batch” execution capability • Unix/Linux • bash, ksh (Korn shell), sh (Bourne shell) • csh (C shell), tsch • These shells can work interactively or in script mode • Have basic programming constructs (if, loops, …) • Windows • cmd/command have no programming constructs • Windows PowerShell (4Q2006) will have extensive scripting based on C# language

  6. Scripting Languages • Limited scripting languages, level 2, have more sophisticated language structure but are limited in their native functionality • No native file I/O capability for example • JavaScript / Jscript / ECMAScript • Available on Unix/Linux and Windows • C based syntax • Used almost exclusively as the client-side scripting language in the various web browsers • Can be used as a system scripting language in Windows via the Windows Scripting Host • Not generally used in Unix/Linux for general purpose scripting • SpiderMonkey is C based JS interpreter • Rhino is Java based JS interpreter

  7. Scripting Languages • VBScript • Available only on Windows, based on Visual Basic for Applications (VBA) • Can be used as the client-side scripting language in Internet Explorer • Most often used with Windows Active Server Pages (ASP) for IIS based web sites • Can be used as a system scripting language in Windows via the Windows Scripting Host

  8. Scripting Languages • Full scripting languages, level 3, have a sophisticated language structure and extensive application support • Perl – Practical Extraction and Reporting Language • A procedure based language with support for objects • Extensive text processing capabilities and regular expressions • Extensible using modules • C based syntax with plethora of symbols • Developed in late 1980’s • Python (also Jython) • An object oriented language with some procedure traits • Extensible • A format based syntax • Developed in early 1990’s

  9. Scripting Languages • Ruby • An object oriented language • Extensible • C like syntax with minimal symbols (no {} () …) • Developed in early 1990’s • TCL – Tool Command Language • A procedural language • Extensible • A stack evaluation syntax, similar to Lisp (lots of []) • Developed as an embeddable scripting language • Developed in late 1980’s

  10. Scripting Languages • Other niche scripting languages • BeanShell • Makes it possible to use Java as a scripting language • REXX • C-like, objects, cross-platform, has a Java version

  11. Perl • General purpose scripting language – Practical Extraction and Reporting Language • Based on the Unix program awk in its early incarnation • Runs everywhere • On Unix/Linux it runs standalone using #! script file convention • On Windows it can run standalone or as an ActiveX scripting engine • Pros • Extensive text processing capabilities including built-in regular expressions • Can be easily extended, there is extensive support for all types of system programming • Has syntax to support object based programming • Most Unix system calls are built-in functions • The built-in system calls will do the right thing in Windows

  12. Perl • Cons • Can be difficult for beginners to learn • Variable naming scheme is initially confusing • There is a high learning curve

  13. Perl • Has 3 classes of variables • $var - scalar (integer, real, string, ...) • @var - array of scalars, $var[0] • %var - hash of scalars, $var{key} • Has local, lexical, and global scoping of variables • Namespace separation • Objects and references are supported • Has the same set of operators as C plus some • Lexical and global scoping of variables • Statements end with ; • Comments are everything after # on a line • Functions sub name { ... }

  14. Perl • Has the same set of operators as C plus some additional • Statements end with ; (semi-colon) • Comments are everything after # on a line • The usual complement of conditional statements • if – then – else (also unless – then – else) • The usual loop statements • for • for each • while • Functions and methods are defined similarly • sub name (…) { … }

  15. Perl • Loop statements for ($i=0 ; $i < 10 ; $i++) { printf “i=%4d\n”,$i; } while (<STDIN>) { print; } @list = (0,5,8,12); foreach $value (@list) { print “Value=$value\n”; } foreach $value (0,5,8,12) { print “Value=$value\n”; } %hash = (part1=>0,part3=>70,part2=>4); foreach $key (sort keys %hash) { print “Value=$hash{$key}\n”; }

  16. Perl • Logical statements if ($i == 1) { print “i=$i\n”; $i++; } unless ($i == 1) { print “Error: i != 1\n”; $i++; } die “Unable to open file” if !open(IN,”filename”); if ($i == 1) { print “Group 1\n”; } else { print “Unknown group\n”; } if ($i == 1) { print “Group 1\n”; } elsif ($i == 2) { print “Group 2\n”; } elsif ($i == 3) { print “Group 3\n”; } else { print “Unknown group\n”; }

  17. Perl • Native Regular Expression Support while (<>) { next if m/.*error.*/; print; } foreach $line (@lines) { next if $line =~ m/^#/; @values = ($line =~ m/.+a=([0-9]+).+c=([0-9]+)/); print “$values[0] $values[1]\n”; } @lines contains (an array of strings): # a b c a=10 b=23 c=16 a=12 b=43 c=17 a=63, b=2, c=999

  18. Perl • Support for objects • use Modulename; (include class definition) • $var = Modulename::new(); (instantiate object) • $var->method(...); (invoke method) • $var->{property}; (access property) • Does not have a class keyword, a class is defined as a Perl module where the functions are invoked as methods and the use of the bless keyword.

  19. Script56.chm is the Windows scripting documentation file Local copy http://www.ae.gatech.edu/classes/ae6382/MS_scripting/ M/S Scripting Documentation

  20. JavaScript / JScript • General purpose scripting language • Usually appears only in web browsers • Available on most platforms • In Windows Jscript is available as an ActiveX scripting engine, when run under the Windows Scripting Host it can functions as a general scripting system • Pros • Its syntax is very much like C • It has support for objects • Cons • Limited availability • Has limited access to host system (security feature)

  21. JavaScript / JScript • Variables • Typeless, refer to primitive types and objects • Can be arrays • Declared with var statement • Uses the usual set of C operators with some additions • Statements are terminated with ; • Comments marked with // and /* ... */ • Functions and methods are declared with function name (...) { ... }

  22. JavaScript / JScript • Loop statements var stdout = WScript.StdOut; var i; for (i=0 ; i < 10 ; i++) { stdout.WriteLine(“i=“+i); } var stdout = WScript.StdOut; var stdin = WScript.StdIn; while (! stdin.AtEndOfStream) { line = stdin.ReadLine() stdout.WriteLine(line); } var stdout = WScript.StdOut; var array = new Array(3); array[0] = 2; array[1] = 12; array[2] = 70; for (var value in array) { stdout.WriteLine("Value: "+array[value]); }

  23. JavaScript / JScript • Logical statements if (i == 5) { stdout.WriteLine(“Equality failed); } if (i == 1) { stdout.WriteLine(“Group 1”); } else { stdout.WriteLine(“Unknown group”); } if (i == 1) { stdout.WriteLine(“Group 1”); } else if (i == 2) { stdout.WriteLine(“Group 2”); } else if (i == 3) { stdout.WriteLine(“Group 3”); } else { stdout.WriteLine(“Unknown group”); }

  24. JavaScript / JScript • Regular Expression Support var stdout = WScript.StdOut; var stdin = WScript.StdIn; var re = new RegExp(".*error.*","i"); while (! stdin.AtEndOfStream) { var line = stdin.ReadLine(); if (line.match(re)) { stdout.WriteLine(line); } }

  25. JavaScript / JScript • Regular Expression Support var stdout = WScript.StdOut; var stdin = WScript.StdIn; var re1 = new RegExp("^#","i"); var re2 = new RegExp(".+a=([0-9]+).+c=([0-9]+)"); var lines = new Array(4); lines[0] = "# a b c"; lines[1] = " a=10 b=23 c=16"; lines[2] = " a=12 b=43 c=17"; lines[3] = " a=63, b=2, c=999"; for (var line in lines) { stdout.WriteLine(lines[line]); if (lines[line].match(re1)) continue; re2.exec(lines[line]); var avalue = RegExp.$1; var cvalue = RegExp.$2; stdout.WriteLine(avalue+", "+cvalue); } // lines contains (an array of strings): // # a b c // a=10 b=23 c=16 // a=12 b=43 c=17 // a=63, b=2, c=999

  26. JavaScript / JScript • Object support • var obj = new Object(); (instantiate object) • obj.method(...); (invoke method) • obj.property; (access property) • obj[“property”]; (access property)

  27. Script56.chm is the Windows scripting documentation file Local copy http://www.ae.gatech.edu/classes/ae6382/MS_scripting/ M/S Scripting Documentation

  28. VBScript • General purpose scripting language • Only available on Windows • Available as an ActiveX scripting engine, when run under the Windows Scripting Host it has general usage • Can be used as the client-side scripting in IE • Pros • Simple syntax (Basic) • Has support for objects • Cons • Windows only

  29. VBScript • Variables • Typeless, refer to primitive types and objects • Can be arrays • Declared with Dim statement • Uses a small subset of C operators • Statements are terminated by the end of line • Comments marked with ‘ (single quote character) • Subroutines Sub name

  30. VBScript • Loop statements Dim i i = 0 For i=0 To 9 Step 1 WScript.StdOut.WriteLine "i=" & i Next Do While Not WScript.StdIn.AtEndOfStream Dim line line = WScript.StdIn.ReadLine() WScript.StdOut.WriteLine(line) Loop Do Until WScript.StdIn.AtEndOfStream Dim line line = WScript.StdIn.ReadLine() WScript.StdOut.WriteLine(line) Loop Dim d 'Create a variable Set d = CreateObject("Scripting.Dictionary") d.Add "0", "Athens" 'Add some keys and items d.Add "1", "Belgrade" d.Add "2", "Cairo" For Each I in d Document.frmForm.Elements(I).Value = D.Item(I) Next

  31. VBScript • Logical statements If i = 5 Then WScript.StdOut.WriteLine “Value is “ & i End If If i = 1 Then WScript.StdOut.WriteLine“Group 1” Else WScript.StdOut.WriteLine “Unknown group” End If If i = 1 Then WScript.StdOut.WriteLine “Group 1” ElseIf i = 2 Then WScript.StdOut.WriteLine “Group 2” ElseIf i = 3 Then WScript.StdOut.WriteLine “Group 3” Else WScript.StdOut.WriteLine “Unknown group” End If

  32. VBScript • Regular Expression Support Dim re Set re = New RegExp re.Pattern = ".*error.*" re.IgnoreCase = True Do While Not WScript.StdIn.AtEndOfStream Dim line line = WScript.StdIn.Readline If re.Test(line) Then WScript.StdOut.WriteLine line End If Loop

  33. VBScript • Regular Expression Support Dim line Dim i Dim match Dim re1, re2, matches, submatches Dim lines(4) lines(0) = "# a b c" lines(1) = " a=10 b=23 c=16" lines(2) = " a=12 b=43 c=17" lines(3) = " a=63, b=2, c=999" set re1 = New RegExp set re2 = New RegExp re1.Pattern = "^#" re2.Pattern = ".+a=([0-9]+).+c=([0-9]+)" For i=0 To 3 line = lines(i) WScript.StdOut.WriteLine "--> " & line If Not re1.Test(line) Then ' WScript.StdOut.WriteLine line Set matches = re2.Execute(line) Set match = matches(0) WScript.StdOut.WriteLine match.SubMatches(0) & ", " & match.SubMatches(1) End If Next ' lines contains (an array of strings): ' # a b c ' a=10 b=23 c=16 ' a=12 b=43 c=17 ' a=63, b=2, c=999

  34. VBScript • Object support • Set obj = New Object (instantiate object) • obj.method(...) (invoke method) • obj.property (access property)

  35. Tcl • General purpose scripting language • Available on most platforms • In Windows it can run standalone an is also available as an ActiveX scripting engine • Pros • Interpreter has a small footprint • Easily embedded • Extensible using C • Cons • Strange syntax

  36. Tcl • Variables • Strings are the basic type • Can create lists and arrays • Uses the expr command to evaluate expressions • Format cmd op op ... op • set var value (set counter 5) • Reference value: $counter (set i $counter) • Use [ ... ] to evaluate immediately

  37. Tcl • Loop statements for (set i 0} {$i < 10} {incr i 3} { lappend aList $i } set aList set i 1 while {$i <= 10} { set product [expr $product * $i] incr i } set product set i 1 foreach value {1 3 5 7 11 13 17 19 23} { set i [expr $i * $value] } set i foreach x [list $a $b [foo]] { puts stdout “x = $x” }

  38. Tcl • Logical statements if {$i == 5} { puts stdout “Equality failed” } if {i == 1} { puts stdout “Group 1” } else { puts stdout “Unknown group” } if {i == 1} { puts tdout “Group 1” } elseif (i == 2) { puts tdout “Group 2” } elseif (i == 3) { puts stdout “Group 3” } else { puts stdout “Unknown group” }

  39. Tcl • Logical statements if {$x == 0} { puts stderr “Divide by zero” if (i == 1) { stdout.WriteLine(“Group 1”); } else { stdout.WriteLine(“Unknown group)”; } if (i == 1) { stdout.WriteLine(“Group 1”); } else if (i == 2) { stdout.WriteLine(“Group 2”); } else if (i == 3) { stdout.WriteLine(“Group 3”); } else { stdout.WriteLine(“Unknown group”); }

  40. Python • General purpose scripting language • Available on most platforms • On Unix/Linux it runs standalone using #! script file convention • In Windows it can run standalone an is also available as an ActiveX scripting engine • Designed from the start as an object oriented language • Pros • Has wide support and runs everywhere • Jython is a version coded in Java and can access Java classes directly • Cons • Has a syntax based on formatting

  41. Python • Variables • Typeless • Scalar name = “sam” • Lists names = [“sam”, “bill”, “ted”] • Tuples (1,2,5,20) • Dictionaries rooms = {“sam”:302,”bill”:305,”ted”:401} • Namespace separation (packages) • Block structure is indicated by spacing • Strings are immutable

  42. Python • Loop statements Count = 0 for line in range(0..10): count = count + 1 print count Count = 10 While count < 10: count = count + 1

  43. Python • Conditional statements Value = 2 if value%2 == 0: print “Value is even” else: print “Value is odd”

  44. Python • Object support • Class definition • Object instantiation • obj = Special() • Method invocation • obj.method1(…) class Special: def __init__(self): self.count = 0 def method1(self,…): … def method2(self,…): …

  45. Ruby • General purpose scripting language • Available on most platforms • On Unix/Linux it runs standalone using #! script file convention • Designed from the start as an object oriented language • Pros • Is becoming widely used • Has a more conventional syntax without the clutter of C and Perl • Cons • Is relatively new on scene

  46. Ruby • Variables • Typeless • $global_variable • @@class_variable • @instance_variable • local_variable • Types of variables • Scalar name = “sam” • Arrays names = [“sam”, “bill”, “ted”], names[2] • Hashes rooms = {“sam”:302,”bill”:305,”ted”:401}, rooms{“sam} • Namespace separation

  47. Ruby • Loop statements count = 1 while count < 10 count = count + 1 end count = 1 until count == 10 count = count + 1 end loop count = count + 1 end count = 1 begin count = count + 1 end while count < 10 count = 1 begin count = count + 1 end until count == 10

  48. Ruby • Conditional statements value = 6 if value%3 == 0 print “remainder 0” elsif value%3 == 1 print “remainder 1” else print “remainder 2” end value = 6 unless value == 6 print “value is not 6 end print “stop” if value == 0

  49. Ruby • Object support • Class definition • Object instantiation • obj = special.new • Method invocation • obj.method1(…) class special def initialize … end def method1(…) … end def method2(…) … end end

  50. Scripting in Unix • The usual method of executing a script in Unix/Linux is to include the location of the interpreter on line 1 • #!/usr/bin/perl • #!/usr/bin/sh • The script must be readable and executable by the user attempting to run it

More Related