*
[email protected] (Kenny McCormack)
| I have an Expect program that takes a single command line parameter,
| which is a filename (which must exist). The program starts with:
| #!/usr/bin/expect --
| if {$argc != 1 || ![file exists $argv]} { puts "Error";exit 1 }
| This works fine as long as the filename is "normal" - i.e., doesn't contain
| any spaces (and/or perhaps other weird characters). However, if it has a
| space, then the "file exists" check fails, because the actual value of
| $argv is something like:
| {/path/to/file name with spaces}
| The curly braces foo up the "file exists" check. I am able to workaround
| this by adding the following line before the above "if" statement:
| set argv [join $argv]
| And this seems to be the fix.
Wrong fix. As you know, argv is a list. If you use $argv in a string
context (as in 'file exists', which expects a single argument filename),
the string representation of that list is substituted. And if the list contains spaces, TCL needs to quote these spaces in case you want to
re-use the string representation as a list again - this is why the "{}"
creep in.
To access the individual elements of a list, you need to use lindex:
set filename [lindex $argv 0]
set second_arg [lindex $argv 1]
set third_arg [lindex $argv 2]
# or alternatively all in one go:
lassign $argv filename second_arg third_arg
if {![file exists $filename]} ...
Using 'join' on a list just joins the individual parts of the list with
spaces, which sometimes works for filenames. But as you have noted, in
other cases (double space in filename) it fails, so this is not a
solution.
Also, just printing 'Error' in case of failure is not very helpful to
the end-user (which might be you in three months, wondering what 'Error'
might mean). Just add "input file '$filename' does not exist" to the
error message.
HTH
R'
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)