【问题标题】:system command rm not working in expect program系统命令 rm 在期望程序中不起作用
【发布时间】:2012-11-04 21:57:13
【问题描述】:

我有一个代码 (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:无法删除“titiExpect.txt”:找不到文件或目录”(对此感到抱歉......)

这是错误消息之后的 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

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

有什么想法吗?

【问题讨论】:

  • totoExpect.txttitiExpect.txt 文件的文件权限?

标签: c linux bash expect rm


【解决方案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"
}

第一个 send 创建一个名为 totoExpect.txt\r 的文件。

第二个send 生成一个名为titiExpect.txt\r 的文件。 cat 部分实际上失败了,因为没有文件 totoExpect.txt,但由于该命令是管道的一部分,而不是所述管道中的最后一个命令,因此预期不会将其视为错误。 (您会看到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"
}

消除那些不稳定的\rs。

【讨论】:

  • 我会为最后一行 +1
  • 谢谢你的回答事实上(那是我的错,我没有说),我确实使用\作为转义字符的命令阅读器,所以保存的名称不是以\r结尾。所以删除它根本不起作用..我仍然有问题,但 -f 确实有帮助,现在至少它不会崩溃......
  • @FernandoP:你真的验证了吗?在 Linux 中,\r = CR = ASCII 13 是文件名中的有效字符。在真正的 Bash shell 中运行 touch $'titiExpect.txt\r' 看看我的意思。 ($'foo' 就是您在 Bash 中使用 C 样式的反斜杠转义指定 foo 的方式。)
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 2022-08-22
  • 2020-07-08
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
相关资源
最近更新 更多