我正在尝试在大文本文件中搜索字符串并相应地返回退出代码。对于上下文来说,文本文件是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选项仅删除最后一个字符,如果它是换行符。所有其他换行符都留在文本中。 –