Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

LAB 3 NS2 Preliminaries. Contents TCL/OTCl Fundamentals Creating Network Setting Connections Generating Traffic Inserting Errors Configuring for multicast.
Programming Using Tcl/Tk These slides are based upon u several Tcl/Tk text books u material byDr. Ernest J. Friedman-Hill.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
NS-2 LAB: TCL Semester A Miriam Allalouf.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Tcl/TK Tutorial Fan Yang Kristy Hollingshead
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Using Unix Shell Scripts to Manage Large Data
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Shell Control Structures CSE 2031 Fall August 2015.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Eagle: Tcl Implementation in C# 15th Annual Tcl Conference Joe Mistachkin.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Programming Using Tcl/Tk Week 2 Seree Chinodom
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Introduction to Tcl/Tk TraNese Christy U.S. Army Research Laboratory.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Dongsoo S. Kim  Tcl: Tool Command Language Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable.
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
NMD202 Web Scripting Week2. Web site
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Shell Control Structures CSE 2031 Fall June 2016.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
TCL/TK Tool Command Language/Tool Kit. What is TCL? u Tool Command Language u An interpreted programming language  Created by John Ousterhout.John Ousterhout.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
Using Unix Shell Scripts to Manage Large Data
Tutorial of Unix Command & shell scriptS 5027
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
Tutorial of Unix Command & shell scriptS 5027
TCL/TK Tool Command Language/Tool Kit.
NS-2 LAB: TCL Miriam Allalouf Semester A.
Tutorial of Unix Command & shell scriptS 5027
Tutorial Unix Command & Makefile CIS 5027
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Presentation transcript:

Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries Goal –Understand how to write Tcl programs Reading –Ch , Practical Programming in Tcl and Tk

Variable Scoping (cont.) upvar - call by reference –use to pass variable name rather than value –useful for when passing arrays to procs –upvar associates local variable with variable in higher context –upvar ?level? varName localvar –#0 is global scope –level is 1 (one level up) by default –example proc incrArray { array index amount } { upvar $array A incr A($index) $amount }

String Manipulation format - sprintf-style string generation format “Value is %.3f” [expr sqrt(10)] => Value is Pattern matching –regexp/regsub - regular subexpression matching/replacement –scan - scanf-style matching –string match - glob-style matching Query string length $str Search string first $str John String-list conversion –split, join

Eval eval str - evaluate strings as commands set string “Hello, World!” set cmd [list puts stdout $string] unset string eval $cmd => Hello, World! –eval concatenates several arguments into one list to evaluate uplevel ?level? cmd - do eval in another context subst str - do substitutions, but do not evaluate

File/Channel I/O Open/close –open, close - open/close file channel –socket - open TCP socket to port on host Search - cd, glob, pwd I/O - read, write, gets, puts, flush, seek Control –fconfigure - blocking, eof character, etc. Status –file, eof, tell, fblocked –fileevent - execute script when file/channel is readable or writeable Example set f [open [lindex $argv 1] r] if {[gets $f line] >= 0} { puts stdout $line } close $f

Processes Execute other programs exec grep foo << $input | wc Specifies a process pipeline –using C shell-like syntax –can put processes in background exec lpr -Plw501 motd.txt & –returns stdout of last subprocess exec date returns: Wed Jan 25 15:23:25 CST 1995 –returns process IDs if in background exec cat motd.txt | wc & returns: Note piping done in Tcl, not a string passed to csh

System Information Environment –array env contains environment variables env(EDITOR) Process –pid command returns process ID Time –clock command interfaces to system time –get time in clocks or seconds –convert to/from date/time strings –after - execute command after time delay

Errors Errors normally abort commands in progress, application displays error message set n 0 foreach i { } { set n [expr {$n + i*i}] } expr command prints: syntax error in expression “$n + i*i”

Errors (cont.) Global variable errorInfo provides stack trace set errorInfo => syntax error in expression “$n + i*i” while executing “expr {$n + i*i}” invoked from within “set n [expr {$n + i*i]...” (“foreach” body line 2)...

Errors (cont.) Can catch errors with catch command catch {expr {2 +}} msg => 1 set msg => syntax error in expression “2 +” Can generate errors error “bad argument” Global variable errorCode holds machine-readable information about errors (e.g. UNIX errno value)

Reflection and Debugging History –like csh history –mostly for interactive use Info –information about internal state –info exists A - does variable A exist? Command names –rename - rename/delete a command Timing –time - time a script for performance analysis Variable tracing –trace variable foo r PrintVar »calls PrintVar whenever foo is read

Code Libraries Loading source code into Tcl source init.tcl Autoloading source code –Tcl procedures loaded on demand –using search path of directories –auto_mkindex - generate a file-proc autoload index Packages –load file ?pkgname? ?interp? –load binary package from file and initialize –pkg_mkIndex - generate autoload index –package command - version control

Example: Random Numbers proc Random {} { global NextRandom set NextRandom [expr $NextRandom * ] return [expr abs($NextRandom % (0x7fffffff + 1))] } proc MakeRandomList len { for {set i 0} {$i < $len} {incr i} { lappend lst [Random] } return $lst } set NextRandom [expr {[pid]+[clock clicks]}] set rndlst [MakeRandomList 52]

Example: tgrep #/user/walker/bin/tclsh if {$argc != 2} { error “Usage: tgrep pattern fileName” } set f [open [lindex $argv 1] r] set pat [lindex $argv 0] while {[gets $f line] >= 0} { if [regexp $pat $line] { puts $line } catch { close $f }