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/passwdexistsSearches 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
-fto check if/etc/passwdexistsUse
grepto search for userUse
whileloop to count linesUse 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
forloop with C-style syntaxUse
continueto skip even numbersUse 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
-fto check if it’s a regular fileUse
-rto check if readableUse
-wto check if writableUse
date +%Y%m%d_%H%M%Sto generate timestampUse 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 valuesUse
[[ ... ]]for easier string comparisonUse
==to compare stringsUse 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 divisibilityUse comparison operators
-gt,-lt,-eqUse 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 arithmeticUse
$1and$2for argumentsCheck that exactly 2 arguments were provided