#!/usr/local/bin/tclsh # # Copyright (c): Uwe Schmidt, FH Wedel # # You may study, modify and distribute this source code # FOR NON-COMMERCIAL PURPOSES ONLY. # This copyright message has to remain unchanged. # # Note that this document is provided 'as is', # WITHOUT WARRANTY of any kind either expressed or implied. # die wichtigsten Tcl Kommandos #-------------------------- # puts: Ausgabe auf eine Datei # o.k. puts hello # Fehler: bad file identifier puts hello world # o.k. puts stdout hello puts stderr hello puts "hello world" #-------------------------- # # expr: Artithmetik expr 1 * 1 #-------------------------- # # set: Zuweisungen an Variablen # Zuweisung set a 5 #-------------------------- # # Variablen Substitution # Auslesen puts $a # Ersetzen in Texten puts $abc puts ${a}bc puts $a$a$a # Abfragen: gibt es eine Variable info exists a info exists unbekannt # Anhaengen append a 6 7 8 #-------------------------- # # Kommando Substitution set a [expr 1 * 1] #-------------------------- # # \ Substitution puts [expr $a * $a] puts \[expr\ \$a\ *\ \$a\] puts abc\ndef #-------------------------- # # "..." Quoting puts "hello world good bye " set a 5 set b 7 puts "$a * $b = [expr $a * $b]" #-------------------------- # # {...} Quoting puts {$a * $b = [expr $a * $b]} puts {hello world good bye } #-------------------------- # # expr: Syntax wie in C expr 1 < 5 expr ! ( 1 < 5 ) # string Vergleich expr {"Hellmut" < "Oskar"} #-------------------------- # # if {cond} then {cmd} else {cmd} set a 5 set b 7 if {$a < $b} { set res "a ist kleiner als b" } else { set res " a ist groesser als b" } puts $res set a emil set b egon if {"$a" == "$b"} { set res "a ist gleich b" } else { set res "a ist ungleich b" } puts $res #-------------------------- # # for {init-cmd} {test-expr} {incr-cmd} {body-cmd} for {set i 1} {$i < 4} {incr i} { puts "Das sage ich Dir zum $i. Mal!" } # foreach variable {value-list} {body-cmd} set res "" foreach i {Vanille Erdbeer Schoko} { append res "Eine Kugel $i bitte!\n" } puts $res # while {cond-expr} {body-cmd} set i 1 while {$i < 5} { puts "Das $i. Mal\n" incr i } #-------------------------- # # proc name {arg-list} {body} : Funktionen # ein Parameter proc square {x} { return [expr $x * $x] } # zwei Parameter proc times {x y} { return [expr $x * $y] } # eine Liste von Parametern: fester Name args proc echoargs args { set cnt 1 set res "" foreach i $args { append res "Der $cnt. Parameter hat den Wert: \"$i\"\n" incr cnt } puts $res } # 0 Parameter proc p0 {} { return "dies ist eine Prozedur ohne Parameter" } set gv "this is a global variable" proc p {} { global gv puts $gv } #-------------------------- # # exec: BS Kommandos ausfuehren exec date exec date +%H:%M exec ps -l set time [exec date "+%H:%M"] set status [exec ps -l] set this_file [exec cat Kommandos.tcl] #-------------------------- # # catch {cmd-body} res-var : Fehler abfangen if {[catch {exec fortune} res]} { puts stderr "Fehler: kein fortune Programm" set res "" } else { puts stderr "fortune erfolgreich aufgerufen" } puts $res #-------------------------- # # eval arg1 ... argn # nimmt seine Argumente, setzt diese zu einem Tcl Kommando zusammen # und fuehrt es aus, wird z.B. bei der Verarbeitung von Listen gebraucht set a "set x" set b "5" eval $a $b puts "$x" echoargs $a eval echoargs $a #-------------------------- # # weitere Kommandos: Einzelheiten mit man nachsehen # # Navigation durchs Filesystem # # pwd print working dir # cd change dir # file file information # glob