Chapter 61: Exercises and Quiz
Exercises and Quizzes — what they are, why they are extremely important, how to do them properly, and I will give you a full set of graded exercises + quizzes that match exactly what we have covered so far.
Why Exercises & Quizzes Are Super Important (Teacher Speaking from Heart)
- Your brain forgets 70–80% of what you only read/watch within 24 hours (Ebbinghaus forgetting curve) → Doing exercises forces your brain to retrieve information → makes memory 5–10× stronger.
- Bash is a typing skill + debugging skill — you only become fast & confident by making mistakes and fixing them.
- Most job interviews / freelance tasks / server work give you real problems — not theory questions. → Practicing small real-life tasks prepares you 100× better than just reading.
- When you finish an exercise and it works → you get dopamine hit → you feel proud → you want to learn more.
So from today onwards: Every single class should end with 3–5 exercises + 1 mini quiz Do them in your own time — no hurry — but try first without looking back, then check solutions if stuck.
How to Do Exercises the Right Way (Teacher’s Method)
- Create a clean folder: mkdir ~/bash_exercises && cd ~/bash_exercises
- For each exercise → make a new file: nano exercise_01.sh
- Write the code → save → chmod +x exercise_01.sh → ./exercise_01.sh
- If it fails → read error message → fix → try again
- Only after it works → compare with my solution (I’ll give hints + full answer)
- Add your own comments → make it your own style
Ready? Let’s start!
Level 1 – Beginner Exercises (Basic Commands + Variables + Quoting)
Exercise 1 Write a script called user_info.sh that prints:
- Your username
- Your home directory
- Current date & time in nice format (example: Wednesday, 26 February 2025 14:30 IST)
- Number of files in current directory
- Disk usage of your home folder in human-readable format
Exercise 2 Create a script greet.sh that:
- Asks for your name (read)
- Asks for your favorite city
- Prints: “Namaste [name] ji! I see you love [city] very much 😄” “Have a great day in Hyderabad!”
Exercise 3 Fix this broken script (save as broken.sh, make executable, run, find & fix 5 mistakes):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/bash name=Webliance age = 25 echo Hello $name you are $age years old echo Your home is $HOME city = "Hyderabad" echo I live in $city |
Level 2 – Intermediate Exercises (if/else + loops + functions)
Exercise 4 Write backup_check.sh that:
- Checks if folder ~/Documents exists
- If yes → creates backup name Documents_YYYY-MM-DD.tar.gz in ~/backups/
- If no → prints error message and exits with status 1
- Uses function called make_backup
Exercise 5 Create file_counter.sh that:
- Loops through all files in current directory
- Counts how many .txt, .jpg, .pdf, .sh files there are
- Prints summary like: Text files: 12 Images: 8 PDFs: 5 Scripts: 3 Other files: 20
Exercise 6 Make a script grade_calculator.sh that:
- Asks for marks (0–100)
- Uses if-elif-else to print grade: ≥90 → A+ ≥80 → A ≥70 → B ≥60 → C <60 → Fail
- Bonus: also print motivational message for A+/Fail
Level 3 – Mini Quiz (Answer These Without Looking Back)
- What is the correct way to assign a variable? a) name = “Rahul” b) name=”Rahul” c) name =Rahul d) $name = “Rahul”
- How do you make a script executable? a) chmod +x script.sh b) chmod 777 script.sh c) sudo run script.sh d) bash -x script.sh
- What does this print?
|
0 1 2 3 4 5 6 7 |
name="Webliance" echo Hello $name from $city |
-
Which is the correct test for “greater than or equal” in [[ ]]? a) [[ $a >= $b ]] b) [[ $a -ge $b ]] c) [[ $a > $b ]] d) (( $a >= $b ))
-
How do you loop over all elements of an array safely?
a) for i in $array b) for i in ${array[@]} c) for i in “${array[@]}” d) for i in $array[*]
(Answers at the end — no peeking!)
Bonus Challenge (Do If You Feel Brave)
Challenge 1 Write a script daily_reminder.sh that:
- Checks current day of week
- If Monday → “Start week strong 💪”
- If Friday → “Weekend coming soon 🎉”
- Else → “Keep going boss!”
- Also prints current time & weather in Hyderabad (use curl wttr.in/Hyderabad?format=3)
Challenge 2 Make a script that asks for a folder path, then:
- If folder exists → counts files, shows biggest file (du -h | sort -h)
- If not → creates it and says “New folder created!”
How to Submit / Check Your Work
- Do at least 4–5 exercises
- Save them in ~/bash_exercises/
- Run them → see output
- If stuck → ask me: “Teacher, I tried exercise 4 but getting error XYZ — help!”
Answers to Mini Quiz
- b) name=”Rahul”
- a) chmod +x script.sh
- Hello Webliance from (empty — $city not set)
- b) [[ $a -ge $b ]] (or (( $a >= $b )) )
- c) for i in “${array[@]}” (safest)
You did great just by reading till here! Now open terminal → create folder → start typing exercises → make mistakes → fix them → feel proud.
Next class — tell me:
- Which exercises were hardest?
- Want solutions / hints for any?
- Want harder exercises (arrays + functions + cron together)?
- Or move to next topic (case statement, string manipulation, debugging)?
Teacher is waiting — go practice now! All the best from Hyderabad! 🐧✏️🔥 😄
