用户的完整 unix 用户名 [英] full unix username of a user
问题描述
想知道是否有一种巧妙的方法可以从 shell 获取完整的用户名?
<块引用>示例:如果我的 unix 用户名是 froyo 那么我想得到我的全名在这种情况下已在系统中注册 [froyo === Abhishek Pratap]
finger 命令可以做到这一点,但是对于所有登录的用户来说都同时......所以需要一些解析来获得正确的值.有什么更好的吗?
python 内部的任何东西都会很棒.
谢谢!-阿比
实现此目的的一种方法是使用 pwd.getpwuid
和 os.getuid
:
这是传统的 Unix 方式(基于标准的 C 库调用).
Wondering if you know if there is a slick way to get full username from shell?
example: if my unix username is froyo then I want to get my full name as registered in the system in this case [froyo === Abhishek Pratap]
finger command does it but for all the logged in users at the same time ..so that needs some parsing to get the right value. Anything slicker ?
Anything from inside python would be great too.
Thanks! -Abhi
One way to achieve this is using pwd.getpwuid
and os.getuid
:
>>> import os
>>> import pwd
>>> pwd.getpwuid(os.getuid())[4]
This is the traditional Unix way of doing it (based on standard C library calls).
这篇关于用户的完整 unix 用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!