2012-11-04 76 views
0

我有一個代碼(1)應該在另一個代碼(它是僞bash)中執行測試。此代碼(1)是使用「期望」寫入「用戶模擬」的。系統命令rm不能在expect程序中工作

問題是當代碼(1)執行一個系統(在這種情況下,系統「rm totoExpect.txt titiExpect.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 "(\[\]+)\..." 
    (file "./testshell.expect" line 99) 
make: ** [test] Erro 1 

其中 「RM:NAO FOIpossível卸妝」 titiExpect.txt 「:Arquivo oudiretórionãoencontrado」 表示「rm:無法刪除」titiExpect.txt「:找不到文件或目錄」(對不起...)

and this是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 

最糟糕的問題是這樣的代碼是不應該被修改,但它在我看來,這是程序故障這失敗了。 (其實評論線解決問題..)

任何想法?

+0

'totoExpect的文件權限。txt'和'titiExpect.txt'文件? – askmish

+0

這兩個rwx爲用戶 – FernandoP

回答

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部分實際上失敗了,因爲沒有文件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秒。

+0

我會+1這只是爲最後一行 –

+0

謝謝你的回答 事實上(這是我的錯,我沒有說),命令的讀者,我確實利用\作爲轉義字符,因此保存的名稱不會以\ r結尾。所以刪除,不工作根本.. 我仍然有問題,但-f真的幫助,現在至少它不會崩潰... – FernandoP

+0

@FernandoP:你真的驗證它嗎?在Linux中,'\ r' = CR = ASCII 13是文件名中的有效字符。在真正的Bash shell中運行'touch $'titiExpect.txt \ r''來查看我的意思。 ('$'foo''就是您在Bash中用C風格反斜槓轉義指定'foo'的方式。) –