mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-23 13:45:19 +00:00
Refactor utility functions
Separate utitility functions into user manager functions and monitor control functions, disable somechecks and formatting
This commit is contained in:
parent
ab7a67087d
commit
7533606302
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to auto-detect displays and set common resolution
|
# Function to auto-detect displays and set common resolution
|
||||||
auto_detect_displays() {
|
auto_detect_displays() {
|
||||||
|
@ -18,8 +17,8 @@ auto_detect_displays() {
|
||||||
temp_common_resolutions=$(mktemp)
|
temp_common_resolutions=$(mktemp)
|
||||||
temp_resolutions=$(mktemp)
|
temp_resolutions=$(mktemp)
|
||||||
|
|
||||||
printf "%b\n" "$common_resolutions" > "$temp_common_resolutions"
|
printf "%b\n" "$common_resolutions" >"$temp_common_resolutions"
|
||||||
printf "%b\n" "$resolutions" > "$temp_resolutions"
|
printf "%b\n" "$resolutions" >"$temp_resolutions"
|
||||||
|
|
||||||
common_resolutions=$(comm -12 "$temp_common_resolutions" "$temp_resolutions")
|
common_resolutions=$(comm -12 "$temp_common_resolutions" "$temp_resolutions")
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to change monitor orientation
|
# Function to change monitor orientation
|
||||||
change_orientation() {
|
change_orientation() {
|
||||||
|
@ -47,7 +46,10 @@ change_orientation() {
|
||||||
2) orientation="left" ;;
|
2) orientation="left" ;;
|
||||||
3) orientation="right" ;;
|
3) orientation="right" ;;
|
||||||
4) orientation="inverted" ;;
|
4) orientation="inverted" ;;
|
||||||
*) printf "%b\n" "${RED}Invalid selection.${RC}"; return ;;
|
*)
|
||||||
|
printf "%b\n" "${RED}Invalid selection.${RC}"
|
||||||
|
return
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if confirm_action "Change orientation of $monitor_name to $orientation?"; then
|
if confirm_action "Change orientation of $monitor_name to $orientation?"; then
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to disable a monitor
|
# Function to disable a monitor
|
||||||
disable_monitor() {
|
disable_monitor() {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to duplicate displays
|
# Function to duplicate displays
|
||||||
duplicate_displays() {
|
duplicate_displays() {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to enable a monitor
|
# Function to enable a monitor
|
||||||
enable_monitor() {
|
enable_monitor() {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to extend displays
|
# Function to extend displays
|
||||||
extend_displays() {
|
extend_displays() {
|
||||||
|
@ -11,7 +10,7 @@ extend_displays() {
|
||||||
i=1
|
i=1
|
||||||
for monitor in $monitor_array; do
|
for monitor in $monitor_array; do
|
||||||
if [ "$i" -gt 1 ]; then
|
if [ "$i" -gt 1 ]; then
|
||||||
prev_monitor=$(echo "$monitor_array" | cut -d' ' -f$((i-1)))
|
prev_monitor=$(echo "$monitor_array" | cut -d' ' -f$((i - 1)))
|
||||||
if confirm_action "Extend $monitor to the right of $prev_monitor?"; then
|
if confirm_action "Extend $monitor to the right of $prev_monitor?"; then
|
||||||
printf "%b\n" "${GREEN}Extending $monitor to the right of $prev_monitor${RC}"
|
printf "%b\n" "${GREEN}Extending $monitor to the right of $prev_monitor${RC}"
|
||||||
execute_command "xrandr --output $monitor --right-of $prev_monitor"
|
execute_command "xrandr --output $monitor --right-of $prev_monitor"
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to manage monitor arrangement
|
# Function to manage monitor arrangement
|
||||||
manage_arrangement() {
|
manage_arrangement() {
|
||||||
|
@ -45,7 +44,10 @@ manage_arrangement() {
|
||||||
2) position="--right-of" ;;
|
2) position="--right-of" ;;
|
||||||
3) position="--above" ;;
|
3) position="--above" ;;
|
||||||
4) position="--below" ;;
|
4) position="--below" ;;
|
||||||
*) printf "%b\n" "${RED}Invalid selection.${RC}"; return ;;
|
*)
|
||||||
|
printf "%b\n" "${RED}Invalid selection.${RC}"
|
||||||
|
return
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
printf "%b\n" "${YELLOW}Choose the reference monitor:${RC}"
|
printf "%b\n" "${YELLOW}Choose the reference monitor:${RC}"
|
||||||
|
|
|
@ -10,7 +10,7 @@ setup_xrandr() {
|
||||||
pacman)
|
pacman)
|
||||||
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm xorg-xrandr
|
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm xorg-xrandr
|
||||||
;;
|
;;
|
||||||
apt-get|nala)
|
apt-get | nala)
|
||||||
"$ESCALATION_TOOL" "$PACKAGER" install -y x11-xserver-utils
|
"$ESCALATION_TOOL" "$PACKAGER" install -y x11-xserver-utils
|
||||||
;;
|
;;
|
||||||
apk)
|
apk)
|
||||||
|
@ -30,6 +30,7 @@ execute_command() {
|
||||||
command="$1"
|
command="$1"
|
||||||
printf "Executing: %s\n" "$command"
|
printf "Executing: %s\n" "$command"
|
||||||
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
|
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
|
||||||
|
#shellcheck disable=SC2181
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
printf "%b\n" "${RED}An error occurred while executing the command. Check /tmp/xrandr.log for details.${RC}"
|
printf "%b\n" "${RED}An error occurred while executing the command. Check /tmp/xrandr.log for details.${RC}"
|
||||||
fi
|
fi
|
||||||
|
@ -56,7 +57,7 @@ get_unique_resolutions() {
|
||||||
standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"
|
standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"
|
||||||
|
|
||||||
temp_file=$(mktemp)
|
temp_file=$(mktemp)
|
||||||
printf "%s" "$available_resolutions" > "$temp_file"
|
printf "%s" "$available_resolutions" >"$temp_file"
|
||||||
|
|
||||||
filtered_standard_resolutions=$(printf "%s" "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
|
filtered_standard_resolutions=$(printf "%s" "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
|
||||||
|
|
||||||
|
@ -64,8 +65,8 @@ get_unique_resolutions() {
|
||||||
|
|
||||||
available_res_file=$(mktemp)
|
available_res_file=$(mktemp)
|
||||||
filtered_standard_res_file=$(mktemp)
|
filtered_standard_res_file=$(mktemp)
|
||||||
printf "%s" "$available_resolutions" | sort > "$available_res_file"
|
printf "%s" "$available_resolutions" | sort >"$available_res_file"
|
||||||
printf "%s" "$filtered_standard_resolutions" | sort > "$filtered_standard_res_file"
|
printf "%s" "$filtered_standard_resolutions" | sort >"$filtered_standard_res_file"
|
||||||
|
|
||||||
remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")
|
remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")
|
||||||
|
|
||||||
|
@ -87,31 +88,6 @@ confirm_action() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEmpty() {
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
checkGroups() {
|
|
||||||
groups="$1"
|
|
||||||
available_groups="$2"
|
|
||||||
for group in $groups; do
|
|
||||||
if ! echo "$available_groups" | grep -q -w "$group"; then
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
confirmAction() {
|
|
||||||
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
|
||||||
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
checkEscalationTool
|
checkEscalationTool
|
||||||
setup_xrandr
|
setup_xrandr
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to Reset scaling back to 1 (native resolution) for all monitors
|
# Function to Reset scaling back to 1 (native resolution) for all monitors
|
||||||
Reset_scaling() {
|
Reset_scaling() {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to scale smaller monitors to the highest resolution of a bigger monitor
|
# Function to scale smaller monitors to the highest resolution of a bigger monitor
|
||||||
scale_monitors() {
|
scale_monitors() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
|
|
||||||
# Function to adjust brightness for a selected monitor
|
# Function to adjust brightness for a selected monitor
|
||||||
adjust_monitor_brightness() {
|
adjust_monitor_brightness() {
|
||||||
|
@ -29,7 +30,7 @@ adjust_monitor_brightness() {
|
||||||
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then
|
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then
|
||||||
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
||||||
printf "Press [Enter] to continue..."
|
printf "Press [Enter] to continue..."
|
||||||
read -r dummy
|
read -r _
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -37,7 +38,7 @@ adjust_monitor_brightness() {
|
||||||
if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$monitor_count" ]; then
|
if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$monitor_count" ]; then
|
||||||
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
||||||
printf "Press [Enter] to continue..."
|
printf "Press [Enter] to continue..."
|
||||||
read -r dummy
|
read -r _
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to set a monitor as primary
|
# Function to set a monitor as primary
|
||||||
set_primary_monitor() {
|
set_primary_monitor() {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../utility_functions.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./monitor-control-functions.sh
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Function to set resolutions
|
# Function to set resolutions
|
||||||
set_resolutions() {
|
set_resolutions() {
|
||||||
|
@ -33,7 +32,7 @@ set_resolutions() {
|
||||||
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$' || [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$((i - 1))" ]; then
|
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$' || [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$((i - 1))" ]; then
|
||||||
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
|
||||||
printf "%b\n" "Press [Enter] to continue..."
|
printf "%b\n" "Press [Enter] to continue..."
|
||||||
read -r dummy
|
read -r _
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -41,13 +40,13 @@ set_resolutions() {
|
||||||
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)
|
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)
|
||||||
|
|
||||||
temp_res_file=$(mktemp)
|
temp_res_file=$(mktemp)
|
||||||
printf "%b\n" "$resolutions" | awk '{print NR " " $0}' > "$temp_res_file"
|
printf "%b\n" "$resolutions" | awk '{print NR " " $0}' >"$temp_res_file"
|
||||||
|
|
||||||
i=1
|
i=1
|
||||||
while read -r resolution; do
|
while read -r resolution; do
|
||||||
echo "$resolution" >> "$temp_res_file"
|
echo "$resolution" >>"$temp_res_file"
|
||||||
i=$((i + 1))
|
i=$((i + 1))
|
||||||
done < "$temp_res_file"
|
done <"$temp_res_file"
|
||||||
|
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}=========================================${RC}"
|
printf "%b\n" "${YELLOW}=========================================${RC}"
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./user-manager-functions.sh
|
||||||
. ../utility_functions.sh
|
|
||||||
|
|
||||||
addToGroup() {
|
addToGroup() {
|
||||||
clear
|
clear
|
||||||
|
@ -24,7 +23,7 @@ addToGroup() {
|
||||||
printf "%b" "${YELLOW}Enter the groups you want to add user $username to (space-separated): ${RC}"
|
printf "%b" "${YELLOW}Enter the groups you want to add user $username to (space-separated): ${RC}"
|
||||||
read -r groups
|
read -r groups
|
||||||
|
|
||||||
checkEmpty "$groups" || exit 1
|
checkEmpty "$groups"
|
||||||
if ! checkGroups "$groups" "$available_groups"; then
|
if ! checkGroups "$groups" "$available_groups"; then
|
||||||
printf "%b\n" "${RED}One or more groups are not available.${RC}"
|
printf "%b\n" "${RED}One or more groups are not available.${RC}"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -34,14 +33,11 @@ addToGroup() {
|
||||||
|
|
||||||
printf "%b" "${YELLOW}Are you sure you want to add user $username to $groups_to_add? [Y/n]: ${RC}"
|
printf "%b" "${YELLOW}Are you sure you want to add user $username to $groups_to_add? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction "$confirm"
|
||||||
|
|
||||||
"$ESCALATION_TOOL" usermod -aG "$groups_to_add" "$username"
|
"$ESCALATION_TOOL" usermod -aG "$groups_to_add" "$username"
|
||||||
|
|
||||||
printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}"
|
printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
||||||
checkGroups
|
|
||||||
addToGroup
|
addToGroup
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./user-manager-functions.sh
|
||||||
. ../utility_functions.sh
|
|
||||||
|
|
||||||
createUser() {
|
createUser() {
|
||||||
clear
|
clear
|
||||||
|
@ -11,7 +10,9 @@ createUser() {
|
||||||
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
read -r username
|
read -r username
|
||||||
|
|
||||||
if ! echo "$username" | grep '^[a-zA-Z]*$' > /dev/null; then
|
checkEmpty "$username"
|
||||||
|
|
||||||
|
if ! echo "$username" | grep '^[a-zA-Z]*$' >/dev/null; then
|
||||||
printf "%b\n" "${RED}Username must only contain letters and cannot contain spaces.${RC}"
|
printf "%b\n" "${RED}Username must only contain letters and cannot contain spaces.${RC}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
@ -33,6 +34,4 @@ createUser() {
|
||||||
printf "%b\n" "${GREEN}To add additional groups use Add User To Groups${RC}"
|
printf "%b\n" "${GREEN}To add additional groups use Add User To Groups${RC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
||||||
createUser
|
createUser
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./user-manager-functions.sh
|
||||||
. ../utility_functions.sh
|
|
||||||
|
|
||||||
changePassword() {
|
changePassword() {
|
||||||
clear
|
clear
|
||||||
|
@ -12,13 +11,15 @@ changePassword() {
|
||||||
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
read -r username
|
read -r username
|
||||||
|
|
||||||
if id "$username" > /dev/null 2>&1; then
|
checkEmpty "$username"
|
||||||
|
|
||||||
|
if id "$username" >/dev/null 2>&1; then
|
||||||
printf "%b" "${YELLOW}Enter new password: ${RC}"
|
printf "%b" "${YELLOW}Enter new password: ${RC}"
|
||||||
read -r password
|
read -r password
|
||||||
|
|
||||||
printf "%b" "${YELLOW}Are you sure you want to change password for ""$username""? [Y/n]: ${RC}"
|
printf "%b" "${YELLOW}Are you sure you want to change password for ""$username""? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction "$confirm"
|
||||||
|
|
||||||
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
||||||
printf "%b\n" "${GREEN}Password changed successfully${RC}"
|
printf "%b\n" "${GREEN}Password changed successfully${RC}"
|
||||||
|
@ -28,6 +29,4 @@ changePassword() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
||||||
changePassword
|
changePassword
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./user-manager-functions.sh
|
||||||
. ../utility_functions.sh
|
|
||||||
|
|
||||||
deleteUser() {
|
deleteUser() {
|
||||||
clear
|
clear
|
||||||
|
@ -12,10 +11,12 @@ deleteUser() {
|
||||||
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
read -r username
|
read -r username
|
||||||
|
|
||||||
if id "$username" > /dev/null 2>&1; then
|
checkEmpty "$username"
|
||||||
|
|
||||||
|
if id "$username" >/dev/null 2>&1; then
|
||||||
printf "%b" "${YELLOW}Are you sure you want to delete user ""$username""? [Y/n]: ${RC}"
|
printf "%b" "${YELLOW}Are you sure you want to delete user ""$username""? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction "$confirm"
|
||||||
|
|
||||||
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null
|
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null
|
||||||
printf "%b\n" "${GREEN}User $username deleted successfully${RC}"
|
printf "%b\n" "${GREEN}User $username deleted successfully${RC}"
|
||||||
|
@ -25,6 +26,4 @@ deleteUser() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
||||||
deleteUser
|
deleteUser
|
|
@ -1,8 +1,7 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
# shellcheck disable=SC1091
|
||||||
|
. ./user-manager-functions.sh
|
||||||
. ../utility_functions.sh
|
|
||||||
|
|
||||||
removeFromGroup() {
|
removeFromGroup() {
|
||||||
clear
|
clear
|
||||||
|
@ -12,7 +11,9 @@ removeFromGroup() {
|
||||||
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
read -r username
|
read -r username
|
||||||
|
|
||||||
if ! id "$username" > /dev/null 2>&1; then
|
checkEmpty "$username"
|
||||||
|
|
||||||
|
if ! id "$username" >/dev/null 2>&1; then
|
||||||
printf "%b\n" "${RED}User $username does not exist.${RC}"
|
printf "%b\n" "${RED}User $username does not exist.${RC}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
@ -25,7 +26,7 @@ removeFromGroup() {
|
||||||
printf "%b" "${YELLOW}Enter the groups you want to remove user $username from (space-separated): ${RC} "
|
printf "%b" "${YELLOW}Enter the groups you want to remove user $username from (space-separated): ${RC} "
|
||||||
read -r groups
|
read -r groups
|
||||||
|
|
||||||
checkEmpty "$groups" || exit 1
|
checkEmpty "$groups"
|
||||||
if ! checkGroups "$groups" "$user_groups"; then
|
if ! checkGroups "$groups" "$user_groups"; then
|
||||||
printf "%b\n" "${RED}One or more specified groups do not exist.${RC}"
|
printf "%b\n" "${RED}One or more specified groups do not exist.${RC}"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -35,13 +36,12 @@ removeFromGroup() {
|
||||||
|
|
||||||
printf "%b" "${YELLOW}Are you sure you want to remove user $username from $groups_to_remove? [Y/n]: ${RC}"
|
printf "%b" "${YELLOW}Are you sure you want to remove user $username from $groups_to_remove? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction "$confirm"
|
||||||
|
|
||||||
|
#shellcheck disable=SC2086
|
||||||
$ESCALATION_TOOL usermod -rG $groups_to_remove "$username"
|
$ESCALATION_TOOL usermod -rG $groups_to_remove "$username"
|
||||||
|
|
||||||
printf "%b\n" "${GREEN}User successfully removed from $groups_to_remove${RC}"
|
printf "%b\n" "${GREEN}User successfully removed from $groups_to_remove${RC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
||||||
removeFromGroup
|
removeFromGroup
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
checkGroups() {
|
||||||
|
groups="$1"
|
||||||
|
available_groups="$2"
|
||||||
|
for group in $groups; do
|
||||||
|
if ! echo "$available_groups" | grep -q -w "$group"; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
confirmAction() {
|
||||||
|
confirm="$1"
|
||||||
|
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
||||||
|
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
checkEmpty() {
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
checkEnv
|
Loading…
Reference in New Issue
Block a user