Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Bourne Shell Program which accepts one to many filenames on the command

ID: 3779654 • Letter: W

Question

Write a Bourne Shell Program which accepts one to many filenames on the command line. The program will compute the number of characters, words, and lines in each file. The program will also calculate the total number of characters, words, and lines found in all of the files. For this program, you may NOT use the built-in "we" program. Also, this program does not allow any switches or options on the command line. You need to perform error checking for valid filenames. The output of the program should % my_we file 1 file 2 file 1 stats: Number characters: 251 Number words: 65 Number lines: 34 file2 stats: Number characters: 456 Number words: 89 Number lines: 67 Complete stats: Total number characters: 707 Total number words: 154 Total number lines: 101

Explanation / Answer

[ -n "$1" ] || {
printf "error: insufficient argument. Usage: %s " "${0////}"
exit 1
}

file="$1"

[ -r "$file" ] || {
printf "error: file not found: '%s' " "$file"
exit 1
}

declare -i cnt=0

while read -r line || [ -n "$line" ]; do # read lines from file
temp=( $line ) # create temp array of words
cnt=$((cnt + ${#temp[@]})) # add no. of words to count
done <"$file"

printf " %s words in %s " "$cnt" "$file" # show results

exit 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote