Tool Command Language (Tcl)初体验

Tcl只有一种语法: command arg arg ....

这种设计真的是非常好,简洁,快速!

它和很多程序设计语言不一样,它没有关键词!譬如if, switch在Tcl中也是命令。

初次体验Tcl的感觉是:利用Tcl来构建一些东西的时候,只做两件事情:一,调用命令;二,直接给参数或者用替换的方法给参数。 仅有的一种语法在有了“可替换“这个特性后,果然强大了很多!可见,好的设计,真的是至简的设计。你无法再用更少的东西来达到相同的目的了。

Tcl也是支持结构化的,比如c中的function,可以对应为Tcl中的proc(procedure的简称)。

proc procName args... {

main body

}

这样就相当于定义了一个procedure。其实,它还是个命令,这个命令做的事情的,当procName被调用时,执行mainbody.

以下给出我初步试验的脚本和结果:

#test tcl built-in commands
puts "======================start=========================="
#print "running" after 5 seconds
puts "waiting for 1 second ..."
after 1000
puts "running ..."

#append var(="Hello") with " " and "World!"
set var {}
append var "Hello" " " "World!"
puts ${var}

#test command 'for', 'if' and 'incr'
#output the odd number between [1:10]
for {set i 1} {$i <= 10} {incr i} {
    if {$i%2 != 0} {puts $i}
}

#command substitution
set var [expr 1+2+3]
puts $var

#file
puts [file attributes tmp.tcl]
puts [file atime tmp.tcl]
puts [file channels *err]
puts [file dirname ~]
puts [file dirname ~/Desktop]
#file copy tmp.tcl commandtest.tcl
set flag [file executable commandtest.tcl]
if {$flag == 1} {
    puts "commandtest.tcl is executable"
}
if {$flag == 0} {
    puts "commandtest.tcl is not executable"
}
file mkdir testdir1 testdir2 testdir3
puts [file normalize commandtest.tcl]
puts [file separator]

#test 'proc' and 'cd'
proc mypwd args {
    return [file normalize ./]
}

puts [mypwd]
set current_dir [file normalize ./]
cd /home/chenqi/Desktop
puts [mypwd]
cd ${current_dir}
puts [mypwd]

#test pwd
puts [pwd]

#test 'file stat', 'array' and 'foreach'
file stat commandtest.tcl var_stat_array
foreach {field value} [array get var_stat_array] {
    puts "field: $field; value: $value"
}

puts $::tcl_platform(platform)
#test 'switch'
switch  $::tcl_platform(platform) {
    unix {
    puts "the platform is unix"
    }
    windows {
    puts "the platform is windows"
    }
}

#test 'exec'
puts "executing 'uname -r' command"
puts [exec uname -r]

#test time
puts [time {
    puts [exec find /home/chenqi/MyPro/CFiles/ -name *.c]
}]
puts "===================end================================="





======================start==========================
waiting for 1 second ...
running ...
Hello World!
1
3
5
7
9
6
-group root -owner root -permissions 00644
1336199519
stderr
/
~
commandtest.tcl is executable
/home/chenqi/MyPro/ShellScript/tcl/commandtest.tcl
/
/home/chenqi/MyPro/ShellScript/tcl
/home/chenqi/Desktop
/home/chenqi/MyPro/ShellScript/tcl
/home/chenqi/MyPro/ShellScript/tcl
field: mtime; value: 1336203195
field: atime; value: 1336203234
field: gid; value: 0
field: nlink; value: 1
field: mode; value: 33261
field: type; value: file
field: ctime; value: 1336203195
field: uid; value: 0
field: ino; value: 2360375
field: size; value: 1724
field: dev; value: 2056
unix
the platform is unix
executing 'uname -r' command
2.6.32-21-generic
/home/chenqi/MyPro/CFiles/temp/bufsiz.c
/home/chenqi/MyPro/CFiles/Chapter-1/Hello.c
/home/chenqi/MyPro/CFiles/POSIX_Threads/autobakup.c
/home/chenqi/MyPro/CFiles/POSIX_Threads/thread1.c
/home/chenqi/MyPro/CFiles/virtual_process/virt_proc.c
/home/chenqi/MyPro/CFiles/virtual_process/test.c
/home/chenqi/MyPro/CFiles/virtual_process/hash.c
/home/chenqi/MyPro/CFiles/virtual_process/pid.c
/home/chenqi/MyPro/CFiles/IPC/semaphores/cfunc.c
/home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/client.c
/home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/server.c
/home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/tmp/procB.c
/home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/tmp/procA.c
/home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/server_with_queue.c
/home/chenqi/MyPro/CFiles/IPC/pipes/pipes_as_stdio/pipe_as_stdout.c
/home/chenqi/MyPro/CFiles/IPC/pipes/pipes_as_stdio/pipe_as_stdin.c
/home/chenqi/MyPro/CFiles/IPC/pipes/unnamed_pipes/procB.c
/home/chenqi/MyPro/CFiles/IPC/pipes/unnamed_pipes/procA.c
/home/chenqi/MyPro/CFiles/IPC/signals/test.c
/home/chenqi/MyPro/CFiles/IPC/signals/getpidbyname.c
/home/chenqi/MyPro/CFiles/IPC/signals/test2.c
/home/chenqi/MyPro/CFiles/programming-pearls/chapter1/qsort.c
/home/chenqi/MyPro/CFiles/programming-pearls/chapter1/mergesort.c
/home/chenqi/MyPro/CFiles/programming-pearls/chapter1/tmp/test.c
/home/chenqi/MyPro/CFiles/programming-pearls/chapter1/bitmap_sort.c
/home/chenqi/MyPro/CFiles/programming-pearls/chapter1/generate_random.c
4776 microseconds per iteration
===================end=================================

转载于:https://my.oschina.net/u/158589/blog/56250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值