📖 Scenario: You work as a system helper who needs to check and organize information stored in text files on a computer. Many system details and logs are saved as text, so scripts help to read and manage this text quickly.
🎯 Goal: Build a simple Bash script that reads a list of usernames and their login counts, then filters and shows only those users who logged in more than a certain number of times.
📋 What You'll Learn
Create a text data variable with usernames and login counts
Add a threshold variable to filter users by login count
Use a loop to process each line of the text data
Print only users with login counts above the threshold
💡 Why This Matters
🌍 Real World
System administrators often write scripts to read and analyze text logs or user data to monitor system usage or detect issues.
💼 Career
Knowing how to process text in scripts is essential for automating tasks, managing servers, and handling data in many IT and DevOps roles.
Progress0 / 4 steps
1
Create the user login data
Create a Bash variable called user_data that contains these exact lines of text, each with a username and login count separated by a space: alice 5 bob 3 carol 8 dave 2
Bash Scripting
Hint
Use double quotes and \n to separate lines inside the variable.
2
Set the login count threshold
Create a variable called threshold and set it to 4 to filter users who logged in more than 4 times.
Bash Scripting
Hint
Just assign the number 4 to the variable threshold.
3
Filter users by login count
Use a while loop with read to process each line of user_data. Inside the loop, split each line into user and count. Then use an if statement to check if count is greater than threshold. If yes, print the user and count separated by a space.
Bash Scripting
Hint
Use done <<< "$user_data" to feed the variable lines into the loop.
4
Print the filtered users
Run the script so it prints only the users with login counts greater than the threshold. The output should show each qualifying user and their count on a separate line.
Bash Scripting
Hint
The script should print only users with counts above 4.
Practice
(1/5)
1. Why do many bash scripts process text files instead of other file types?
easy
A. Because text files are easy to read and manipulate with simple commands
B. Because binary files are faster to process
C. Because text files take up less disk space
D. Because scripts cannot open images or videos
Solution
Step 1: Understand file types in scripting
Text files contain readable characters, making them easy to handle with commands like grep and sed.
Step 2: Compare text and binary files
Binary files need special tools and are harder to manipulate directly in scripts.
Final Answer:
Because text files are easy to read and manipulate with simple commands -> Option A
Quick Check:
Text files = easy manipulation [OK]
Hint: Text files are simple to handle with basic commands [OK]
Common Mistakes:
Thinking binary files are easier to process
Assuming scripts can't open non-text files
Confusing file size with ease of processing
2. Which of the following is the correct syntax to search for the word error in a file named log.txt using grep?
easy
A. grep 'log.txt' error
B. grep error log.txt
C. grep -f error log.txt
D. grep log.txt error
Solution
Step 1: Identify grep command syntax
The correct syntax is grep [pattern] [file].
Step 2: Match pattern and file
Here, error is the pattern and log.txt is the file to search.
The input string is "apple banana cherry". Awk splits input by spaces and $2 refers to the second word.
Step 2: Identify the second word
The second word is "banana".
Final Answer:
banana -> Option D
Quick Check:
awk $2 = second word = banana [OK]
Hint: awk $2 prints the second word from input [OK]
Common Mistakes:
Confusing $1 and $2 fields
Expecting full line output
Not splitting input by spaces
4. This script is intended to replace all occurrences of 'cat' with 'dog' in a file named pets.txt. What is wrong?
sed 's/cat/dog/g pets.txt'
medium
A. sed cannot replace text
B. File name should come before the command
C. Missing closing single quote after g
D. The command should use double quotes instead of single quotes
Solution
Step 1: Check sed command syntax
The substitution command must be enclosed in quotes fully: 's/cat/dog/g'
Step 2: Identify the error in the given command
The closing single quote is missing after the g, causing a syntax error.
Final Answer:
Missing closing single quote after g -> Option C
Quick Check:
Quotes must enclose entire sed expression [OK]
Hint: Check for matching quotes around sed expressions [OK]
Common Mistakes:
Forgetting to close quotes
Misplacing file name inside quotes
Thinking sed can't replace text
5. You want to extract all lines containing the word error from multiple log files and save them into a single file errors.txt. Which script snippet correctly does this?
hard
A. grep error *.log > errors.txt
B. grep error errors.txt > *.log
C. grep > errors.txt *.log
D. grep *.log error > errors.txt
Solution
Step 1: Understand grep usage with multiple files
Using grep error *.log searches for 'error' in all files ending with .log.
Step 2: Redirect output to a file
The > operator sends all matching lines into errors.txt.
Final Answer:
grep error *.log > errors.txt -> Option A
Quick Check:
grep pattern files > outputfile [OK]
Hint: Use grep pattern files > outputfile to save matches [OK]
Common Mistakes:
Placing > before files
Swapping pattern and files
Using incorrect order of arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.