我正在尝试运行一个非常简单的 tcl 脚本
package require Expect
spawn sftp user@host
我得到的错误是
该系统找不到指定的文件。在执行“spawn sftp user@host”时
我看到它的唯一原因是应该以某种方式指定 sftp 路径。我从批处理脚本中调用它,并且我还尝试在调用脚本之前将目录更改为 sftp 位置,但错误仍然相同。
By far the most likely cause of the issue here is that the sftp
program is not in a directory that is on your PATH
. The concept is almost the same across platforms, but with some minor niggles.
PATH
Check to see if sftp
is available in a PATH
-known directory by typing:
which sftp
At your shell prompt. It should respond with the location of the sftp
program, but if it isn't found then you get no response at all. If it isn't found, you'll need to find it yourself and add its location (strictly, the directory that contains the program) to the PATH
. Find the program with something like:
locate sftp
Or (very slow!):
find / -name sftp -print
To append a directory to the PATH
, do this in your shell:
PATH=$PATH:/the/dir/to/append
You can add a directory within the Expect script too (as long as it is before the spawn
, of course!):
append env(PATH) : /the/dir/to/append
PATH
On Windows, use Windows Search (Windows+F IIRC) and look for a file called sftp.exe
(there's also a command line search tool, but I forget how to use it).
With the Windows PATH, a little more care is required:
append env(PATH) ";" {C:\the\dir\to\append}
# Or this...
append env(PATH) ";" [file nativename C:/the/dir/to/append]
Which is to say, the Windows PATH
uses a different separator character (because :
is used for separating drive names from directory parts) and the native name of the directory must be used, rather than the somewhat-more-convenient forward-slash variation (the backslashes interact with Tcl's syntax, hence the {
braces}
). Forward-slashes can be used provided you use file nativename
to convert before appending, as in my second version.
You can use the Tcl command auto_execok
to find out whether a program is on your PATH
or not. For example:
puts [auto_execok sftp]
However, for some commands (notably start
on Windows) you get a more complex response; the command really exists as part of the code that supports interactive Tcl usage, describing how to run some external program which can sometimes be a lot more complex than it appears to be at first glance. Still, it approximates to a cross-platform version of which
as listed in the beginning of this answer...
Tcl 8.6 provides $tcl_platform(pathSeparator)
variable as a way to get the PATH
element separator character (a :
or ;
, depending on platform). Probably doesn't help you though, as 8.6 hasn't yet been distributed as widely as previous versions.