end of day 5 week 1
This commit is contained in:
11
week 1/day 5/day5.md
Normal file
11
week 1/day 5/day5.md
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
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
|
||||
Big-O: O(n·m)
|
||||
$$
|
||||
14
week 1/day 5/exo1.sh
Executable file
14
week 1/day 5/exo1.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
read -p "please enter a number : " number
|
||||
|
||||
if [[ -z $number ]]; then
|
||||
echo "you must enter a number"
|
||||
exit
|
||||
fi
|
||||
|
||||
if (( $number % 2 == 0 )); then
|
||||
echo "Even"
|
||||
else
|
||||
echo "Odd"
|
||||
fi
|
||||
19
week 1/day 5/exo2.sh
Executable file
19
week 1/day 5/exo2.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
read -p "please enter a phrase: " text
|
||||
|
||||
if [[ -z "$text" ]]; then
|
||||
echo "please enter a string"
|
||||
exit
|
||||
fi
|
||||
|
||||
count=0
|
||||
|
||||
for ((i=0; i<${#text}; i++)); do
|
||||
char="${text:i:1}"
|
||||
if [[ "$char" =~ [A-Za-z]]]; then
|
||||
count++
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Your string contain $count letter"
|
||||
31
week 1/day 5/exo3.sh
Executable file
31
week 1/day 5/exo3.sh
Executable file
@@ -0,0 +1,31 @@
|
||||
#!/bin/bash
|
||||
|
||||
read -p "Please enter your username: " username
|
||||
|
||||
if [[ -z "$username" ]]; then
|
||||
echo "You must enter a username"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
badChar=false
|
||||
|
||||
for ((i=0; i<${#username}; i++)); do
|
||||
char="${username:i:1}"
|
||||
if ! [[ "$char" =~ [A-Za-z] ]]; then
|
||||
badChar=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
firstChar="${username:0:1}"
|
||||
validFirstLetter=false
|
||||
|
||||
if [[ "$firstChar" =~ [A-Za-z] ]]; then
|
||||
validFirstLetter=true
|
||||
fi
|
||||
|
||||
if ! $badChar && (( ${#username} <= 20 )) && $validFirstLetter; then
|
||||
echo "The username $username is valid"
|
||||
else
|
||||
echo "The username is not valid"
|
||||
fi
|
||||
33
week 1/day 5/exo4.sh
Executable file
33
week 1/day 5/exo4.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
|
||||
users=("admin" "root1" "John_Doe" "Alice" "Bob42")
|
||||
|
||||
badChar=false
|
||||
|
||||
for username in "${users[@]}"; do
|
||||
|
||||
badChar=false
|
||||
|
||||
for ((i=0; i<${#username}; i++)); do
|
||||
char="${username:i:1}"
|
||||
if ! [[ "$char" =~ [A-Za-z] ]]; then
|
||||
badChar=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
firstChar="${username:0:1}"
|
||||
validFirstLetter=false
|
||||
|
||||
if [[ "$firstChar" =~ [A-Za-z] ]]; then
|
||||
validFirstLetter=true
|
||||
fi
|
||||
|
||||
if ! $badChar && (( ${#username} <= 20 )) && $validFirstLetter; then
|
||||
echo "The username $username is valid"
|
||||
else
|
||||
echo "The username $username is not valid"
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
158
week 1/day 5/readme.md
Normal file
158
week 1/day 5/readme.md
Normal file
@@ -0,0 +1,158 @@
|
||||
# 📘 Week 1 · Day 5 — Bash Exercises
|
||||
|
||||
This README documents **all exercises for Week 1 Day 5**, focused on **Bash scripting for Linux system administration and security workflows**.
|
||||
|
||||
The exercises mirror the logic implemented previously in **C, Python, SQL, and PowerShell**, reinforcing algorithmic thinking while introducing **Unix shell syntax, conditionals, loops, and defensive scripting**.
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Learning Objectives
|
||||
|
||||
By completing today’s exercises, you will:
|
||||
|
||||
* Use Bash variables and user input (`read`)
|
||||
* Apply conditional logic with `if / elif / else`
|
||||
* Loop over strings and arrays using `for`
|
||||
* Perform character validation using regex
|
||||
* Implement secure input validation logic
|
||||
* Analyze algorithm runtime and scalability
|
||||
* Gain confidence writing Linux automation scripts
|
||||
|
||||
---
|
||||
|
||||
## 🛠 Exercises Overview
|
||||
|
||||
### 🧪 Exercise 1 — Even / Odd Number Checker
|
||||
|
||||
**Task:**
|
||||
|
||||
* Prompt the user for a number
|
||||
* Determine whether the number is `Even` or `Odd`
|
||||
|
||||
**Concepts practiced:**
|
||||
|
||||
* Numeric input handling
|
||||
* Arithmetic expressions: `(( ))`
|
||||
* Conditional branching
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Exercise 2 — Count Letters in a String
|
||||
|
||||
**Task:**
|
||||
|
||||
* Prompt the user for a string
|
||||
* Count **letters only** (`A–Z`, `a–z`)
|
||||
* Ignore spaces, digits, and special characters
|
||||
|
||||
**Concepts practiced:**
|
||||
|
||||
* Looping over string characters
|
||||
* Regex matching using `[[ =~ ]]`
|
||||
* Counter variables
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Exercise 3 — Username Validator (Single User)
|
||||
|
||||
**Task:**
|
||||
Validate a username using these rules:
|
||||
|
||||
* Length ≤ 20 characters
|
||||
* Starts with a letter
|
||||
* Contains letters only
|
||||
* No digits
|
||||
* No spaces
|
||||
|
||||
**Output:**
|
||||
|
||||
* `Accepted` or `Refused`
|
||||
|
||||
**Concepts practiced:**
|
||||
|
||||
* Defensive input validation
|
||||
* Early exit with `break`
|
||||
* String length checking
|
||||
* First-character validation
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Exercise 4 — Validate Multiple Usernames
|
||||
|
||||
**Input:**
|
||||
|
||||
```bash
|
||||
users=("admin" "root1" "John_Doe" "Alice" "Bob42")
|
||||
```
|
||||
|
||||
**Task:**
|
||||
|
||||
* Loop through each username
|
||||
* Apply the same validation rules as Exercise 3
|
||||
* Print whether each username is valid or invalid
|
||||
|
||||
**Concepts practiced:**
|
||||
|
||||
* Array iteration
|
||||
* Nested loops
|
||||
* Reusable validation logic
|
||||
|
||||
---
|
||||
|
||||
### 🧪 Exercise 5 — Algorithm Analysis (Written)
|
||||
|
||||
**Task:**
|
||||
Explain what happens to execution time if the number of usernames doubles.
|
||||
|
||||
Use:
|
||||
|
||||
* `n` = number of usernames
|
||||
* `m` = average username length
|
||||
|
||||
**Expected result:**
|
||||
|
||||
```
|
||||
T(n, m) = n × m
|
||||
T(2n, m) = 2 × n × m
|
||||
Big-O: O(n·m)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔐 Security & Sysadmin Perspective
|
||||
|
||||
These exercises reflect real-world Bash usage in:
|
||||
|
||||
* Linux user management
|
||||
* Input validation scripts
|
||||
* SSH and access control automation
|
||||
* Log parsing and monitoring
|
||||
* Security audits and recon scripts
|
||||
|
||||
Strong validation and predictable runtime are critical for **secure and reliable system scripts**.
|
||||
|
||||
---
|
||||
|
||||
## ✅ Completion Criteria
|
||||
|
||||
Day 5 is complete when:
|
||||
|
||||
* Exercises 1–4 run correctly in Bash
|
||||
* Exercise 5 clearly explains runtime scaling
|
||||
* Scripts follow safe Bash practices (`[[ ]]`, quoting, defensive checks)
|
||||
|
||||
---
|
||||
|
||||
## 🔜 Next Step
|
||||
|
||||
After Day 5, Week 1 concludes.
|
||||
|
||||
Upcoming:
|
||||
|
||||
* **Week 2:** Data Structures (arrays, maps, sets)
|
||||
* Stronger algorithm focus
|
||||
* More security-oriented scripting examples
|
||||
|
||||
---
|
||||
|
||||
📌 *This README serves as a reference and checklist for Week 1 Day 5 Bash exercises.*
|
||||
Reference in New Issue
Block a user