macos 如何通过 OS X 中的命令行获取活动用户的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1104972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:16:08  来源:igfitidea点击:

How do I get the name of the active user via the command line in OS X?

macoscommand-line

提问by Lawrence Johnston

How do I get the name of the active user via the command line in OS X?

如何通过 OS X 中的命令行获取活动用户的名称?

回答by kent

as 'whoami' has been obsoleted, it's probably more forward compatible to use:

由于“whoami”已过时,因此使用它可能更向前兼容:

id -un

回答by Andrew

If you'd like to display the full name (instead of the username), add the -Fflag:

如果您想显示全名(而不是用户名),请添加-F标志:

$ id -F
Andrew Havens

回答by dfa

whoami

我是谁

EDIT

编辑

The whoami utility has been obsoleted by the id(1) utility, and is equivalent to id -un. The command id -pis suggested for normal interactive use.

whoami 实用程序已被 id(1) 实用程序淘汰,等效于id -un. id -p建议将该命令用于正常的交互式使用。

回答by Eric Koslow

I'm pretty sure the terminal in OS X is just like unix, so the command would be:

我很确定 OS X 中的终端就像 unix,所以命令是:

whoami

I don't have a mac on me at the moment so someone correct me if I'm wrong.

我现在没有 mac,所以如果我错了,有人会纠正我。

NOTE - The whoamiutility has been obsoleted, and is equivalent to id -un. It will give you the current user

注意 - 该whoami实用程序已过时,等效于id -un. 它将为您提供当前用户

回答by Lawrence Johnston

Via here

通过这里

Checking the owner of /dev/console seems to work well.

检查 /dev/console 的所有者似乎运作良好。

stat -f "%Su" /dev/console

stat -f "%Su" /dev/console

回答by ephemient

If you want to know who's currently logged in to the system:

如果您想知道谁当前登录到系统:

$ w
 15:56:14 up 5 days, 20:58,  6 users,  load average: 0.43, 0.53, 0.50
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
me       pts/2     Fri19    1:03m  0.98s  0.98s -/bin/bash
me       pts/3     09:55    6:00m  0.43s  0.43s /bin/bash
me       pts/5     15:56    0.00s  0.23s  0.00s w

(This is from a Linux system; the formatting on OS X may be slightly different, but the information should be about the same.)

(这是来自 Linux 系统;OS X 上的格式可能略有不同,但信息应该大致相同。)

There may be multiple login sessions; UNIX is designed to be a multi-user system, after all.

可能有多个登录会话;毕竟,UNIX 被设计成一个多用户系统。

回答by Adam Link

You can also use the lognamecommand from the BSD General Commands Manual under Linux or MacOS to see the username of the user currently logged in, even if the user is performing a sudooperation. This is useful, for instance, when modifying a user's crontab while installing a system-wide package with sudo: crontab -u $(logname)

您还可以使用lognameLinux 或 MacOS 下 BSD 通用命令手册中的命令来查看当前登录的用户的用户名,即使该用户正在执行sudo操作。例如,在使用 sudo 安装系统范围的软件包时修改用户的 crontab 时,这很有用:crontab -u $(logname)

Per man logname:

man logname:

LOGNAME(1)

NAME
    logname -- display user's login name

回答by Maurizio Loreti

The question has not been completely answered, IMHO. I will try to explain: I have a crontab entry that schedules a bash shell command procedure, that in turn does some cleanup of my files; and, when done, sends a notification to meusing the OS X notification center (with the command osascript -e 'display notification ...). If someone (e.g. my wife or my daughter) switches the current user of the computer to her, leaving me in the background, the cron script failswhen sending the notification.

这个问题还没有完全回答,恕我直言。我将尝试解释:我有一个 crontab 条目,它调度一个 bash shell 命令过程,它反过来对我的文件进行一些清理;完成后,使用 OS X 通知中心(使用命令)向我发送通知osascript -e 'display notification ...。如果有人(例如我的妻子或我的女儿)将计算机的当前用户切换为她,而将我留在后台,则发送通知时cron 脚本将失败

So, Who is the current usermeans Has some other people become the effective user leaving me in the background? Do stat -f "%Su" /dev/consolereturns the current active username?

那么,谁是当前用户意味着是否有其他人成为有效用户而将我留在后台?是否stat -f "%Su" /dev/console返回当前活动的用户名?

The answer is yes; so, now my crontab shell script has been modified in the following way:

答案是肯定的;所以,现在我的 crontab shell 脚本已按以下方式修改:

...
if [ "$(/usr/bin/stat -f ""%Su"" /dev/console)" = "loreti" ]
then /usr/bin/osascript -e \
  'display notification "Cleanup done" sound name "sosumi" with title "myCleanup"'
fi

回答by Alireza

getting username in MAC terminalis easy...

MAC 终端中获取用户名很容易...

I generally use whoamiin terminal...

我一般用whoami终端...

For example, in this case, I needed that to install Tomcat Server...

例如,在这种情况下,我需要它来安装Tomcat Server...

enter image description here

在此处输入图片说明

回答by Jonathan Leffler

Define 'active user'.

定义“活跃用户”。

If the question is 'who is the logged in user', then 'who am i' or 'whoami' is fine (though they give different answers - 'whoami' reports just a user name; 'who am i' reports on terminal and login time too).

如果问题是“谁是登录用户”,那么“我是谁”或“whoami”就可以了(尽管他们给出了不同的答案——“whoami”报告的只是一个用户名;“我是谁”在终端和登录时间也是)。

If the question is 'which user ID is the effective ID for the shell', then it is often better to use 'id'. This reports on the real and effective user ID and group ID, and on the supplementary group IDs too. This might matter if the shell is running SUID or SGID.

如果问题是“哪个用户 ID 是 shell 的有效 ID”,那么使用“id”通常更好。这会报告真实有效的用户 ID 和组 ID,以及补充组 ID。如果 shell 正在运行 SUID 或 SGID,这可能很重要。