0

I often use the return/status value of a command in scripts. I'm curious why the finger command doesn't have a return value.

E.g.,

$ finger kevin
Login: klang                 Name: Kevin Kang
etc...                       Shell: /bin/bash
No mail.
No Plan.

$ echo $?
0

$ finger blabla
finger: blabla: no such user.

$ echo $?
0

Tested under Ubuntu and RH. I guess I could do some string processing and look for the "no such user" response, but that seems cumbersome.

The man page doesn't list any return values.


Yes yes, it has a return value of zero, but that's not helpful if that's the only value it has - I didn't realize I had to actually spell that out.

Levon
  • 11,174
  • 4
  • 45
  • 41
  • 2
    You mean a _non-zero_ return code? 0 is a return code. – Jeff Schaller Oct 26 '17 at 23:06
  • This question is asking about finger's return code, but is your (separate) question really about determining whether a given username exists on the system? There might be cleaner ways of doing that than parsing `finger` output. – Jeff Schaller Oct 26 '17 at 23:42
  • @JeffSchaller I was thinking of an easy bash script assignment for my student who have learned about command line args and exit values. I.e., give a user name, if it doesn't exist, issue an error message, otherwise use finger to display just their name (learn about `cut` :) .. they could grep `/etc/password` (I just checked `grep`, it actually return 0 or 1 depending on finding a match). Maybe I'll go that way. Just seemed odd to me that finger would not indicate an invalid user id, esp given that it checks and issues the appropriate error message. – Levon Oct 26 '17 at 23:47
  • 1
    If I can suggest a broader approach, `getent passwd | grep -q username` is a more-flexible way of listing users with access to the system (from external databases such as LDAP, Active Directory, NIS, etc) – Jeff Schaller Oct 26 '17 at 23:49
  • @JeffSchaller +1 :) .. thanks, our system is a pretty isolated system without using any of those services, but stored away for future reference/use. – Levon Oct 26 '17 at 23:52
  • Just keeping those students' minds open as long as I can! And there's no educational difference in checking return codes between `grep -q foo /etc/passwd` and `getent passwd | grep -q foo`; if you have no external sources, they return the same results. – Jeff Schaller Oct 26 '17 at 23:55

3 Answers3

3

finger doesn't consider a not found situation to be worth a non zero return code.

There are however real error cases like:

$ finger -x
finger: invalid option -- 'x'
usage: finger [-lmps] [login ...]
$ echo $?
1

Changing finger behavior would be trivial by modifying its source code but this was not done probably not to break scripts that expect finger never to fail.

I'm afraid you have to keep on with post-processing finger output to achieve your goal if you need to stick with finger..

Alternatively, you might use getent which supplies a useful return value:

$ getent passwd root
root:x:0:0:root:/root:/bin/bash
$ echo $?
0
$ getent passwd foo
$ echo $?
2
jlliagre
  • 60,319
  • 10
  • 115
  • 157
  • Ah, so it does actually return a non-zero value if used incorrectly - the man pages didn't list any return values. Any tips on how to post process? I guess I could use `awk` (or `grep`, not sure what if any return values it may have) to check for the "no such user" string, but I suspect bash might have some nicer built-in way of doing that. I'll have to look into that. – Levon Oct 26 '17 at 23:36
2

finger is not POSIX-specified, so implementations are free to handle return codes however they want. It appears that most Linux distributions have copied a BSD version of the package.

For Ubuntu, https://launchpad.net/ubuntu/+source/bsd-finger leads to a .bz2 archive containing the source code for finger.c. That code shows only two places where it returns something other than zero:

  • if you enter an invalid option (as jlliagre already pointed out).

  • if the calloc routine fails to find an integer's worth of free memory (in the userlist function) -- unlikely

Otherwise, the main function unconditionally returns zero.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
0

It does have a return code. You can't not have a return code. It's simply that finger is in your tests always returning 0. 0 is not 'no return code'.

DopeGhoti
  • 73,792
  • 8
  • 97
  • 133