Reputation: 21
I am programming TCL for Cisco TCL/IVR inside voice gateway.
I have some text files with public holidays dates, which I need to open on the primary server. If an error is returned while opening the file, I want to try to use a secondary backup server.
code:
if [catch {set fd [open $filename] } errmsg] {
error "Unable to open the file: $filename on Primary Server \n $errmsg"
set filename $httpsvr2$textfile
if [catch {set fd [open $filename] } errmsg] {
error "Unable to open the file: $filename on Primary Server \n $errmsg"
set Read 0
}
else {
set Read 1
}
}
else {
set Read 1
}
I was trying to use the Read
flag; if it is 1
, then I will search inside the file. If it is 0
, it's because the file couldn't be opened on any of the servers, so I will just treat the call as if it's a working (non-holiday) day.
However, in my current code when the first attempt to open the file fails, it automatically stops executing the script.
How could I continue executing after the first error? Should I make a procedure and return values like -1
? If so, how could I do that?
Upvotes: 2
Views: 5620
Reputation: 71588
The command error
exits the script (meaning once error
is reached, you could say that execution stops). You would probably be better off by puts
ing the error message through stderr
or a more suitable channel:
puts stderr "Unable to open the file: $filename on Primary Server \n $errmsg"
Upvotes: 2
Reputation: 415
I would make it a procedure like you are thinking
proc openFils { filename httpsvr2 textfile ) {
set fd -1
foreach f $filename $httpsvr2$textfile {
if { ! [file exists $f] } {
puts stderr "File $f not on system"
}
if [catch {set fd [open $f] } errmsg] {
puts stderr "Unable to open the file: $f \n $errmsg"
} else {
break
}
}
return $fd
}
Now you can perform an operation on the file handle 'fd', note: the above script will open and return the first file it can, not both.
Upvotes: 0