Fix remove-analyst silent failure caused by set -e + pipefail
The script was exiting silently on the GROUPS=$(groups ... | cut ...) line — set -eo pipefail caused bash to terminate the script before any echo output, making it appear to do nothing. Replace set -euo pipefail with set -u and explicit error handling. Admin scripts must always report what happened, never exit silently. Also: use id -nG instead of groups|cut pipe, add verification step after userdel, and log each operation for visibility.
This commit is contained in:
parent
2181d490e9
commit
440662c8fe
1 changed files with 26 additions and 11 deletions
|
|
@ -1,11 +1,15 @@
|
|||
#!/bin/bash
|
||||
# Remove user (analyst or admin)
|
||||
# Usage: sudo remove-analyst username [--force]
|
||||
#
|
||||
# Note: This script uses explicit error handling instead of set -e.
|
||||
# set -e causes silent exits with command substitutions and pipefail,
|
||||
# which is unacceptable for admin scripts that must always report what happened.
|
||||
|
||||
set -euo pipefail
|
||||
set -u # Catch unset variables, but no -e (explicit error handling)
|
||||
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "This script must be run as root (use sudo)"
|
||||
echo "Error: This script must be run as root (use sudo)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
|
@ -37,17 +41,18 @@ if ! id "$USERNAME" &>/dev/null; then
|
|||
fi
|
||||
|
||||
# Prevent removing yourself
|
||||
CURRENT_USER=$(logname 2>/dev/null || echo "$SUDO_USER")
|
||||
CURRENT_USER=$(logname 2>/dev/null || echo "${SUDO_USER:-unknown}")
|
||||
if [[ "$USERNAME" == "$CURRENT_USER" ]]; then
|
||||
echo "Error: Cannot remove yourself"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get user groups for info
|
||||
GROUPS=$(groups "$USERNAME" 2>/dev/null | cut -d: -f2 || echo "")
|
||||
# Get user groups for info (safe extraction, no pipefail issues)
|
||||
GROUPS=$(id -nG "$USERNAME" 2>/dev/null) || GROUPS="(unknown)"
|
||||
|
||||
echo "Removing user: $USERNAME"
|
||||
echo "Groups: $GROUPS"
|
||||
echo " Groups: $GROUPS"
|
||||
echo " Home: /home/$USERNAME"
|
||||
|
||||
if [[ "$FORCE" != true ]]; then
|
||||
read -p "Are you sure? [y/N] " -n 1 -r
|
||||
|
|
@ -59,15 +64,25 @@ if [[ "$FORCE" != true ]]; then
|
|||
fi
|
||||
|
||||
# Remove user and home directory
|
||||
# userdel -r may fail if home is owned by someone else; fall back to manual cleanup
|
||||
echo " Deleting OS user..."
|
||||
if userdel -r "$USERNAME" 2>/dev/null; then
|
||||
: # success
|
||||
else
|
||||
userdel "$USERNAME"
|
||||
echo " User and home directory removed"
|
||||
elif userdel "$USERNAME" 2>/dev/null; then
|
||||
echo " User removed (userdel -r failed, cleaning up home manually)"
|
||||
if [[ -d "/home/$USERNAME" ]]; then
|
||||
rm -rf "/home/$USERNAME"
|
||||
echo "Home directory removed manually"
|
||||
echo " Home directory /home/$USERNAME removed"
|
||||
fi
|
||||
else
|
||||
echo "Error: Failed to remove user '$USERNAME'"
|
||||
echo " Check if processes are running as this user: ps -u $USERNAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify removal
|
||||
if id "$USERNAME" &>/dev/null; then
|
||||
echo "Warning: User '$USERNAME' still exists (OS login system may have re-created it)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "User '$USERNAME' removed successfully"
|
||||
|
|
|
|||
Loading…
Reference in a new issue