切换以将用户添加到 sudo 组和从中删除用户?

Toggle for adding a user to and removing the user from sudo group?

我知道如何将用户添加到 sudo 组或如何从中删除 him/her:

sudo usermod -aG wheel  #  represents the username as an argument passed to the command
sudo gpasswd -d  wheel 

如何将其设为切换?如果用户在 sudo 组中,我会将其从组中删除,否则添加。这对于临时授予 sudo 权限很有用。

使用 if 检查用户当前是否在组中。 另见 Check if a user is in a group, from which I borrowed this check

if [[ " $(id -Gn "") " == *" wheel "* ]]; then
    sudo gpasswd -d "" wheel
else
    sudo usermod -aG wheel " "
fi