我正在嘗試在大文本文件中搜索字符串並相應地返回退出代碼。對於上下文來說,文本文件是ModelSim日誌文件,我需要將1(壞)或0(好)的退出碼傳遞給調用以下TCL腳本(片段)的Windows批處理腳本:TCL字符串匹配不起作用
set sim_pass {# ** Failure: NONE. End of Test: PASS}
set sim_fail {# ** Failure: ERRORS. End of Test: FAIL}
# set each_line "# ** Failure: ERRORS. End of Test: FAIL\r";
set failed_sim 0
# Set top-level paths and testbench.
global app_sim_path reg_tb_path parent_path log_directory msim_ini
set TOP_LEVEL_NAME project_io_tb
cd $app_sim_path
# Create logfile.
set sim_log_file ${parent_path}/${log_directory}/${TOP_LEVEL_NAME}_sim.log
if {[file exists $sim_log_file]} {
file delete -force $sim_log_file
}
set fs [open $sim_log_file {RDWR CREAT EXCL}]
close $fs
# Initialize ModelSim.
puts "\[INFO\] Now testing ... $TOP_LEVEL_NAME"
vsim -modelsimini $msim_ini -c -quiet -l $sim_log_file
# Compile all files required for testbench.
source compile.tcl
# Log all signals for debug.
log -r /*
# Run simulation.
run -all
quit -sim
# Read log file.
set fptr [open ${sim_log_file} r]
set log_data [read $fptr]
close $fptr
# Split file contents on new line.
set line_data [split $log_data "\n"];
# Parse each line.
foreach each_line $line_data {
# Check if simulation test failed.
if {[string match $sim_fail [string trim $each_line]]} {
set failed_sim 1
break
}
}
# Return correct exit code.
if {$failed_sim} {
puts "Simulation Failed, returning $failed_sim"
} else {
puts "Simulation Passed, returning $failed_sim"
}
exit -code $failed_sim
quit -f
至少2出問題當我執行上面的腳本:
- 日誌文件都會以我的Windows命令外殼W/O換行符。如果我在「#讀取日誌文件」之後註釋掉所有內容,則不會發生這種情況。
- 即使字符串出現在日誌文件中,退出代碼也爲0。
我在做什麼錯?
[編輯]: 感謝所有的答覆。不幸的是,我仍然無法讓我的TCL腳本工作。這可能與我的日誌文件中包含了我在尋找下面的文本字符串的事實做:
# ** Failure: ERRORS. End of Test: FAIL
我不能改變文本的第一部分,「#**失敗:」作爲這是ModelSim用於日誌文件的語法。所以「**」可能會搞砸串匹配,因爲我不知道如何逃避它。
我真正想要做的是搜索子字符串「錯誤。測試結束:失敗」,這是我可以改變的文本。
請注意,我正在使用字符串命令在foreach循環中。
我仍然收到整個日誌文件到我的Windows命令窗口的瘋狂長回聲,它也被寫入到沒有換行符的日誌文件中。恐怕我沒有正確打開/關閉日誌文件,所以在我的原始代碼片段中添加了更多內容。
-nonewline選項僅刪除最後一個字符,如果它是換行符。所有其他換行符都留在文本中。 –