问题
I am using following lsof
command:
lsof -iTCP -a -sTCP:^LISTEN -a -p <pid>
If there is some output this command returns an exit code of 0
but if there is no output the exit code is 1
.
echo $?
Can anyone explain why is this happening? Is there a way to avoid it?
回答1:
This is the expected behaviour: you just get 0
if something was found.
From man lsof
:
DIAGNOSTICS
Errors are identified with messages on the standard error file.
Lsof returns a one (1) if any error was detected, including the failure to locate command names, file names, Internet addresses or files, login names, NFS files, PIDs, PGIDs, or UIDs it was asked to list. If the -V option is specified, lsof will indicate the search items it failed to list.
It returns a zero (0) if no errors were detected and if it was able to list some information about all the specified search arguments.
来源:https://stackoverflow.com/questions/29841984/non-zero-exit-code-for-lsof