The idea for this exercise came from Sana Adam who said on Thursday "Sir, we could ask for the user input!" ;-)
We are going to emulate the behaviour of the wc
command with a Bash script. The input will be the user input.
In Bash, we use the built-in utility read
in order to read a line from the user input on the keyboard: the command read b
gets the input from the standard input stream (usually fed from the keyboard) and stores into the variable b
whatever you type until you press Enter
(of course, you choose the name of the variable to write into).
-
Write a small shell program that asks for the user input, and then reports in a user-friendly way a message telling the number of words and the number of characters in the input, e.g. "The input contained xx words, yy characters in total."
-
Add the line
#!/bin/bash
as the first line of your script, change its name towc
and change its permissions so that you can run it directly as an executable file, without having to pass it as an argument to abash
command. -
Run you program. What happens when you type
wc
? And what when you type./wc
?