Last active
May 10, 2016 04:56
-
-
Save yradunchev/91c8c215e243d3b5f2ef95626f3398d0 to your computer and use it in GitHub Desktop.
Copy group membership from one account to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Copy group membership from one account to another | |
| function yes_no() { | |
| read -p "$1 ([y]es or [N]o): " | |
| case $(echo $REPLY | tr '[A-Z]' '[a-z]') in | |
| y|yes) echo "yes" ;; | |
| *) echo "no" ;; | |
| esac | |
| } | |
| if [[ ! getopts qf:t: opts ]]; then | |
| echo "Usage: $(basename $0) [-q] -f username1 -t usename2"; | |
| exit 1; | |
| fi | |
| while getopts qf:t: opts; do | |
| case ${opts} in | |
| q) quiet=1 ;; | |
| f) from_usr=${OPTARG} ;; | |
| t) to_usr=${OPTARG} ;; | |
| \?) echo "Invalid option: -$OPTARG" ;; | |
| :) echo "Option -$OPTARG requires an argument." | |
| exit 2 | |
| ;; | |
| esac | |
| done | |
| for grps in $(groups ${from_usr} | cut -f2 -d':') ;do | |
| if [[ ${quiet} ]]; then | |
| usermod -A ${grps} ${to_usr} &> /dev/null || echo "Unable to add user ${to_usr} to ${grps}; | |
| elif [[ "yes" == $(yes_no "Add ${to_usr} to group ${grps}?")]]; then | |
| usermod -A ${grps} ${to_usr} &> /dev/null || echo "Unable to add user ${to_usr} to ${grps}"; | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.