我必须编写一个脚本,该脚本需要一个句子并打印字数、字符数(不包括空格)、每个单词的长度和长度。我知道单词中存在wc -m
计数字符数,但是如何在脚本中使用它?
#!/bin/bash
mystring="one two three test five"
maxlen=0;
for token in $mystring; do
echo -n "$token: ";
echo -n $token | wc -m;
if [ ${#token} -gt $maxlen ]; then
maxlen=${#token}; fi;
done
echo "--------------------------";
echo -n "Total words: ";
echo "$mystring" | wc -w;
echo -n "Total chars: ";
echo "$mystring" | wc -m;
echo -n "Max length: ";
echo $maxlen