我必须编写一个脚本,它需要一个句子并输出字数,字符数(不包括空格),每个单词的长度和长度。我知道存在wc -m
来计数单词中的字符数,但是如何在脚本中使用它?Couting characters,words,lenght of the words and total lenght in a sentence
#!/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
听起来[标签:家庭作业。如果是这样,请标记为。对于我们中的很多人来说,如果我们看到这个标签,我们会做出更大的努力来帮助您了解答案是什么,而不仅仅是提供解决方案。 – ghoti 2012-01-05 02:56:22
谢谢,ghoti,会进一步提及。但我只是练习脚本和shell命令 – mydreamadsl 2012-01-05 02:59:26
你的问题是什么? – Raedwald 2013-07-09 19:55:51