Menu
CoddyTech

Recap - File Inspector

Part of the Bash Scripting section of Coddy's Terminal journey. Lesson 10 of 39.

challenge icon

Challenge

Hard

Write inspect.sh, a script that takes any number of paths and reports on each one.

  1. Loop over every argument, in the order given. Keep paths with spaces whole.
  2. For a regular file, print PATH: file, lines: N, where N is its line count. wc -l < PATH prints the count without the file name.
  3. For a folder, print PATH: folder.
  4. For a path that does not exist, print PATH: missing and add one to a counter.
  5. After the loop, exit with the number of missing paths as the exit code: 0 when every path exists.

Try it: run bash inspect.sh notes.txt photos old.txt; echo "exit $?". It prints:

notes.txt: file, lines: 3
photos: folder
old.txt: missing
exit 1

Then run bash inspect.sh a.txt b.txt; echo "exit $?". Neither exists, so it prints a.txt: missing, b.txt: missing and exit 2.

Try it yourself

Terminal

All lessons in Bash Scripting

Practice on your own: Terminal playground