-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlinux-adduser2group
executable file
·100 lines (90 loc) · 2.55 KB
/
linux-adduser2group
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
# shellcheck disable=SC2089
# shellcheck disable=SC2090
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
# Debian/Ubuntu --help:
#
# adduser USER GROUP
# Add an existing user to an existing group
#- linux-adduser2group 1.0
## Usage: linux-adduser2group -- USER GROUP
## Add a user to a group cross-platform.
## Arguments are a subset of the Debian/Ubuntu 'adduser'.
##
## --force-badname Don't check for bad names.
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
FORCE_BADNAME=
USER=
GROUP=
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,force-badname -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--force-badname)
FORCE_BADNAME=true
shift
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
# [[ $# -eq 0 ]] || sh_script_usage
USER="$1"
GROUP="$2"
[[ -n "${USER}" ]] || {
echo_err "Please provide a USER name."
exit 1
}
[[ -n "${GROUP}" ]] || {
echo_err "Please provide a GROUP name."
exit 1
}
case ${OS_SHORT}-${OS_RELEASE_ID}-${OS_RELEASE_VERSION_ID} in
linux-alpine-*)
ARGS=()
[[ "${FORCE_BADNAME}" != "true" ]] || \
echo_warn "'--force-badname' is not supported. Trying anyway."
ARGS+=("${USER}")
ARGS+=("${GROUP}")
exe ${YP_SUDO:-} /usr/sbin/addgroup "${ARGS[@]}"
;;
linux-arch-*|linux-amzn-*|linux-centos-*|linux-rhel-*)
ARGS=()
[[ "${FORCE_BADNAME}" != "true" ]] || ARGS+=("--badnames")
ARGS+=("-a")
ARGS+=("-G" "${GROUP}")
ARGS+=("${USER}")
exe ${YP_SUDO:-} /usr/sbin/usermod "${ARGS[@]}"
;;
linux-debian-*|linux-ubuntu-*)
ARGS=()
[[ "${FORCE_BADNAME}" != "true" ]] || ARGS+=("--force-badname")
ARGS+=("${USER}")
ARGS+=("${GROUP}")
exe ${YP_SUDO:-} /usr/sbin/adduser "${ARGS[@]}"
;;
*)
echo_err "${OS_SHORT}-${OS_RELEASE_ID}-${OS_RELEASE_VERSION_ID} is an unsupported OS for adding a user to a group."
exit 1
;;
esac