diff --git a/week 1/day 5/day5.md b/week 1/day 5/day5.md index dd906ce..e346e5d 100644 --- a/week 1/day 5/day5.md +++ b/week 1/day 5/day5.md @@ -1,9 +1,7 @@ - if the number of user dobules the time of execution will be multiply by 2 - $$ T(n, m) = n \times m T(2n, m) = 2 \times n \times m diff --git a/week 1/projects/User audit Script/correction.ps1 b/week 1/projects/User audit Script/correction.ps1 new file mode 100644 index 0000000..7b21aea --- /dev/null +++ b/week 1/projects/User audit Script/correction.ps1 @@ -0,0 +1,28 @@ +# PowerShell User Audit Script + +# Get all local usernames as strings +#! Get-LocalUser returns objects, not just strings. +#! -split $users is incorrect for objects. +$users = Get-LocalUser | Select-Object -ExpandProperty Name + +foreach ($username in $users) { + $badChar = $false + + # Check each character + foreach ($char in $username.ToCharArray()) { + if ($char -notmatch "[A-Za-z]") { + $badChar = $true + break + } + } + + # Check first character + $firstChar = $username[0] + $validFirstLetter = $firstChar -match "[A-Za-z]" + + # Flag invalid usernames + # * Note: -ge 20 includes length 20. If you want max length 20 valid, use -gt 20. + if ($badChar -or $username.Length -gt 20 -or -not $validFirstLetter) { + Write-Host "The username '$username' is invalid" + } +} diff --git a/week 1/projects/User audit Script/correction.sh b/week 1/projects/User audit Script/correction.sh new file mode 100644 index 0000000..4a21212 --- /dev/null +++ b/week 1/projects/User audit Script/correction.sh @@ -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 diff --git a/week 1/projects/User audit Script/userAudit.ps1 b/week 1/projects/User audit Script/userAudit.ps1 new file mode 100644 index 0000000..77291bb --- /dev/null +++ b/week 1/projects/User audit Script/userAudit.ps1 @@ -0,0 +1,21 @@ +$users = Get-LocalUser + +foreach($username in -split $users){ + + $badChar = $false + + foreach ($char in $username.ToCharArray()) { + if($char -notmatch "[A-Za-z]"){ + $badChar = $true + break + } + } + + $fistChar = $username[0] + $validFirstLetter = $fistChar -match "[A-Za-z]" + + if ($badChar -or $username.Length -ge 20 -or -not $validFirstLetter ){ + Write-Host "the userName " + $username + " is invalid" + } + +} \ No newline at end of file diff --git a/week 1/projects/User audit Script/userAudit.sh b/week 1/projects/User audit Script/userAudit.sh new file mode 100755 index 0000000..94b4006 --- /dev/null +++ b/week 1/projects/User audit Script/userAudit.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# userAudit + +#Read the passwd file for all the username +file=$(awk -F ":" '{print $1}' /etc/passwd) + +for username in $file; do + firstChar="${username:0:1}" + badChar=false + +#check if the username contain only letters + for ((i=0; i<${#username}; i++)); do + char="${username:i:1}" + if ! [[ "$char" =~ [A-Za-z] ]]; then + badChar=true + break + fi + done + +#if the username is not valid +# longer than 20 characters, start with not a letter and contain other char than letter +# print it + if ((${#username} >= 20)) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then + echo $username + fi +done + +