I was writing a test on basic Unix commands etc, and there was a question show all registered users and their groups with one command. At the same time I can't use getent
command.
欢迎各位兄弟 发布技术文章
这里的技术是共享的
awk -F':' '{ print $1}' /etc/passwd | while read -r line; do id "$line"; done
或者把下面的代码存为一个文件 ,再 /bin/bash 此文件
awk -F":" 'NR==FNR {
m[$1] = "";
next;
}
{
for (i in m) {
if ($4 ~ i || $1 == i) {
m[i] = m[i] $1 " ";
}
}
}
END {
for (i in m) {
print i ":", m[i];
}
}' /etc/shadow /etc/group
1 | |
add a comment |
1 | A possible solution is for example with awk -F":" 'NR==FNR { if ($2 !~ /\!/ && $2 !~ /\*/) { m[$1] = ""; } next; } { for (i in m) { if ($4 ~ i || $1 == i) { m[i] = m[i] $1 " "; } } } END { for (i in m) { print i ":", m[i]; } }' /etc/shadow /etc/group You can remove |
1 | cat /etc/passwd (filter the contents with grep as per your requirements) Here you go: |
|