Project 1 audit user
This commit is contained in:
26
week 1/projects/User audit Script/correction.sh
Normal file
26
week 1/projects/User audit Script/correction.sh
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user