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?
When
who
is invoked with two arguments, the-m
flag is activated. From the manual page:In your case, the standard input is a pipe, rather than a terminal, and
who
(orlogname
) cannot determine the associated user. Consider this shell command snippet:From the GNU Coreutils
who.c
source:Since
ttyname()
returnsNULL
on errors, such as the file descriptor not corresponding to a terminal (ENOTTY
),who -m
will return immediately whenstdin
is a pipe.As for
logname
, its proper behavior is to return the string that would have been returned by thegetlogin()
function. Unfortunately, the bug section of thegetlogin()
manual page is quite clear: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 ofwho
, much likels
, has the distinct feel of being intended for humans, rather than machines.