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:

    1. Dictionary File:

      • Ensure there is a file named dictionary.txt located 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.

    2. 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.

    3. 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.txt is missing or empty, the script should handle these cases gracefully by creating the file or prompting the user.

    4. 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:

  1. Creating the Script:

  2. Write the Script with Enhanced Logic:

  3. 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 if dictionary.txt exists.

      • If it doesn’t, it creates dictionary.txt with 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").

      • if block outputs an appropriate message based on whether the word was found.

  4. Set Script Permissions:

  5. Test Cases:

    • Positive Test: Run ./wordcheck.sh apple to confirm it returns “apple is a dictionary word.”

    • Negative Test: Run ./wordcheck.sh grape to confirm it returns “grape is not a dictionary word.”

    • No Argument: Run ./wordcheck.sh to 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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need