【问题标题】:Bash script, for a given group name, print all the users and the groups they are in including the given oneBash 脚本,对于给定的组名,打印所有用户和他们所在的组,包括给定的一个
【发布时间】:2021-05-02 12:22:12
【问题描述】:

我正在创建一个带有组名的脚本,它应该打印所有用户和他们所在的组,包括给定的用户,但我仍然不知道如何正确执行,这是我的代码:

#!/bin/bash

# contentFile is the file you are trying to read
# contentFile will be a parameter supplied by the user
# thus we leave it empty for now
groupName=

## List of options the program will accept;
## those options that take arguments are followed by a colon
## in this case, group name: n
optstring=n:

## The loop calls getopts until there are no more options on the command line
## Each option is stored in $opt, any option arguments are stored in OPTARG
while getopts $optstring opt
do
    case $opt in
        n) groupName=$OPTARG ;; ## $OPTARG contains the argument to the option (contentFile in this context)
        *) exit 1 ;; ## exit if anything else other than -f file name was entered
    esac
done

groups=$(cat /etc/group | cut -d: -f1)

for user in $(cat /etc/passwd  | cut -d: -f1)
do 
    echo grep -q $groupName $groups
    if [ $? = 0  ]
    then
        echo "- grupuri:" "$user" "\n;"
    fi
done

我得到的输出

grep -q root adm wheel kmem tty utmp audio disk input kvm lp optical render storage uucp video users sys mem ftp mail log smmsp proc games lock network floppy scanner power systemd-journal rfkill nobody dbus bin daemon http systemd-journal-remote systemd-network systemd-resolve systemd-timesync systemd-coredump uuidd dnsmasq rpc gdm ntp avahi colord cups flatpak geoclue git nm-openconnect nm-openvpn polkitd rtkit usbmux fsociety postgres locate mongodb dhcpcd docker openvpn mysql
  • grupuri: mysql \n;

预期输出:

whoopsie - grupuri:whoopsie;
colord - grupuri:colord;
sssd - grupuri:sssd;
geoclue - grupuri:geoclue;
pulse - grupuri:audio;pulse;pulse-access;

非常感谢您的帮助

【问题讨论】:

  • 什么不起作用?有什么错误吗?
  • 你能不能只做 cut -d: -f1 /etc/passwd | xargs 组?
  • 感谢您的快速回复。我没有收到错误,但我没有得到我正在寻找的输出或数据。我更新了问题以包含我得到的输出。
  • @RamanSailopal,它只打印“根”组
  • 它应该打印在 /etc/passwd 中找到的每个用户的组

标签: linux bash shell


【解决方案1】:

考虑以下 bash 脚本;

group='certuser'
for user in $(awk -F ':' "/${group}/"'{print $4}' /etc/group | tr ',' '\n'); do
    echo "$(groups $user)"
done

在哪里

  1. awk -F ':' '/certuser/ { print $4 }' /etc/group

    获取 $group ('certuser') 组中的每个用户,由 , 分隔

    使用 删除组信息,因此我们只保留用户

  2. | tr ',' '\n'

    csv 到 for 循环的管道

  3. echo "$(groups $user)"

    使用groups 命令获取该组中的所有用户

【讨论】:

  • 感谢您的回复。请问如何使组名成为变量,例如 awk -F ':' '/$groupName/ { print $4 }' /etc/group;而不是 awk -F ':' '/certuser/ { print $4 }' /etc/group?谢谢。
  • 当然!请检查我的编辑,您可以使用a bash variable to create an AWK expression
  • awk -v grp="certuser" -F ':' '$0 ~ grp { if ($4 !="") { split($4,map,",");for (i in地图) { 打印地图[i] } } }' /etc/group | xargs 组允许一次性执行。
  • @0stone0,请问您是否有办法返回问题中的输出中的组字符串? (使用 ';' 分隔符而不是空格)。非常感谢!
  • 啊,我想通了。谢谢!
【解决方案2】:

使用getent 命令检索有关组和用户的信息。特别是,getent group 查询组,而getent passwd 查询用户。

#! /bin/bash
group=$1
gid=$(getent group "$1" | cut -d: -f3)
getent passwd \
| awk -F: -v gid=$gid '($4==gid){print $1}' \
| xargs -n1 groups

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多