Red Hat Certified System Administrator RHCSA · Free Practice Question Medium
Question 43
On ServerB, write a script named “wordcheck.sh” that performs a dictionary lookup to determine if a given word exists in a custom dictionary file called “dictionary.txt”.
Requirements:
Dictionary File:
Ensure there is a file named
dictionary.txtlocated in the root directory (/) with a list of valid English words, one word per line.If the file doesn’t exist, the script should create it with a few sample words (e.g., "apple," "banana," "carrot") as placeholders and then proceed with the check.
Script Behavior:
The script should accept a single word as an argument.
It should perform a lookup to check if the word exists as an exact match in
dictionary.txt.The script should output:
“<word> is a dictionary word” if the word is found.
“<word> is not a dictionary word” if the word is not found.
Error Handling:
If no argument is provided, the script should display an error message and usage instructions, e.g., “Usage: ./wordcheck.sh <word>”.
If
dictionary.txtis missing or empty, the script should handle these cases gracefully by creating the file or prompting the user.
Permissions:
Ensure the script is executable by setting appropriate permissions.
Test Cases:
Run the script with words like “apple” (in the dictionary) and “grape” (not in the dictionary) to verify expected outputs.
-
A
Pass
-
B
Fail
Reveal correct answer
Correct answer: A
Explanation
Solution:
Creating the Script:
- # vim /wordcheck.sh
Write the Script with Enhanced Logic:
- #!/bin/bash
- # Check if a word was provided
- if [ -z "$1" ]; then
- echo "Error: No word provided."
- echo "Usage: ./wordcheck.sh <word>"
- exit 1
- fi
- word=$1
- # Check if dictionary.txt exists; if not, create it with sample words
- if [ ! -f /dictionary.txt ]; then
- echo "Dictionary file not found. Creating dictionary with sample words."
- echo -e "apple\nbanana\ncarrot" > /dictionary.txt
- fi
- # Search for the word in dictionary.txt
- if grep -q "^$word$" /dictionary.txt; then
- echo "$word is a dictionary word"
- else
- echo "$word is not a dictionary word"
- fi
Explanation of Key Steps:
Argument Check:
[ -z "$1" ]checks if no argument is provided. Displays an error and usage instructions if true.
Dictionary Check and Creation:
[ ! -f /dictionary.txt ]verifies ifdictionary.txtexists.If it doesn’t, it creates
dictionary.txtwith a few sample words.
Word Lookup:
grep -q "^$word$" /dictionary.txt: Searches for an exact match using regex to avoid partial matches (e.g., "car" won’t match "carrot").ifblock outputs an appropriate message based on whether the word was found.
Set Script Permissions:
- # chmod a+x /wordcheck.sh
Test Cases:
Positive Test: Run
./wordcheck.sh appleto confirm it returns “apple is a dictionary word.”Negative Test: Run
./wordcheck.sh grapeto confirm it returns “grape is not a dictionary word.”No Argument: Run
./wordcheck.shto confirm it displays the usage instructions.
Additional Notes for Learners:
Error Handling: Checking arguments and creating missing files builds robust scripts that handle edge cases, a valuable skill for real-world Linux system administration.
Using Regular Expressions: Exact matches ensure accuracy in dictionary lookups.
Permissions: Emphasize setting executable permissions and explain any issues learners may face with file ownership or permissions.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
