6.5. Lab: Operator Practice#

Time to practice what you’ve learned. These exercises will help you internalize operator usage through hands-on experience.

6.5.1. Exercise 6: Integrated Challenge#

Write a script usercheck.sh that:

  • Takes a username as argument

  • Checks if /etc/passwd exists

  • Searches for the user in the file

  • Displays user info if found, or message if not

  • Shows total number of users on system

Example output:

$ bash usercheck.sh root
User 'root' found!
root:x:0:0:root:/root:/bin/bash

Total users on system: 23

$ bash usercheck.sh nonexistent
User 'nonexistent' not found
Total users on system: 23

Hints:

  • Use -f to check if /etc/passwd exists

  • Use grep to search for user

  • Use while loop to count lines

  • Use if/else for conditional logic

  • Use $? or || to check command success

6.5.2. Exercise 5: Loop Conditionals#

Write a script countdown.sh that:

  • Takes a number as argument (default 5)

  • Counts down from that number to 1

  • Says “Blastoff!” at the end

  • Skips even numbers (using continue)

Example output:

$ bash countdown.sh 5
5
3
1
Blastoff!

Hints:

  • Use a for loop with C-style syntax

  • Use continue to skip even numbers

  • Use modulo to check if even

  • Demonstrate loop control flow

6.5.3. Exercise 4: File Operations#

Write a script backup.sh that:

  • Takes a file path as argument

  • Checks if the file exists

  • Checks if it’s readable and writable

  • Creates a backup with timestamp if valid

  • Provides appropriate error messages

Example output:

$ bash backup.sh /etc/passwd
Error: /etc/passwd is not writable (cannot backup system files)

$ bash backup.sh myfile.txt
Backup created: myfile.txt.20240319_143022

Hints:

  • Use -f to check if it’s a regular file

  • Use -r to check if readable

  • Use -w to check if writable

  • Use date +%Y%m%d_%H%M%S to generate timestamp

  • Use logical operators to combine conditions

6.5.4. Exercise 3: String Comparisons#

Write a script greet.sh that:

  • Takes a name as argument (or defaults to “Guest”)

  • Compares the name and gives appropriate greetings

  • Checks if name is empty and warns if so

Example outputs:

$ bash greet.sh Alice
Hello, Alice! Nice to meet you!

$ bash greet.sh
Hello, Guest! Nice to meet you!

$ bash greet.sh admin
Welcome, admin! You have special access.

Hints:

  • Use ${var:-default} for default values

  • Use [[ ... ]] for easier string comparison

  • Use == to compare strings

  • Use logical operators && and ||

6.5.5. Exercise 2: Number Validator#

Write a script validate.sh that:

  • Takes one number as argument

  • Checks if it’s even or odd

  • Checks if it’s positive, negative, or zero

  • Checks if it’s divisible by 3

Example output:

$ bash validate.sh 12
12 is even
12 is positive
12 is divisible by 3

Hints:

  • Use modulo (%) to check even/odd and divisibility

  • Use comparison operators -gt, -lt, -eq

  • Use nested if/elif/else statements

6.5.6. Exercise 1: Basic Arithmetic#

Write a script math.sh that:

  • Takes two numbers as arguments

  • Calculates and prints their sum, difference, product, quotient, and remainder

Example output:

$ bash math.sh 15 4
Sum: 19
Difference: 11
Product: 60
Quotient: 3
Remainder: 3

Hints:

  • Use $((...)) for arithmetic

  • Use $1 and $2 for arguments

  • Check that exactly 2 arguments were provided