0

我有一个代码(1)应该在另一个代码中执行测试(它是一个伪 bash)。此代码 (1) 是使用“用户模拟”的“期望”编写的。

问题是当这段代码(1)执行一个系统(在这种情况下,系统“rm totoExpect.txttitiExpect.txt”)时,它只是没有找到titiExpected.txt,但是在那里!

这两个文件没有什么不同,即使权限相同,我也想不出一个不起作用的原因。

这是出现问题的代码 (1) 部分:

# test 5
proc t5 {} {
    send "ls > totoExpect.txt\r"
    send "cat < totoExpect.txt | wc -l > titiExpect.txt\r"
    send "cat titiExpect.txt\r"
    expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
    system "rm totoExpect.txt titiExpect.txt"
}

和错误信息:

ls > totoExpect.txt
out: totoExpect.txt
seq[0]: 'ls' 
-----3
ensishell>c
 ***** TEST 5 ok

rm: não foi possível remover "titiExpect.txt": Arquivo ou diretório não encontrado
child process exited abnormally
    while executing
"system "rm totoExpect.txt titiExpect.txt""
    (procedure "t5" line 6)
    invoked from within
"t5"
    ("eval" body line 1)
    invoked from within
"eval "t$t;" "
    ("foreach" body line 1)
    invoked from within
"foreach t {0 1 2 3 4 5 6 7} { eval "t$t;" } "
    invoked from within
"expect_user {
    -timeout 30 "auto\n" {
    puts "AUTO:\n";
    foreach t {0 1 2 3 4 5 6 7} { eval "t$t;" } 
    }
    -timeout 30 -re "(\[0123456789 \]+)\..."
    (file "./testshell.expect" line 99)
make: ** [test] Erro 1

其中“rm: não foi possível remover "titiExpect.txt": Arquivo ou diretório não encontrado" 的意思是“rm: it is not possible to remove "titiExpect.txt": file or directory not found”(抱歉...)

这是错误消息之后的 ls -l (所以titiExpect.txt 不应该在那里):

-rwxrwxr-x   1 fernando       fernando    273 Out 23 17:53 #Makefile#
-rwxrwxr-x   1 fernando       fernando   6238 Nov  5 21:18 #ensishell.c#
-rwxrwxr-x   1 fernando       fernando   1271 Out 24 20:30 #readcmd.h#
-rwxrwxr-x   1 fernando       fernando   3250 Nov  5 21:07 #testshell.expect#
-rwxrwxrwx   1 fernando       fernando    303 Out 24 20:21 Makefile
drwxrwxr-x   2 fernando       fernando   4096 Nov  4 19:06 SEPC shell
-rw-rw-r--   1 fernando       fernando 193453 Out 18 18:25 Sujet_shell.pdf
-rwxrwxr-x   1 fernando       fernando  25451 Nov  5 21:17 ensishell
-rwxrwxrwx   1 fernando       fernando   6238 Nov  5 20:32 ensishell.c
-rw-rw-r--   1 fernando       fernando  10664 Nov  5 21:17 ensishell.o
-rwxrwxr-x   1 fernando       fernando   7251 Nov  4 00:33 foo
-rw-rw-r--   1 fernando       fernando    173 Nov  4 00:33 foo.c
-rw-rw-r--   1 fernando       fernando     16 Nov  4 01:15 in.txt~
-rwxrwxrwx   1 fernando       fernando   6603 Out 23 00:37 readcmd.c
-rwxrwxrwx   1 fernando       fernando   1271 Out 23 01:24 readcmd.h
-rwxrwxrwx   1 fernando       fernando   1271 Out 23 00:37 readcmd.h~
-rw-rw-r--   1 fernando       fernando  11216 Nov  5 21:17 readcmd.o
-rwxrwxrwx   1 fernando       fernando   3250 Nov  5 20:41 testshell.expect
-rwx------   1 fernando       fernando   1263 Nov  5 12:43 toto.txt

最糟糕的问题是这段代码不应该被修改,但在我看来,这是失败的程序错误。(实际上,注释行解决问题..)

有任何想法吗?

4

1 回答 1

3

看。

# test 5
proc t5 {} {
    send "ls > totoExpect.txt\r"
    send "cat < totoExpect.txt | wc -l > titiExpect.txt\r"
    send "cat titiExpect.txt\r"
    expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
    system "rm totoExpect.txt titiExpect.txt"
}

第一个send板条箱创建一个名为totoExpect.txt\r.

第二个send生成一个名为titiExpect.txt\r. 该cat部分实际上失败了,因为没有 file totoExpect.txt,但是由于该命令是管道的一部分,而不是所述管道中的最后一个命令,因此 expect 不会将其视为错误。(你会看到titiExpect.txt\r文件是空的。)

以上\r是CR字符,可能是你错过的原因。在 Linux 中,文件名中完全允许使用字符(仅/作为\0禁止使用)。只需将其从测试中删除,您就会发现它工作正常。

或者,如果您坚持保留它,请始终保持它:

# test 5
proc t5 {} {
    send "ls > totoExpect.txt\r"
    send "cat < totoExpect.txt\r | wc -l > titiExpect.txt\r"
    send "cat titiExpect.txt\r"
    expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
    system "rm totoExpect.txt\r titiExpect.txt\r"
}

最后,删除文件时,建议使用该-f标志,因此rm如果其中一个文件碰巧不存在,则不会抱怨。

我的建议是将该测试重写为

# test 5
proc t5 {} {
    send "ls > totoExpect.txt"
    send "cat < totoExpect.txt | wc -l > titiExpect.txt"
    send "cat titiExpect.txt"
    expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
    system "rm -f totoExpect.txt titiExpect.txt"
}

根除 那些 飘忽不定\r的 .

于 2012-11-05T03:16:57.853 回答