Files
7-Modern-Languages-in-7-Wee…/week 1/projects/User audit Script/correction.sh
2026-01-26 08:39:36 +01:00

27 lines
749 B
Bash

#!/bin/bash
# userAudit: print invalid usernames
#! Safe iteration over /etc/passwd using while read -r
awk -F ":" '{print $1}' /etc/passwd | while read -r username; do
firstChar="${username:0:1}"
badChar=false
# Check if username contains only letters
for ((i=0; i<${#username}; i++)); do
char="${username:i:1}"
if ! [[ "$char" =~ [A-Za-z] ]]; then
badChar=true
break
fi
done
# Flag invalid usernames
#! Corrected logic to properly flag invalid usernames:
#! check if the firstchar is NOT a letter
if (( ${#username} > 20 )) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then
#! Quotes around $username in echo for robustness.
echo "$username"
fi
done