Hi I have this script that works great for one file.
e.g:
Code:
./filestats.sh /etc/hosts
/etc/hosts:
lines: 5
blank: 1 (20%)
chars: 251 in 16 word(s) (15 char/word)
I want it to work for multiple files:
Code:
e.g:
./filestats.sh /etc/hosts /etc/resolv.conf
How would I get the code below to dynamically parse any number of files?
shift, forloop? confused at the moment.
Thanks for your time.
Below is the code:
Code:
#!/bin/bash
#filestats
#!/bin/bash
if ! [[ "$#" -ge 1 ]]
then
echo "Usage: $0 <filename>"
exit 188
fi
#Count the amount of lines for the file
lines="$(wc -l < "$1")"
#Count the amount of blank lines in the file
blanklines="$(grep -c '^$' "$1")"
#Calculate the Percent
percent=$((blanklines*100/lines))
#Count the amount of chars for the file
chars="$(wc -c < "$1")"
#Count the amount of words for the file
words="$(wc -w < "$1")"
#Total Chars per word
totalchar=$((chars/words))
# Print the Outputs
totalchar=$((chars/words))
echo "$1:"
echo " " "lines: $lines"
echo " " "blank: $blanklines" '('$percent'%)'
echo " " "chars: $chars in ${words} word(s) ($totalchar char/word)"