简体   繁体   中英

Running DOS command or batch file in tcl script in Windows 7 with ActiveTcl 8.5

The following is the tcl code.

#!/bin/sh
# \
exec tclsh "$0"  ${1+"$@"}

package require Expect
package require log
source [file join [info library] init.tcl]

set exp::winnt_debug 1

log::lvChannel debug stdout
log::log debug "debug msg ON\r"

set env(TERM) dumb

array set OPTS {
host    ""
user    ""
passwd  ""
login   telnet
prompt  "(%|#|>|\\$) $"
ls      "/bin/ls -A1"
}

set xlpad "yahoo.com"

exec [auto_execok dir]

while {1} {
spawn ping  $xlpad
set loss 0
set replya 0
for { set pingc 0 } { $pingc < 3 } { incr pingc 1} {
    expect { 
        "timed out" {
            incr loss 1
            puts "intercepted time out message: $loss\n"
        }
        "Reply" {
            incr replya 1
        }    
    }
    if { $replya > 2 } {    
        break               
    }
   }            
break
}

return

I got the following messages:

couldn't execute "C:\Windows\System32\cmd.exe \c dir": no such file or directory
    while executing
"exec [auto_execok dir]"
    (file "mytst.tcl" line 35)

But the ping works ok.

auto_execok returns a list, which is passed to exec as a single argument. You need to expand the list:

exec {*}[auto_execok dir]

example:

% file mkdir tmp
% cd tmp
% close [open test w]
% exec [auto_execok dir]
couldn't execute "C:\WINNT\system32\cmd.exe \c dir": no such file or directory
% exec {*}[auto_execok dir]
 Volume in drive D is Data
 Volume Serial Number is ....

 Directory of d:\glennj\tmp

2011-09-30  10:08 AM    <DIR>          .
2011-09-30  10:08 AM    <DIR>          ..
2011-09-30  10:08 AM                 0 test
               1 File(s)              0 bytes
               2 Dir(s)  77,613,654,016 bytes free

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM