zarrexx
zarrexx

Reputation: 27

Bash script to remove large number of users is not running

my bash script wont run, and does not output anything past echo "Running gke old user cleanup". There is no fail message, it just doesn't run. Any suggestions?

#!/bin/bash

set -o pipefail
set -o nounset

date
echo "Running old user cleanup"

for user in $(awk -F':' '$1 ~ /^kub-[a-z0-9]{20}$/ { print $1 }' /etc/passwd); do
  echo "Cleaning up '${user}'"
  userdel -r "${user}"
  rc=$?
  if [[ $rc != 0 ]]; then
    echo "Failed to cleanup '${user}': exit code: ${rc}"
  else
    echo "Successfully cleaned up '${user}'"
  fi
done

Upvotes: 1

Views: 77

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15293

Maybe simplify the loop.

while read user
do if userdel -r "${user}"
   then echo "Successfully cleaned up '${user}'"
   else echo "Failed to cleanup '${user}': exit code: '$?'"
   fi
done < <( awk -F':' '$1 ~ /^gke-[a-z0-9]{20}$/ { print $1 }' /etc/passwd )

Should at least be easier to debug.
Add set -x as suggested to see what's being evaluated.

Upvotes: 2

Related Questions