Recap - File Inspector
Part of the Bash Scripting section of Coddy's Terminal journey. Lesson 10 of 39.
Challenge
HardWrite inspect.sh, a script that takes any number of paths and reports on each one.
- Loop over every argument, in the order given. Keep paths with spaces whole.
- For a regular file, print
PATH: file, lines: N, where N is its line count.wc -l < PATHprints the count without the file name. - For a folder, print
PATH: folder. - For a path that does not exist, print
PATH: missingand add one to a counter. - After the loop, exit with the number of missing paths as the exit code:
0when 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 1Then 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
All lessons in Bash Scripting
1Arguments And Exit Codes
Script ArgumentsAll The ArgumentsExit CodesChecking ArgumentsRecap - Unit ConverterPractice on your own: Terminal playground