如何读取wc -l命令的值

时间:2015-11-05 05:08:35

标签: tcl expect

我有一个要求,我需要读取wc -l命令的值。基本上我运行以下命令来获取值 5

bash-3.2$ wc -l sample.tcl
5 sample.tcl

现在使用 tcl 期望编程,我需要检查上面获得的值是否正确。

有人可以说如何使用tcl期望编程来写这个吗?

3 个答案:

答案 0 :(得分:1)

如果您只想执行shell命令,可以使用exec

% set result [exec wc -l sample.tcl]; # The variable 'result' will have the output
5 sample.tcl
% puts $result
5 sample.tcl

答案 1 :(得分:1)

您可以使用Tcl独立计算文件中的行数:

# I assume that the file is already opened and that the OS file pointer is at the start of the file
while {[gets $f dummyVariable] >= 0} {
    incr count
}

gets命令读取一行,所以我们只需计算它成功的次数。 (在该使用模式下,它会在EOF上返回-1。)

答案 2 :(得分:0)

以下是代码:

    set fname "Sample.tcl"
    puts "\n++++ The value of fname is $fname++++\n"
    send "cat > $fname\r"
    after 1000
    send "default ks\r"
    after 1000
    send "prompt 0\r"
    after 1000
    send "label ks\r"
    after 1000
    send " debug\r"
    after 1000
    send " error\r"
    after 1000      
    set var [exec wc -l $fname]
    puts "****The value of #line is $var*****"