没办法,高手都跑到这里来了--Tcl写的脚本的问题--rm \无法删除"(N001"做文件名的文件。请各位大侠不吝指点一二。

akin2000 2001-05-30 11:58:00
#!/usr/local/bin/expect --

########################################################################
# Check command line
########################################################################
if { $argc < 2 } {
send_user "Usage: $argv0 <target> <file/directory> \[file/directory\] ...\n"
exit 1
}

set target [lindex $argv 0]
set paths [lrange $argv 1 end]

########################################################################
# Function: rmr_whatisit
#
# Description: Determines if the specified path exists, and returns
# 0 if path is a directory, 1 if path is file, -1 if
# path does not exist
########################################################################

proc rmr_whatisit { path } {
set matchidx [string last "/" $path]

if { $matchidx == -1 } {
set parentpath /hd0
} else {
set parentpath /hd0/[string range $path 0 [expr { $matchidx - 1 }]]
}

set pname [string toupper [string range $path [expr { $matchidx + 1 }] end]]

send "ll \"$parentpath\"\n"

set retval -1

expect {
-re ":.. $pname\[^\r\n\]+DIR\[^\r\n\]*\n\r" { set retval 0 ; exp_continue }
-re ":.. $pname\[^\r\n\]*\n\r" { set retval 1 ; exp_continue }
-gl "->" {}
timeout { send_user "timeout in rmr_whatisit\n" ; exit 1 }
}

return $retval
}

########################################################################
# Function: rmr_getlist
#
# Description: Returns a list of files in a directory. The list is
# empty if the directory is empty
########################################################################

proc rmr_getlist { path } {
send "ll \"/hd0/$path\"\n"

set retval {}

expect {
-re ":.. \[A-Z_0-9\]+\[.A-Z_0-9\]* " {
lappend retval [string trim [string range $expect_out(0,string) 6 end]]
exp_continue
}
-gl "->" {}
timeout { send_user "timeout in rmr_getlist\n" ; exit 1 }
}

return $retval
}

########################################################################
# Function: rmr_hdpath
#
# Description: Recursively remove file or directory
########################################################################

proc rmr_hdpath { path keepit } {
set type [rmr_whatisit $path];

if { $type == -1 } { ;# path does not exist
send_user "/hd0/$path: no such path\n"
return
} elseif { $type == 0 } { ;# path is a directory
set subpaths [rmr_getlist $path]
foreach subp $subpaths {
rmr_hdpath $path/$subp 0
}
if { $keepit == 0 } {
send "rmdir \"/hd0/$path\"\n"
expect -gl "->" {} timeout { exit 1 }
}
} else { ;# path is a file
if { $keepit == 0 } {
send "remove \"/hd0/$path\"\n"
expect -gl "->" {} timeout { exit 1 }
}
}
}

########################################################################
# Remove all specified paths recursively
########################################################################

spawn telnet $target
expect "*unknown*" { exit 1 } -gl "->" {} timeout { exit 1 }

set keepflag 0
foreach path $paths {

if { $path == "-k" } {
set keepflag 0
continue
}

if { $path == "+k" } {
set keepflag 1
continue
}

rmr_hdpath $path $keepflag
}

send "logout\n"
close
exit 0

现在某目录下除正常文件名外,还有(N001和(N002的文件,无法删除。如何在程序中改动呢?请各位大侠指点。谢谢,还请给出用rm \ 不成功的原因。再谢谢!

...全文
40 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧