I have to write a script that takes a sentence and prints the word count, character count (excluding the spaces), length of each word and the length. I know that there exist
You are very close. In bash you can use #
to get the length of your variable.
Also, if you want to use bash
interpreter use bash
instead of sh
and the first line goes like this -
#!/bin/bash
Use this script -
#!/bin/bash
mystring="one two three test five"
for token in $mystring
do
if [ $token = "one" ]
then
echo ${#token}
elif [ $token = "two" ]
then
echo ${#token}
elif [ $token = "three" ]
then
echo ${#token}
elif [ $token = "test" ]
then
echo ${#token}
elif [ $token = "five" ]
then
echo ${#token}
fi
done