* Cecil Westerhof <
Ce...@decebal.nl>
| That cannot be circumvented?
No, but it is not a problem.
| In Bash I did:
| systemctl status "${service}" || true
>
| How would I do the same in Tcl? I tried several things, but none
| worked. For example:
| set status [exec systemctl {*}"status ${service} || true"]
| gives:
| Unit \x7c\x7c.service could not be found.
| Unit true.service could not be found.
You need to understand how exec works.
Man exec(n)
[...]
If any of the commands in the pipeline exit abnormally or are killed
or suspended, then exec will return an error
[...]
If any of the commands writes to its standard error file and that
standard error is not redirected and -ignorestderr is not specified,
then exec will return an error
Thus if you don't [catch] the exec, the error will stop your script.
If the [catch] triggers, global variable errorCode (or the -errorcode
key in the optional options dict) holds more information about the cause
of the failure:
% catch {exec systemctl status foobar} output opts
1
% set output
● foobar.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
child process exited abnormally
% set opts
-code 1 -level 0 -errorcode {CHILDSTATUS 23458 3} -errorinfo {● foobar.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
child process exited abnormally
while executing
"exec systemctl status foobar"} -errorline 1
% set ::errorCode
CHILDSTATUS 23458 3
Note also the following section in exec(n):
WORKING WITH NON-ZERO RESULTS
To execute a program that can return a non-zero result, you
should wrap the call to exec in catch and check the contents of
the -errorcode return option if you have an error:
set status 0
if {[catch {exec grep foo bar.txt} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
} else {
# Some other error; regenerate it to let caller handle
return -options $options -level 0 $results
}
}
HTH
R'