linux下执行TCL脚本
你的linux装了tcl解释器了吗,一般是 /usr/bin/tclsh,如果确认你装了 (确认方法是在命令行下执行 tclsh),如果没问题,就在你的TCL脚本第一行加上
#!/usr/bin/env tclsh
然后确保你的脚本(假定脚本名字叫test.tcl) 是可执行的 (chmod +x test.tcl)
然后直接运行 ./test.tcl 即可。
实操一:每次读入固定字符数量
#Set the length of the data read each time
set LENGTH 256
#Set input and output file names
set FILE_IN ./temp_in.txt
set FILE_OUT ./temp_out.txt
if { $argc > 1 } {
#user input file_in name
set FILE_IN [lindex $argv 0]
#user input file_out name
set FILE_OUT [lindex $argv 1]
}
#open file for reading
set file_input [open $FILE_IN r]
#open file for writing
set file_output [open $FILE_OUT w+]
#
fconfigure $file_input -translation binary -encoding binary -buffering full
#print execution time
set SystenTime [clock seconds]
puts "start time ...... [clock format $SystenTime -format %D:%H:%M:%S]"
while { 1 } {
set buffer [read $file_input $LENGTH]
puts -nonewline $file_output $buffer
if { [ eof $file_input ] } {
break
}
}
close $file_input
close $file_output
实操二:每次读入一行数据
#Set the length of the data read each time
set LENGTH 256
#Set input and output file names
set FILE_IN ./temp_in.txt
set FILE_OUT ./temp_out.txt
if { $argc > 1 } {
#user input file_in name
set FILE_IN [lindex $argv 0]
#user input file_out name
set FILE_OUT [lindex $argv 1]
}
#open file for reading
set file_input [open $FILE_IN r]
#open file for writing
set file_output [open $FILE_OUT w+]
#
fconfigure $file_input -translation binary -encoding binary -buffering full
#print execution time
set SystenTime [clock seconds]
puts "start time ...... [clock format $SystenTime -format %D:%H:%M:%S]"
while { ![ eof $file_input ] } {
gets $file_input line
puts $file_output $line
}
close $file_input
close $file_output