簡體   English   中英

系統命令rm在預期程序中不起作用

[英]system command rm not working in expect program

我有一個代碼(1),應該在另一個代碼中執行測試(其為偽重擊)。 此代碼(1)使用“用戶模擬”的“期望”來編寫。

問題是,當此代碼(1)只是找不到titiExpected.txt,而是在其中時,執行系統(在這種情況下,系統為“ rm totoExpect.txt titiExpect.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:可能的卸載程序” titiExpect.txt”:表示“ 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

最嚴重的問題是不應修改此代碼,但在我看來,這是程序失敗的錯誤。 (實際上,注釋行即可解決問題。)

有任何想法嗎?

看。

# 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"
}

消除那些不穩定的\\r s。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM