Finally, write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come from a cat command to print a file. Here is an example, showing the contents of a file then using the word_counts.sh script:
[user@localhost ~]$ cat jedi_code
Jedi code:
There is no emotion, there is peace.
There is no ignorance, there is knowledge.
There is no passion, there is serenity.
There is no chaos, there is harmony.
There is no death, there is the Force.
[user@localhost ~]$ cat jedi_code | word_counts.sh
Line 1: 2
Line 2: 7
Line 3: 7
Line 4: 7
Line 5: 7
Line 6: 8
Total: 38
[user@localhost ~]$
Be sure to test your script thoroughly. When you're done, TAKE A SCREENSHOT (7) of the output of the script using some example inputs. TAKE A SCREENSHOT (8 – word_counts.sh) your script.
#!/bin/bash
i=1
while read line
do
echo -n "Line $i : "
echo -n $line | wc -w
i=$(( $i + 1 ))
done < $1
while read line
do
echo "Total: $line"
done < <(cat $1 | wc -w)
if you have any doubt then please ask me without any hesitation in the comment section below, if you like my answer then please thumbs up for the answer, before giving thumbs down please discuss the question it may possible that we may understand the question in a different way and I can edit and change the answers if you argue, thanks :)
Get Answers For Free
Most questions answered within 1 hours.