Running “who am i” in a single command with sudo doesnt return anything

一世执手 提交于 2019-12-12 06:50:12

问题


I am trying to run "who am i" command in a single line in the following way and it returns nothing.

echo $password | sudo -u user -S who am i

Even logname command doesn't work in this way.

echo $password | sudo -u user -S logname

Can anyone please help?


回答1:


When who is invoked with two arguments, the -m flag is activated. From the manual page:

-m
    only hostname and user associated with stdin

In your case, the standard input is a pipe, rather than a terminal, and who (or logname) cannot determine the associated user. Consider this shell command snippet:

$ who am i
user   pts/11       2014-09-04 00:15
$ logname
user
$ echo | who am i
$ echo | logname
logname: no login name
$ 

From the GNU Coreutils who.c source:

if (my_line_only)
  {
    ttyname_b = ttyname (STDIN_FILENO);
    if (!ttyname_b)
      return;
    if (STRNCMP_LIT (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH) == 0)
      ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix.  */
  }

Since ttyname() returns NULL on errors, such as the file descriptor not corresponding to a terminal (ENOTTY), who -m will return immediately when stdin is a pipe.

As for logname, its proper behavior is to return the string that would have been returned by the getlogin() function. Unfortunately, the bug section of the getlogin() manual page is quite clear:

Note that glibc does not follow the POSIX specification and uses stdin instead of /dev/tty. A bug. (Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8 all return the login name also when stdin is redirected.)

Bottom line: you should probably update your code to use hostname and e.g. id -un or whatever other means your shell interpretter may offer to determine the current user. who -m and its friends are not very predictable - quite honestly the output of who, much like ls, has the distinct feel of being intended for humans, rather than machines.



来源:https://stackoverflow.com/questions/26168998/running-who-am-i-in-a-single-command-with-sudo-doesnt-return-anything

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!