Challenge ·Hard

RHCSA Prep: Fix the Broken Script That Processes Special-Character Filenames

Mister P
by  Mister P · on
Linux
A shell script that processes files with spaces and special characters in their names is producing "No such file or directory" errors even though the files exist. Diagnose the quoting bug and fix the script so it runs without errors.

The laborant user has a script at ~/process_reports.sh that is supposed to process report files sitting in ~/reports/. When run, the script throws "No such file or directory" errors — even though ls ~/reports/ clearly shows the files are there.

The files have names like report 2024.txt, costs [final].csv, and price$list.txt.

Investigate why the script fails, fix the quoting bugs in process_reports.sh, and make it run without any errors.

Hint 1 — Where to start

Run the script and read the error messages carefully. Then open the script and look at each wc -l line — compare how the filenames are quoted (or not quoted) against what the shell actually sees.

Hint 2 — What to check for each filename

There are three separate quoting problems, each involving a different special character: a space ( ), square brackets ([ ]), and a dollar sign ($). Check each wc -l invocation independently and consider which quoting form is appropriate for each case.

Hint 3 — The exact command that reveals the shell's view

Use printf '%q ' <argument> to see the shell-safe representation of what each argument will expand to before the command runs. For the dollar-sign filename in particular, try the difference between printf '%q\n' "price$list.txt" and printf '%q\n' 'price$list.txt' to see which form preserves the literal $.