在調試第三方Tcl腳本時,我遇到了一個非常奇怪的行爲:當腳本通過gunzip解壓現有文件時,Tcl文件存在'否認結果文件存在,即使它可以通過'ls'從shell中甚至通過Tcl'glob'來看到。我能夠複製在這個短(ish)腳本中的行爲:Tcl文件存在錯過生成的文件
#!/usr/bin/tclsh
proc assert {msg asserted} {
if {!$asserted} {
puts "Assertion failed: $msg"
exit 1
}
}
set script_path [info script]
set test_file [file join [file dirname $script_path] "test.tcl.gz"]
puts "The test file is '$test_file'"
assert "The test file does not exist" [file exists $test_file]
set this_dir [pwd]
set test_dir [file join $this_dir "test"]
if {![file exist $test_dir]} {
file mkdir $test_dir
}
puts "The test directory is '$test_dir'"
assert "'$test_dir' does not exist or is not a directory" [file isdirectory $test_dir]
file copy $test_file $test_dir
set working_file [file join $test_dir [file tail $test_file]]
assert "Failed to to copy the test file to '$working_file'" [file exists $working_file]
exec gunzip $working_file
puts "After decompression, $test_dir contains:"
foreach file [glob -dir $test_dir *] {
puts $file
}
set decompressed_file [file rootname $working_file]
assert "$decompressed_file does not exist" [file exists $decompressed_file]
我正在使用的測試文件只是腳本源的gzipped副本。它與腳本本身位於同一目錄中,如腳本所期望和檢查的那樣。下面是我運行它時得到的結果(tcl 8.5/CentOS 6.5):
bash-4.1$ ./test.tcl
The test file is './test.tcl.gz'
The test directory is '/home/jbolling/tmp/test'
After decompression, /home/jbolling/tmp/test contains:
/home/jbolling/tmp/test/test.tcl
Assertion failed: /home/jbolling/tmp/test/test.tcl does not exist
任何人都可以解釋這種行爲嗎?有沒有辦法說服'文件存在'來識別解壓縮的文件?
我沒有收到運行腳本時斷言失敗錯誤。你可以做一個'ls -ld。 test test/test.tcl'。 –
我也沒有看到斷言失敗。我的環境:Mac OS X 10.9.x,Tcl 8.5.9。 –
'file exists'命令幾乎直接映射到所有Unix(包括OSX)上帶'F_OK'標誌的'access()'系統調用;由於文件鎖定模型的不同,Windows更爲複雜。你在哪個平臺上? –